Skip to content

Commit

Permalink
Merge pull request #8 from beef331/master
Browse files Browse the repository at this point in the history
Support 1.6.14
  • Loading branch information
termermc authored Sep 27, 2023
2 parents f6ae2b6 + 59ffb20 commit d080142
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ The generated HTML docs will be available in the `docs` directory in the project

# Nim Version Support

Only Nim `2.0.0`+ is supported because the module takes advantage of various type system improvements introduced in `2.0.0`.
Only Nim `1.6.14`+ is supported as there are bugs with `static int` in prior versions.
27 changes: 20 additions & 7 deletions stack_strings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ template `[]`*(this: StackString, slice: HSlice): openArray[char] =
let part = str[0..4]

doAssert part == ['H', 'e', 'l', 'l', 'o']
doAssert cast[pointer](addr part[0]) == cast[pointer](addr str.data[0])
when NimMajor > 1: ## Nim 2.0 no longer requires `unsafeaddr`
doAssert part[0].addr == addr str.data[0]
else:
doAssert part[0].unsafeaddr == unsafeaddr str.data[0]

when stackStringsPreventAllocation:
{.fatal: "The `[]` template can allocate memory at runtime, see `stackStringsPreventAllocation`".}
Expand Down Expand Up @@ -830,9 +833,14 @@ template toCstring*(this: StackString): cstring =
let cstr = str.toCstring()

doAssert cstr == "Hello world"
doAssert cast[pointer](cstr) == addr str.data[0]

cast[cstring](addr this.data[0])
when NimMajor > 1: ## Nim 2.0 no longer requires `unsafeaddr`
doAssert cstr[0].addr == addr str.data[0]
else:
doAssert cstr[0].unsafeaddr == unsafeaddr str.data[0]
when NimMajor > 1: ## Nim 2.0 no longer requires `unsafeaddr`
cast[cstring](addr this.data[0])
else:
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.
Expand All @@ -843,7 +851,10 @@ proc toHeapCstring*(this: StackString): cstring {.inline.} =
let cstr = str.toHeapCstring()

doAssert cstr == "Hello world"
doAssert cast[pointer](cstr) != addr str.data[0]
when NimMajor > 1: ## Nim 2.0 no longer requires `unsafeaddr`
doAssert cstr[0].addr != addr str.data[0]
else:
doAssert cstr[0].unsafeaddr != unsafeaddr str.data[0]

# You need to deallocate the cstring when you're done with it
dealloc(cstr)
Expand All @@ -855,6 +866,8 @@ 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(result, addr this.data[0], len)
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)
result[len] = '\x00'
4 changes: 2 additions & 2 deletions stack_strings.nimble
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Package

version = "1.1.2"
version = "1.1.3"
author = "termer"
description = "Library for guaranteed zero heap allocation strings"
license = "MIT"


# Dependencies

requires "nim >= 2.0.0"
requires "nim >= 1.6.14"

task docgen, "Generates documentation into the \"docs\" directory":
exec([
Expand Down
Binary file removed tests/t_basic
Binary file not shown.

0 comments on commit d080142

Please sign in to comment.