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:
parent
76a699701c
commit
0927ab855e
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue