diff options
author | Ruslan Mustakov <endragor@users.noreply.github.com> | 2017-02-13 19:38:30 +0700 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-02-13 13:38:30 +0100 |
commit | 92665e6e9a74995bd108da5bf3668e2c09c8d79c (patch) | |
tree | 01c9634b0bfc0b996767db769043f97e25001956 /lib/pure | |
parent | 6fa1dba515e514ea9a4ac5850b0188fb6cf95fbf (diff) | |
download | Nim-92665e6e9a74995bd108da5bf3668e2c09c8d79c.tar.gz |
Add hash proc for cstrings (#5386)
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/hashes.nim | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 11af81149..17d1c6442 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -127,6 +127,15 @@ proc hash*(x: string): Hash = h = h !& ord(x[i]) result = !$h +proc hash*(x: cstring): Hash = + ## efficient hashing of null-terminated strings + var h: Hash = 0 + var i = 0 + while x[i] != 0.char: + h = h !& ord(x[i]) + inc i + result = !$h + proc hash*(sBuf: string, sPos, ePos: int): Hash = ## efficient hashing of a string buffer, from starting ## position `sPos` to ending position `ePos` @@ -239,6 +248,7 @@ proc hash*[A](x: set[A]): Hash = when isMainModule: doAssert( hash("aa bb aaaa1234") == hash("aa bb aaaa1234", 0, 13) ) + doAssert( hash("aa bb aaaa1234") == hash(cstring("aa bb aaaa1234")) ) doAssert( hashIgnoreCase("aa bb aaaa1234") == hash("aa bb aaaa1234") ) doAssert( hashIgnoreStyle("aa bb aaaa1234") == hashIgnoreCase("aa bb aaaa1234") ) let xx = @['H','e','l','l','o'] |