Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored and ardek66 committed Mar 26, 2021
1 parent 732fedc commit ed2335e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/pure/hashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/stdlib/thashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit ed2335e

Please sign in to comment.