Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 1.6.14 #8

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.