From a95b6391fd353074daf2dbfed4d73e8d57f314ca Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 1 Sep 2022 19:10:00 +0300 Subject: 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 --- lib/system/strmantle.nim | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/system') diff --git a/lib/system/strmantle.nim b/lib/system/strmantle.nim index 9cf4f9e55..feaac7817 100644 --- a/lib/system/strmantle.nim +++ b/lib/system/strmantle.nim @@ -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: "", noSideEffect.} -- cgit 1.4.1-2-gfad0