1// String.h 2 3#ifndef STRING_H 4#define STRING_H 5 6#include <string.h> 7 8#include <SupportDefs.h> 9 10 11// string_hash 12// 13// from the Dragon Book: a slightly modified hashpjw() 14static inline 15uint32 16string_hash(const char *name) 17{ 18 uint32 h = 0; 19 if (name) { 20 for (; *name; name++) { 21 uint32 g = h & 0xf0000000; 22 if (g) 23 h ^= g >> 24; 24 h = (h << 4) + *name; 25 } 26 } 27 return h; 28} 29 30#ifdef __cplusplus 31 32namespace UserlandFSUtil { 33 34// String 35class String { 36public: 37 String(); 38 String(const String &string); 39 String(const char *string, int32 length = -1); 40 ~String(); 41 42 bool SetTo(const char *string, int32 maxLength = -1); 43 void Unset(); 44 45 void Truncate(int32 newLength); 46 47 const char *GetString() const; 48 int32 GetLength() const { return fLength; } 49 50 uint32 GetHashCode() const { return string_hash(GetString()); } 51 52 String &operator=(const String &string); 53 bool operator==(const String &string) const; 54 bool operator!=(const String &string) const { return !(*this == string); } 55 56private: 57 bool _SetTo(const char *string, int32 length); 58 59private: 60 int32 fLength; 61 char *fString; 62}; 63 64} // namespace UserlandFSUtil 65 66using UserlandFSUtil::String; 67 68#endif // __cplusplus 69 70#endif // STRING_H 71