Skip to content

Commit

Permalink
Merge pull request #3 from beef331/master
Browse files Browse the repository at this point in the history
Fixed dollar operator and tests
  • Loading branch information
termermc authored Aug 19, 2023
2 parents 16a4f14 + 948796c commit 98df4ce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
5 changes: 2 additions & 3 deletions stack_strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ func `$`*(this: StackString): string {.inline.} =
when stackStringsPreventAllocation:
{.fatal: "The `$` proc can allocate memory at runtime, see `stackStringsPreventAllocation`".}

{.suppress: "XDeclaredButNotUsed".}
const errMsg = "Conversion of StackString to string with `$` proc. If this was intentional, use `toString` instead."
const errMsg {.used.} = "Conversion of StackString to string with `$` proc. If this was intentional, use `toString` instead."
when fatalOnStackStringDollar:
{.fatal: errMsg.}
when warnOnStackStringDollar:
Expand Down Expand Up @@ -857,5 +856,5 @@ proc toHeapCstring*(this: StackString): cstring {.inline.} =
# We don't need a zeroed block of memory because we'll be overwriting it manually
result = cast[cstring](createU(char, len + 1))

moveMem(addr result[0], addr this.data[0], len)
moveMem(result, addr this.data[0], len)
result[len] = '\x00'
Binary file added tests/t_basic
Binary file not shown.
6 changes: 4 additions & 2 deletions tests/t_basic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ test "Random tests":
# The string will be truncated, and the truncated data will be overwritten with zeros
str1.unsafeSetLen(5)
check str1 == "Hello"
check str1.data == ['H', 'e', 'l', 'l', 'o', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']
check str1.data == ['H', 'e', 'l', 'l', 'o', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']

# If we're sure it's safe to skip overwriting the truncated data with zeros, we can disable it
str2.unsafeSetLen(2, writeZerosOnTruncate = false)
check str2 == "Hi"
check str2.data == ['H', 'i', ' ', 'w', 'o', 'r', 'l', 'd']
check str2.data == ['H', 'i', ' ', 'w', 'o', 'r', 'l', 'd', '\0']

# It works with BackwardsIndex, too
str3.unsafeSetLen(^1)
Expand All @@ -35,3 +35,5 @@ test "Random tests":

str1.unsafeSet(5, 'a')
check str1.unsafeGet(5) == 'a'
discard $str1

0 comments on commit 98df4ce

Please sign in to comment.