mirror of
https://review.haiku-os.org/haiku
synced 2024-11-23 07:18:40 +01:00
BString, HashString: Replace string hashes with hashdjb2.
This commit is contained in:
parent
e9254dd79c
commit
67c0b8f2d1
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user