summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2021-01-02 10:13:01 -0600
committerGitHub <noreply@github.com>2021-01-02 17:13:01 +0100
commite869767aa72a71e673c2e8fdc51925f28c13d432 (patch)
tree2f9c27470c67b5d35c6828d8a3df527652b77f59
parentb8775bff575fb6860d43806b8070b904229927bf (diff)
downloadNim-e869767aa72a71e673c2e8fdc51925f28c13d432.tar.gz
fix #16061 (#16551)
-rw-r--r--lib/pure/hashes.nim14
-rw-r--r--tests/stdlib/thashes.nim7
2 files changed, 16 insertions, 5 deletions
diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim
index da72f2fab..dde4be128 100644
--- a/lib/pure/hashes.nim
+++ b/lib/pure/hashes.nim
@@ -297,6 +297,9 @@ proc murmurHash(x: openArray[byte]): Hash =
   h1 = h1 xor (h1 shr 16)
   return cast[Hash](h1)
 
+proc hashVmImpl(x: cstring, sPos, ePos: int): Hash =
+  doAssert false, "implementation override in compiler/vmops.nim"
+
 proc hashVmImpl(x: string, sPos, ePos: int): Hash =
   doAssert false, "implementation override in compiler/vmops.nim"
 
@@ -341,11 +344,14 @@ proc hash*(x: cstring): Hash =
       inc i
     result = !$result
   else:
-    when not defined(js) and defined(nimToOpenArrayCString):
-      murmurHash(toOpenArrayByte(x, 0, x.high))
+    when nimvm:
+      hashVmImpl(x, 0, high(x))
     else:
-      let xx = $x
-      murmurHash(toOpenArrayByte(xx, 0, high(xx)))
+      when not defined(js) and defined(nimToOpenArrayCString):
+        murmurHash(toOpenArrayByte(x, 0, x.high))
+      else:
+        let xx = $x
+        murmurHash(toOpenArrayByte(xx, 0, high(xx)))
 
 proc hash*(sBuf: string, sPos, ePos: int): Hash =
   ## Efficient hashing of a string buffer, from starting
diff --git a/tests/stdlib/thashes.nim b/tests/stdlib/thashes.nim
index b8a2c2c6a..17640387a 100644
--- a/tests/stdlib/thashes.nim
+++ b/tests/stdlib/thashes.nim
@@ -86,8 +86,13 @@ block largeSize: # longer than 4 characters
   doAssert hash(xx, 0, 3) == hash(ssl, 0, 3)
 
 proc main() =
+
+
   doAssert hash(0.0) == hash(0)
-  when sizeof(int) == 8:
+  doAssert hash(cstring"abracadabra") == 97309975
+  doAssert hash(cstring"abracadabra") == hash("abracadabra")
+
+  when sizeof(int) == 8 or defined(js):
     block:
       var s: seq[Hash]
       for a in [0.0, 1.0, -1.0, 1000.0, -1000.0]: