1 /* 2 * Copyright 2004-2007, Ingo Weinhold <ingo_weinhold@gmx.de>. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef HASH_STRING_H 6 #define HASH_STRING_H 7 8 #include <SupportDefs.h> 9 10 11 static inline uint32 12 string_hash(const char *_string) 13 { 14 const uint8* string = (const uint8*)_string; 15 if (string == NULL) 16 return 0; 17 18 uint32 h = 5381; 19 char c; 20 while ((c = *string++) != 0) 21 h = (h * 33) + c; 22 return h; 23 } 24 25 26 namespace BPrivate { 27 28 29 // HashString 30 class HashString { 31 public: 32 HashString(); 33 HashString(const HashString &string); 34 HashString(const char *string, int32 length = -1); 35 ~HashString(); 36 37 bool SetTo(const char *string, int32 maxLength = -1); 38 void Unset(); 39 40 void Truncate(int32 newLength); 41 42 const char *GetString() const; 43 int32 GetLength() const { return fLength; } 44 45 uint32 GetHashCode() const { return string_hash(GetString()); } 46 47 HashString &operator=(const HashString &string); 48 bool operator==(const HashString &string) const; 49 bool operator!=(const HashString &string) const { return !(*this == string); } 50 51 private: 52 bool _SetTo(const char *string, int32 length); 53 54 private: 55 int32 fLength; 56 char *fString; 57 }; 58 59 60 } // namespace BPrivate 61 62 63 using BPrivate::HashString; 64 65 66 #endif // HASH_STRING_H 67