From 5a54e156e5b96ef240ce432a09f18dea364dcd47 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 16 Jul 2011 13:51:50 +0200 Subject: [PATCH] Add HashValue() methods to BString --- headers/os/support/String.h | 10 ++++++++++ src/kits/support/String.cpp | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/headers/os/support/String.h b/headers/os/support/String.h index 47260f1c5a..f3d5ae61d7 100644 --- a/headers/os/support/String.h +++ b/headers/os/support/String.h @@ -30,6 +30,9 @@ public: int32 charCount) const; bool IsEmpty() const; + uint32 HashValue() const; + static uint32 HashValue(const char* string); + // Assignment BString& operator=(const BString& string); BString& operator=(const char* string); @@ -416,6 +419,13 @@ BString::String() const } +inline uint32 +BString::HashValue() const +{ + return HashValue(String()); +} + + inline BString & BString::SetTo(const char* string) { diff --git a/src/kits/support/String.cpp b/src/kits/support/String.cpp index e1be1c00a4..e153e7ef47 100644 --- a/src/kits/support/String.cpp +++ b/src/kits/support/String.cpp @@ -282,6 +282,23 @@ BString::CountBytes(int32 fromCharOffset, int32 charCount) const } +/*static*/ uint32 +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; +} + + // #pragma mark - Assignment