BString, HashString: Replace string hashes with hashdjb2.

This commit is contained in:
Augustin Cavalier 2024-09-09 12:33:09 -04:00
parent e9254dd79c
commit 67c0b8f2d1
2 changed files with 25 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2004-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
* Copyright 2004-2007, Ingo Weinhold <ingo_weinhold@gmx.de>. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef HASH_STRING_H
@ -7,29 +7,25 @@
#include <SupportDefs.h>
// string_hash
//
// from the Dragon Book: a slightly modified hashpjw()
static inline
uint32
string_hash(const char *name)
static inline uint32
string_hash(const char *_string)
{
uint32 h = 0;
if (name) {
for (; *name; name++) {
uint32 g = h & 0xf0000000;
if (g)
h ^= g >> 24;
h = (h << 4) + *name;
}
}
const uint8* string = (const uint8*)_string;
if (string == NULL)
return 0;
uint32 h = 5381;
char c;
while ((c = *string++) != 0)
h = (h * 33) + c;
return h;
}
#ifdef __cplusplus
namespace BPrivate {
// HashString
class HashString {
public:
@ -60,10 +56,11 @@ private:
char *fString;
};
} // namespace BPrivate
using BPrivate::HashString;
#endif // __cplusplus
#endif // HASH_STRING_H

View File

@ -227,19 +227,17 @@ BString::CountBytes(int32 fromCharOffset, int32 charCount) const
/*static*/ uint32
BString::HashValue(const char* string)
BString::HashValue(const char* _string)
{
// from the Dragon Book: a slightly modified hashpjw()
uint32 h = 0;
if (string != NULL) {
for (; *string; string++) {
uint32 g = h & 0xf0000000;
if (g)
h ^= g >> 24;
h = (h << 4) + *string;
}
}
return h;
const uint8* string = (const uint8*)_string;
if (string == NULL)
return 0;
uint32 h = 5381;
char c;
while ((c = *string++) != 0)
h = (h * 33) + c;
return h;
}