Add length aware variant(s) of string hashing

- Being able to hash partial strings is needed for allowing string pool
  to operate on partial strings...
This commit is contained in:
Panu Matilainen 2012-09-18 04:46:10 +03:00
parent 76a699701c
commit 0927ab855e
1 changed files with 27 additions and 0 deletions

View File

@ -61,6 +61,33 @@ static inline unsigned int rstrlenhash(const char * str, size_t * len)
return hash;
}
static inline unsigned int rstrnlenhash(const char * str, size_t n, size_t * len)
{
/* Jenkins One-at-a-time hash */
unsigned int hash = 0xe4721b68;
const char * s = str;
while (*s != '\0' && n-- > 0) {
hash += *s;
hash += (hash << 10);
hash ^= (hash >> 6);
s++;
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
if (len)
*len = (s - str);
return hash;
}
static inline unsigned int rstrnhash(const char * string, size_t n)
{
return rstrnlenhash(string, n, NULL);
}
unsigned int rstrhash(const char * string)
{
return rstrlenhash(string, NULL);