Str.pkg 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. $#include "Str.h"
  2. /// %String class.
  3. class String
  4. {
  5. public:
  6. /// Construct empty.
  7. String() :
  8. length_(0),
  9. capacity_(0),
  10. buffer_(&endZero)
  11. {
  12. }
  13. /// Construct from another string.
  14. String(const String& str) :
  15. length_(0),
  16. capacity_(0),
  17. buffer_(&endZero)
  18. {
  19. *this = str;
  20. }
  21. /// Construct from a C string.
  22. String(const char* str) :
  23. length_(0),
  24. capacity_(0),
  25. buffer_(&endZero)
  26. {
  27. *this = str;
  28. }
  29. /// Construct from a C string.
  30. String(char* str) :
  31. length_(0),
  32. capacity_(0),
  33. buffer_(&endZero)
  34. {
  35. *this = (const char*)str;
  36. }
  37. /// Construct from a char array and length.
  38. String(const char* str, unsigned length) :
  39. length_(0),
  40. capacity_(0),
  41. buffer_(&endZero)
  42. {
  43. Resize(length);
  44. CopyChars(buffer_, str, length);
  45. }
  46. /// Construct from an integer.
  47. explicit String(int value);
  48. /// Construct from a short integer.
  49. explicit String(short value);
  50. /// Construct from a long integer.
  51. explicit String(long value);
  52. /// Construct from a long long integer.
  53. explicit String(long long value);
  54. /// Construct from an unsigned integer.
  55. explicit String(unsigned value);
  56. /// Construct from an unsigned short integer.
  57. explicit String(unsigned short value);
  58. /// Construct from an unsigned long integer.
  59. explicit String(unsigned long value);
  60. /// Construct from an unsigned long long integer.
  61. explicit String(unsigned long long value);
  62. /// Construct from a float.
  63. explicit String(float value);
  64. /// Construct from a double.
  65. explicit String(double value);
  66. /// Construct from a bool.
  67. explicit String(bool value);
  68. /// Construct from a character.
  69. explicit String(char value);
  70. /// Construct from a character and fill length.
  71. explicit String(char value, unsigned length);
  72. /// Destruct.
  73. ~String()
  74. {
  75. if (capacity_)
  76. delete[] buffer_;
  77. }
  78. /// Add a string.
  79. String operator + (const String& rhs) const
  80. {
  81. String ret;
  82. ret.Resize(length_ + rhs.length_);
  83. CopyChars(ret.buffer_, buffer_, length_);
  84. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  85. return ret;
  86. }
  87. /// Add a C string.
  88. String operator + (const char* rhs) const
  89. {
  90. unsigned rhsLength = CStringLength(rhs);
  91. String ret;
  92. ret.Resize(length_ + rhsLength);
  93. CopyChars(ret.buffer_, buffer_, length_);
  94. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  95. return ret;
  96. }
  97. /// Add a character.
  98. String operator + (char rhs) const
  99. {
  100. String ret(*this);
  101. ret += rhs;
  102. return ret;
  103. }
  104. /// Test for equality with another string.
  105. bool operator == (const String& rhs) const { return strcmp(CString(), rhs.CString()) == 0; }
  106. tolua_outside bool StringEq @ Eq(const String& rhs) const;
  107. /// Test if string is less than another string.
  108. bool operator < (const String& rhs) const { return strcmp(CString(), rhs.CString()) < 0; }
  109. bool operator == (const char* rhs) const { return strcmp(CString(), rhs) == 0; }
  110. tolua_outside bool StringEq @ Eq(const char* rhs) const;
  111. /// Test if string is less than a C string.
  112. bool operator < (const char* rhs) const { return strcmp(CString(), rhs) < 0; }
  113. /// Return char at index.
  114. char& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  115. /// Return const char at index.
  116. const char& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  117. /// Return char at index.
  118. char& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  119. /// Return const char at index.
  120. const char& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  121. /// Replace all occurrences of a character.
  122. void Replace(char replaceThis, char replaceWith);
  123. /// Replace all occurrences of a string.
  124. void Replace(const String& replaceThis, const String& replaceWith);
  125. /// Replace a substring.
  126. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  127. /// Return a string with all occurrences of a character replaced.
  128. String Replaced(char replaceThis, char replaceWith) const;
  129. /// Return a string with all occurrences of a string replaced.
  130. String Replaced(const String& replaceThis, const String& replaceWith) const;
  131. /// Append a string.
  132. String& Append(const String& str);
  133. /// Append a C string.
  134. String& Append(const char* str);
  135. /// Append a character.
  136. String& Append(char c);
  137. /// Append characters.
  138. String& Append(const char* str, unsigned length);
  139. /// Insert a string.
  140. void Insert(unsigned pos, const String& str);
  141. /// Insert a character.
  142. void Insert(unsigned pos, char c);
  143. /// Erase a substring.
  144. void Erase(unsigned pos, unsigned length = 1);
  145. /// Resize the string.
  146. void Resize(unsigned newLength);
  147. /// Set new capacity.
  148. void Reserve(unsigned newCapacity);
  149. /// Reallocate so that no extra memory is used.
  150. void Compact();
  151. /// Clear the string.
  152. void Clear();
  153. /// Swap with another string.
  154. void Swap(String& str);
  155. /// Return first char, or 0 if empty.
  156. char Front() const { return buffer_[0]; }
  157. /// Return last char, or 0 if empty.
  158. char Back() const { return length_ ? buffer_[length_ - 1] : buffer_[0]; }
  159. /// Return a substring from position to end.
  160. String Substring(unsigned pos) const;
  161. /// Return a substring with length from position.
  162. String Substring(unsigned pos, unsigned length) const;
  163. /// Return string with whitespace trimmed from the beginning and the end.
  164. String Trimmed() const;
  165. /// Return string in uppercase.
  166. String ToUpper() const;
  167. /// Return string in lowercase.
  168. String ToLower() const;
  169. /// Return substrings split by a separator char.
  170. Vector<String> Split(char separator) const;
  171. /// Join substrings with a 'glue' string.
  172. void Join(const Vector<String>& subStrings, String glue);
  173. /// Return index to the first occurrence of a string, or NPOS if not found.
  174. unsigned Find(const String& str, unsigned startPos = 0) const;
  175. /// Return index to the first occurrence of a character, or NPOS if not found.
  176. unsigned Find(char c, unsigned startPos = 0) const;
  177. /// Return index to the last occurrence of a string, or NPOS if not found.
  178. unsigned FindLast(const String& str, unsigned startPos = String::NPOS) const;
  179. /// Return index to the last occurrence of a character, or NPOS if not found.
  180. unsigned FindLast(char c, unsigned startPos = String::NPOS) const;
  181. /// Return whether starts with a string.
  182. bool StartsWith(const String& str) const;
  183. /// Return whether ends with a string.
  184. bool EndsWith(const String& str) const;
  185. /// Return the C string.
  186. const char* CString() const { return buffer_; }
  187. /// Return length.
  188. unsigned Length() const { return length_; }
  189. /// Return buffer capacity.
  190. unsigned Capacity() const { return capacity_; }
  191. /// Return whether the string is empty.
  192. bool Empty() const { return length_ == 0; }
  193. /// Return comparision result with a string.
  194. int Compare(const String& str, bool caseSensitive = true) const;
  195. /// Return comparision result with a C string.
  196. int Compare(const char* str, bool caseSensitive = true) const;
  197. /// Return whether contains a specific occurences of string.
  198. bool Contains(const String& str) const { return Find(str) != String::NPOS; }
  199. /// Return whether contains a specific character.
  200. bool Contains(char c) const { return Find(c) != String::NPOS; }
  201. /// Return hash value for HashSet & HashMap.
  202. unsigned ToHash() const
  203. {
  204. unsigned hash = 0;
  205. const char* ptr = buffer_;
  206. while (*ptr)
  207. {
  208. hash = *ptr + (hash << 6) + (hash << 16) - hash;
  209. ++ptr;
  210. }
  211. return hash;
  212. }
  213. /// Compare two C strings.
  214. static int Compare(const char* str1, const char* str2, bool caseSensitive);
  215. /// Position for "not found."
  216. static const unsigned NPOS;
  217. /// Initial dynamic allocation size.
  218. static const unsigned MIN_CAPACITY;
  219. /// Empty string.
  220. static const String EMPTY;
  221. };
  222. ${
  223. static bool StringEq(const String* lhs, const char* rhs)
  224. {
  225. return (*lhs) == rhs;
  226. }
  227. static bool StringEq(const String* lhs, const String& rhs)
  228. {
  229. return (*lhs) == rhs;
  230. }
  231. $}