BString, HashString: Replace string hashes with hashdjb2.

This commit is contained in:
Augustin Cavalier
2024-09-09 13:39:29 -04:00
parent e9254dd79c
commit 67c0b8f2d1
2 changed files with 25 additions and 30 deletions
+15 -18
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2004-2007, Ingo Weinhold, [email protected]. All rights reserved. * Copyright 2004-2007, Ingo Weinhold <[email protected]>. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef HASH_STRING_H #ifndef HASH_STRING_H
@@ -7,29 +7,25 @@
#include <SupportDefs.h> #include <SupportDefs.h>
// string_hash
// static inline uint32
// from the Dragon Book: a slightly modified hashpjw() string_hash(const char *_string)
static inline
uint32
string_hash(const char *name)
{ {
uint32 h = 0; const uint8* string = (const uint8*)_string;
if (name) { if (string == NULL)
for (; *name; name++) { return 0;
uint32 g = h & 0xf0000000;
if (g) uint32 h = 5381;
h ^= g >> 24; char c;
h = (h << 4) + *name; while ((c = *string++) != 0)
} h = (h * 33) + c;
}
return h; return h;
} }
#ifdef __cplusplus
namespace BPrivate { namespace BPrivate {
// HashString // HashString
class HashString { class HashString {
public: public:
@@ -60,10 +56,11 @@ private:
char *fString; char *fString;
}; };
} // namespace BPrivate } // namespace BPrivate
using BPrivate::HashString; using BPrivate::HashString;
#endif // __cplusplus
#endif // HASH_STRING_H #endif // HASH_STRING_H
+10 -12
View File
@@ -227,19 +227,17 @@ BString::CountBytes(int32 fromCharOffset, int32 charCount) const
/*static*/ uint32 /*static*/ uint32
BString::HashValue(const char* string) BString::HashValue(const char* _string)
{ {
// from the Dragon Book: a slightly modified hashpjw() const uint8* string = (const uint8*)_string;
uint32 h = 0; if (string == NULL)
if (string != NULL) { return 0;
for (; *string; string++) {
uint32 g = h & 0xf0000000; uint32 h = 5381;
if (g) char c;
h ^= g >> 24; while ((c = *string++) != 0)
h = (h << 4) + *string; h = (h * 33) + c;
} return h;
}
return h;
} }