diff --git a/headers/private/shared/HashString.h b/headers/private/shared/HashString.h index 26a0ab138f..7ee0d50e6b 100644 --- a/headers/private/shared/HashString.h +++ b/headers/private/shared/HashString.h @@ -1,5 +1,5 @@ /* - * Copyright 2004-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved. + * Copyright 2004-2007, Ingo Weinhold . All rights reserved. * Distributed under the terms of the MIT License. */ #ifndef HASH_STRING_H @@ -7,29 +7,25 @@ #include -// 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 diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index 9d34a0d6e7..b3d07bcb97 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -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; }