support cstring in case (#20130)

* implement case for cstring

for now just converts to string on C backend

* custom implementation for cstring

* remove leftover

* revert even more

* add nil + fix packages weird variant literal bug

* update docs
This commit is contained in:
metagn
2022-09-01 19:10:00 +03:00
committed by GitHub
parent 1f838d9af1
commit a95b6391fd
13 changed files with 126 additions and 18 deletions

View File

@@ -43,6 +43,29 @@ proc hashString(s: string): int {.compilerproc.} =
h = h + h shl 15
result = cast[int](h)
proc eqCstrings(a, b: cstring): bool {.inline, compilerproc.} =
if pointer(a) == pointer(b): result = true
elif a.isNil or b.isNil: result = false
else: result = c_strcmp(a, b) == 0
proc hashCstring(s: cstring): int {.compilerproc.} =
# the compiler needs exactly the same hash function!
# this used to be used for efficient generation of cstring case statements
if s.isNil: return 0
var h : uint = 0
var i = 0
while true:
let c = s[i]
if c == '\0': break
h = h + uint(c)
h = h + h shl 10
h = h xor (h shr 6)
inc i
h = h + h shl 3
h = h xor (h shr 11)
h = h + h shl 15
result = cast[int](h)
proc c_strtod(buf: cstring, endptr: ptr cstring): float64 {.
importc: "strtod", header: "<stdlib.h>", noSideEffect.}