1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef STRING_H 6 #define STRING_H 7 8 9 #include "StringPool.h" 10 11 12 class String { 13 public: 14 String(); 15 String(const String& other); 16 ~String(); 17 18 bool SetTo(const char* string); 19 bool SetTo(const char* string, size_t maxLength); 20 bool SetToExactLength(const char* string, 21 size_t length); 22 23 const char* Data() const; 24 uint32 Hash() const; 25 26 bool IsEmpty() const; 27 28 String& operator=(const String& other); 29 30 bool operator==(const String& other) const; 31 bool operator!=(const String& other) const; 32 33 operator const char*() const; 34 35 private: 36 StringData* fData; 37 }; 38 39 40 inline 41 String::String() 42 : 43 fData(StringData::GetEmpty()) 44 { 45 } 46 47 48 inline 49 String::String(const String& other) 50 : 51 fData(other.fData) 52 { 53 fData->AcquireReference(); 54 } 55 56 57 inline 58 String::~String() 59 { 60 fData->ReleaseReference(); 61 } 62 63 64 inline bool 65 String::SetTo(const char* string) 66 { 67 return SetToExactLength(string, (string == NULL) ? 0 : strlen(string)); 68 } 69 70 71 inline bool 72 String::SetTo(const char* string, size_t maxLength) 73 { 74 return SetToExactLength(string, strnlen(string, maxLength)); 75 } 76 77 78 inline const char* 79 String::Data() const 80 { 81 return fData->String(); 82 } 83 84 85 inline uint32 86 String::Hash() const 87 { 88 return fData->Hash(); 89 } 90 91 92 inline bool 93 String::IsEmpty() const 94 { 95 return fData == StringData::Empty(); 96 } 97 98 99 inline bool 100 String::operator==(const String& other) const 101 { 102 return fData == other.fData; 103 } 104 105 106 inline bool 107 String::operator!=(const String& other) const 108 { 109 return !(*this == other); 110 } 111 112 113 inline 114 String::operator const char*() const 115 { 116 return fData->String(); 117 } 118 119 120 #endif // STRING_H 121