From: Aki Tuomi Date: Fri, 28 Jul 2017 06:47:09 +0000 (+0300) Subject: lib: Add strfastcase_hash X-Git-Tag: 2.3.0.rc1~1173 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=00bd7d0e21ffa84965fd6aa0ccbc54aab7fad77c;p=thirdparty%2Fdovecot%2Fcore.git lib: Add strfastcase_hash Fast case-insensitive hashing which works by masking signed bytes with 0x20. --- diff --git a/src/lib/hash.c b/src/lib/hash.c index 2a2db8dfb5..aa9716865e 100644 --- a/src/lib/hash.c +++ b/src/lib/hash.c @@ -565,3 +565,19 @@ unsigned int mem_hash(const void *p, unsigned int size) return h; } +unsigned int strfastcase_hash(const char *p) +{ + const unsigned char *s = (const unsigned char *)p; + unsigned int g, h = 0; + + while (*s != '\0') { + h = (h << 4) + ((*s) & ~0x20); + if ((g = h & 0xf0000000UL) != 0) { + h = h ^ (g >> 24); + h = h ^ g; + } + s++; + } + + return h; +} diff --git a/src/lib/hash.h b/src/lib/hash.h index d51d5cd8a5..53b2ffa89b 100644 --- a/src/lib/hash.h +++ b/src/lib/hash.h @@ -170,6 +170,12 @@ void hash_table_copy(struct hash_table *dest, struct hash_table *src); /* hash function for strings */ unsigned int str_hash(const char *p) ATTR_PURE; unsigned int strcase_hash(const char *p) ATTR_PURE; + +/* fast hash function which uppercases a-z. Does not work well + with input that consists from non number/letter input, as + it works by dropping 0x20. */ +unsigned int strfastcase_hash(const char *p) ATTR_PURE; + /* a generic hash for a given memory block */ unsigned int mem_hash(const void *p, unsigned int size) ATTR_PURE;