summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/hashes.nim10
-rw-r--r--web/news/e031_version_0_16_2.rst6
2 files changed, 15 insertions, 1 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']
diff --git a/web/news/e031_version_0_16_2.rst b/web/news/e031_version_0_16_2.rst
index 5ee40b772..2d725028e 100644
--- a/web/news/e031_version_0_16_2.rst
+++ b/web/news/e031_version_0_16_2.rst
@@ -18,7 +18,11 @@ Changes affecting backwards compatibility
 - The IO routines now raise ``EOFError`` for the "end of file" condition.
   ``EOFError`` is a subtype of ``IOError`` and so it's easier to distinguish
   between "error during read" and "error due to EOF".
-
+- A hash procedure has been added for ``cstring`` type in ``hashes`` module.
+  Previously, hash of a ``cstring`` would be calculated as a hash of the
+  pointer. Now the hash is calculated from the contents of the string, assuming
+  ``cstring`` is a null-terminated string. Equal ``string``s and ``cstring``s
+  produce equal hash value.
 
 Library Additions
 -----------------