From 16bc5ce85eb6c2d2ca7869d71207dd1c5889e1a4 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Fri, 2 May 2025 21:40:24 -0600 Subject: [PATCH 1/3] try automatic len type checks --- stack_strings.nim | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/stack_strings.nim b/stack_strings.nim index 6fbd511..b40a16d 100644 --- a/stack_strings.nim +++ b/stack_strings.nim @@ -154,8 +154,18 @@ template raiseInsufficientCapacityDefect(msg: string, capacity: Natural, request type StackString*[Size: static Natural] = object ## A stack-allocated string with a fixed capacity - lenInternal: Natural - ## The current string length + when Size + 1 < 128: + lenInternal: int8 + ## The current string length + elif Size + 1 < 32768: + lenInternal: int16 + ## The current string length + elif Size + 1 < 2147483648: + lenInternal: int32 + ## The current string length + else: + lenInternal: int + ## The current string length data*: array[Size + 1, char] ## The underlying string data. @@ -557,7 +567,7 @@ proc unsafeAdd*(this: var StackString, strOrChar: auto) {.inline.} = for i in this.len ..< newLen: this.data[i] = strOrChar[i - this.len] - this.lenInternal = newLen + this.lenInternal = typeof(this.lenInternal)(newLen) {.boundChecks: on.} {.boundChecks: off.} @@ -692,7 +702,7 @@ proc unsafeSetLen*(this: var StackString, newLen: Natural | BackwardsIndex, writ for i in lenRes ..< this.len: this.data[i] = '\x00' - this.lenInternal = lenRes + this.lenInternal = typeof(this.lenInternal)(lenRes) {.boundChecks: on.} proc trySetLen*(this: var StackString, newLen: Natural | BackwardsIndex, writeZerosOnTruncate: bool = true): bool {.inline.} = @@ -852,7 +862,7 @@ template toCstring*(this: StackString): cstring = when NimMajor > 1: ## Nim 2.0 no longer requires `unsafeaddr` cast[cstring](addr this.data[0]) else: - cast[cstring](unsafeaddr this.data[0]) + cast[cstring](unsafeAddr this.data[0]) proc toHeapCstring*(this: StackString): cstring {.inline.} = ## Allocates a `cstring` on the heap and copies the contents of the [StackString] into it. @@ -881,7 +891,7 @@ proc toHeapCstring*(this: StackString): cstring {.inline.} = when NimMajor > 1: ## Nim 2.0 no longer requires `unsafeaddr` moveMem(result, addr this.data[0], len) else: - moveMem(result, unsafeaddr this.data[0], len) + moveMem(result, unsafeAddr this.data[0], len) result[len] = '\x00' proc unsafeToStackString*(content: IndexableChars, size: static Natural): StackString[size] {.inline.} = From bc65898633d8c42d617223860c2070a1dfb675b0 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Fri, 2 May 2025 21:43:52 -0600 Subject: [PATCH 2/3] try automatic len type checks --- stack_strings.nim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stack_strings.nim b/stack_strings.nim index b40a16d..27e826f 100644 --- a/stack_strings.nim +++ b/stack_strings.nim @@ -154,7 +154,10 @@ template raiseInsufficientCapacityDefect(msg: string, capacity: Natural, request type StackString*[Size: static Natural] = object ## A stack-allocated string with a fixed capacity - when Size + 1 < 128: + when defined(stackStringsUseInt): + lenInternal: int + ## The current string length + elif Size + 1 < 128: lenInternal: int8 ## The current string length elif Size + 1 < 32768: @@ -164,7 +167,7 @@ type StackString*[Size: static Natural] = object lenInternal: int32 ## The current string length else: - lenInternal: int + lenInternal: int64 ## The current string length data*: array[Size + 1, char] @@ -646,7 +649,7 @@ proc addTruncate*(this: var StackString, strOrChar: auto): bool {.inline, discar for i in this.len ..< newLen: this.data[i] = strOrChar[i - this.len] - this.lenInternal = newLen + this.lenInternal = typeof(this.lenInternal)(newLen) {.boundChecks: on.} proc add*(this: var StackString, strOrChar: auto) {.inline, raises: [InsufficientCapacityDefect].} = From e4b527d600fcd78bffe2b4b7c9fb885d0652f22a Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Fri, 2 May 2025 22:12:22 -0600 Subject: [PATCH 3/3] try automatic len type checks --- stack_strings.nim | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/stack_strings.nim b/stack_strings.nim index 27e826f..3b8efbd 100644 --- a/stack_strings.nim +++ b/stack_strings.nim @@ -151,24 +151,23 @@ template raiseInsufficientCapacityDefect(msg: string, capacity: Natural, request raise newInsufficientCapacityDefect(msg, capacity, requestedCapacity) +template minLenInternal(size: static Natural): auto = + when defined(stackStringsUseInt): + int + elif size < 127: + int8 + elif size < 32767: + int16 + elif size < 2147483647: + int32 + else: + int64 + type StackString*[Size: static Natural] = object ## A stack-allocated string with a fixed capacity - when defined(stackStringsUseInt): - lenInternal: int - ## The current string length - elif Size + 1 < 128: - lenInternal: int8 - ## The current string length - elif Size + 1 < 32768: - lenInternal: int16 - ## The current string length - elif Size + 1 < 2147483648: - lenInternal: int32 - ## The current string length - else: - lenInternal: int64 - ## The current string length + lenInternal: minLenInternal(Size) + ## The current string length data*: array[Size + 1, char] ## The underlying string data.