ref: d3cad93205eb0f2e2dbbead396c2a976face45ba
parent: bb00e7717dfd6cf6cb66b9866dae3ea750216177
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Oct 14 06:02:39 EDT 2015
Add case insensitive hash functions. Useful for case insensitive protocols.
--- a/lib/std/hashfuncs.myr
+++ b/lib/std/hashfuncs.myr
@@ -1,11 +1,17 @@
+use "alloc.use"
use "die.use"
use "sleq.use"
+use "slpush.use"
use "types.use"
+use "utf.use"
pkg std =
const strhash : (s : byte[:] -> uint32)
const streq : (a : byte[:], b : byte[:] -> bool)
+ const strcasehash : (s : byte[:] -> uint32)
+ const strcaseeq : (a : byte[:], b : byte[:] -> bool)
+
generic ptrhash : (p : @a# -> uint32)
generic ptreq : (a : @a#, b : @a# -> bool)
@@ -31,9 +37,38 @@
-> (data castto(byte#))[:n]
}
-/* Supremely simple djb hash. */
const strhash = {s
-> murmurhash2(s, Seed)
+}
+
+const strcaseeq = {a, b
+ var ca, cb
+
+ while true
+ if a.len == 0 || b.len == 0
+ break
+ ;;
+ (ca, a) = std.striter(a)
+ (cb, b) = std.striter(b)
+ if ca != cb
+ -> false
+ ;;
+ ;;
+ -> a.len == b.len
+}
+
+const strcasehash = {s
+ var chars
+ var c, h
+
+ while s.len != 0
+ (c, s) = std.striter(s)
+ chars = std.slpush(chars, c)
+ ;;
+ h = murmurhash2(slbytes(chars), Seed)
+ slfree(chars)
+ -> h
+
}
const streq = {a, b