StringBase.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. // Note: this file is named StringBase.h to prevent conflicts with C headers
  25. #include "Vector.h"
  26. #include <cstring>
  27. #include <ctype.h>
  28. static const int CONVERSION_BUFFER_LENGTH = 128;
  29. /// %String class.
  30. class String
  31. {
  32. public:
  33. typedef RandomAccessIterator<char> Iterator;
  34. typedef RandomAccessConstIterator<char> ConstIterator;
  35. /// Construct empty.
  36. String() :
  37. length_(0),
  38. capacity_(0),
  39. buffer_(&endZero)
  40. {
  41. }
  42. /// Construct from another string.
  43. String(const String& str) :
  44. length_(0),
  45. capacity_(0),
  46. buffer_(&endZero)
  47. {
  48. *this = str;
  49. }
  50. /// Construct from a C string.
  51. String(const char* str) :
  52. length_(0),
  53. capacity_(0),
  54. buffer_(&endZero)
  55. {
  56. *this = str;
  57. }
  58. /// Construct from a C string.
  59. String(char* str) :
  60. length_(0),
  61. capacity_(0),
  62. buffer_(&endZero)
  63. {
  64. *this = (const char*)str;
  65. }
  66. /// Construct from a char array and length.
  67. String(const char* str, unsigned length) :
  68. length_(0),
  69. capacity_(0),
  70. buffer_(&endZero)
  71. {
  72. Resize(length);
  73. CopyChars(buffer_, str, length);
  74. }
  75. /// Construct from an integer.
  76. explicit String(int value);
  77. /// Construct from a short integer.
  78. explicit String(short value);
  79. /// Construct from an unsigned integer.
  80. explicit String(unsigned value);
  81. /// Construct from an unsigned short integer.
  82. explicit String(unsigned short value);
  83. /// Construct from a float.
  84. explicit String(float value);
  85. /// Construct from a double.
  86. explicit String(double value);
  87. /// Construct from a bool.
  88. explicit String(bool value);
  89. /// Construct from a character.
  90. explicit String(char value);
  91. /// Construct from a character and fill length.
  92. explicit String(char value, unsigned length);
  93. /// Construct from a convertable value.
  94. template <class T> explicit String(const T& value) :
  95. length_(0),
  96. capacity_(0),
  97. buffer_(&endZero)
  98. {
  99. *this = value.ToString();
  100. }
  101. /// Destruct.
  102. ~String()
  103. {
  104. if (capacity_)
  105. delete[] buffer_;
  106. }
  107. /// Assign a string.
  108. String& operator = (const String& rhs)
  109. {
  110. Resize(rhs.length_);
  111. CopyChars(buffer_, rhs.buffer_, rhs.length_);
  112. return *this;
  113. }
  114. /// Assign a C string.
  115. String& operator = (const char* rhs)
  116. {
  117. unsigned rhsLength = GetCStringLength(rhs);
  118. Resize(rhsLength);
  119. CopyChars(buffer_, rhs, rhsLength);
  120. return *this;
  121. }
  122. /// Add-assign a string.
  123. String& operator += (const String& rhs)
  124. {
  125. unsigned oldLength = length_;
  126. Resize(length_ + rhs.length_);
  127. CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
  128. return *this;
  129. }
  130. /// Add-assign a C string.
  131. String& operator += (const char* rhs)
  132. {
  133. unsigned rhsLength = GetCStringLength(rhs);
  134. unsigned oldLength = length_;
  135. Resize(length_ + rhsLength);
  136. CopyChars(buffer_ + oldLength, rhs, rhsLength);
  137. return *this;
  138. }
  139. /// Add-assign a character.
  140. String& operator += (char rhs)
  141. {
  142. unsigned oldLength = length_;
  143. Resize(length_ + 1);
  144. buffer_[oldLength] = rhs;
  145. return *this;
  146. }
  147. /// Add-assign an integer.
  148. String& operator += (int rhs);
  149. /// Add-assign a short integer.
  150. String& operator += (short rhs);
  151. /// Add-assign an unsigned integer.
  152. String& operator += (unsigned rhs);
  153. /// Add-assign a short unsigned integer.
  154. String& operator += (unsigned short rhs);
  155. /// Add-assign a float.
  156. String& operator += (float rhs);
  157. /// Add-assign a bool.
  158. String& operator += (bool rhs);
  159. /// Add-assign an arbitraty type.
  160. template <class T> String operator += (const T& rhs) { return *this += rhs.ToString(); }
  161. /// Add a string.
  162. String operator + (const String& rhs) const
  163. {
  164. String ret;
  165. ret.Resize(length_ + rhs.length_);
  166. CopyChars(ret.buffer_, buffer_, length_);
  167. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  168. return ret;
  169. }
  170. /// Add a C string.
  171. String operator + (const char* rhs) const
  172. {
  173. unsigned rhsLength = GetCStringLength(rhs);
  174. String ret;
  175. ret.Resize(length_ + rhsLength);
  176. CopyChars(ret.buffer_, buffer_, length_);
  177. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  178. return ret;
  179. }
  180. /// Add a character.
  181. String operator + (char rhs) const
  182. {
  183. String ret(*this);
  184. ret += rhs;
  185. return ret;
  186. }
  187. /// Test for equality with another string.
  188. bool operator == (const String& rhs) const { return strcmp(CString(), rhs.CString()) == 0; }
  189. /// Test for inequality with another string.
  190. bool operator != (const String& rhs) const { return strcmp(CString(), rhs.CString()) != 0; }
  191. /// Test if string is less than another string.
  192. bool operator < (const String& rhs) const { return strcmp(CString(), rhs.CString()) < 0; }
  193. /// Test if string is greater than another string.
  194. bool operator > (const String& rhs) const { return strcmp(CString(), rhs.CString()) > 0; }
  195. /// Test for equality with a C string.
  196. bool operator == (const char* rhs) const { return strcmp(CString(), rhs) == 0; }
  197. /// Test for inequality with a C string.
  198. bool operator != (const char* rhs) const { return strcmp(CString(), rhs) != 0; }
  199. /// Test if string is less than a C string.
  200. bool operator < (const char* rhs) const { return strcmp(CString(), rhs) < 0; }
  201. /// Test if string is greater than a C string.
  202. bool operator > (const char* rhs) const { return strcmp(CString(), rhs) > 0; }
  203. /// Return char at index.
  204. char& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  205. /// Return const char at index.
  206. const char& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  207. /// Return char at index.
  208. char& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  209. /// Return const char at index.
  210. const char& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  211. /// Replace all occurrences of a character.
  212. void Replace(char replaceThis, char replaceWith);
  213. /// Replace all occurrences of a string.
  214. void Replace(const String& replaceThis, const String& replaceWith);
  215. /// Replace a substring.
  216. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  217. /// Replace a substring by iterators.
  218. Iterator Replace(const Iterator& start, const Iterator& end, const String& replaceWith);
  219. /// Return a string with all occurrences of a character replaced.
  220. String Replaced(char replaceThis, char replaceWith) const;
  221. /// Return a string with all occurrences of a string replaced.
  222. String Replaced(const String& replaceThis, const String& replaceWith) const;
  223. /// Append a string.
  224. void Append(const String& str);
  225. /// Append a C string.
  226. void Append(const char* str);
  227. /// Append a character.
  228. void Append(char c);
  229. /// Append characters.
  230. void Append(const char* str, unsigned length);
  231. /// Insert a string.
  232. void Insert(unsigned pos, const String& str);
  233. /// Insert a character.
  234. void Insert(unsigned pos, char c);
  235. /// Insert a string using an iterator.
  236. Iterator Insert(const Iterator& dest, const String& str);
  237. /// Insert a string partially by iterators.
  238. Iterator Insert(const Iterator& dest, const Iterator& start, const Iterator& end);
  239. /// Insert a character using an iterator.
  240. Iterator Insert(const Iterator& dest, char c);
  241. /// Erase a substring.
  242. void Erase(unsigned pos, unsigned length = 1);
  243. /// Erase a character by iterator.
  244. Iterator Erase(const Iterator& it);
  245. /// Erase a substring by iterators.
  246. Iterator Erase(const Iterator& start, const Iterator& end);
  247. /// Resize the string.
  248. void Resize(unsigned newLength);
  249. /// %Set new capacity.
  250. void Reserve(unsigned newCapacity);
  251. /// Reallocate so that no extra memory is used.
  252. void Compact();
  253. /// Clear the string.
  254. void Clear();
  255. /// Swap with another string.
  256. void Swap(String& str);
  257. /// Return iterator to the beginning.
  258. Iterator Begin() { return Iterator(buffer_); }
  259. /// Return const iterator to the beginning.
  260. ConstIterator Begin() const { return ConstIterator(buffer_); }
  261. /// Return iterator to the end.
  262. Iterator End() { return Iterator(buffer_ + length_); }
  263. /// Return const iterator to the end.
  264. ConstIterator End() const { return ConstIterator(buffer_ + length_); }
  265. /// Return first char, or 0 if empty.
  266. char Front() const { return buffer_[0]; }
  267. /// Return last char, or 0 if empty.
  268. char Back() const { return length_ ? buffer_[length_ - 1] : buffer_[0]; }
  269. /// Return a substring from position to end.
  270. String Substring(unsigned pos) const;
  271. /// Return a substring with length from position.
  272. String Substring(unsigned pos, unsigned length) const;
  273. /// Return string with whitespace trimmed from the beginning and the end.
  274. String Trimmed() const;
  275. /// Return string in uppercase.
  276. String ToUpper() const;
  277. /// Return string in lowercase.
  278. String ToLower() const;
  279. /// Return substrings split by a separator char.
  280. Vector<String> Split(char separator) const;
  281. /// Return index to the first occurrence of a string, or NPOS if not found.
  282. unsigned Find(const String& str, unsigned startPos = 0) const;
  283. /// Return index to the first occurrence of a character, or NPOS if not found.
  284. unsigned Find(char c, unsigned startPos = 0) const;
  285. /// Return index to the last occurrence of a string, or NPOS if not found.
  286. unsigned FindLast(const String& str, unsigned startPos = NPOS) const;
  287. /// Return index to the last occurrence of a character, or NPOS if not found.
  288. unsigned FindLast(char c, unsigned startPos = NPOS) const;
  289. /// Return whether starts with a string.
  290. bool StartsWith(const String& str) const;
  291. /// Return whether ends with a string.
  292. bool EndsWith(const String& str) const;
  293. /// Return the C string.
  294. const char* CString() const { return buffer_; }
  295. /// Return length.
  296. unsigned Length() const { return length_; }
  297. /// Return buffer capacity.
  298. unsigned Capacity() const { return capacity_; }
  299. /// Return whether the string is empty.
  300. bool Empty() const { return length_ == 0; }
  301. /// Return comparision result with a string.
  302. int Compare(const String& str, bool caseSensitive = true) const;
  303. /// Return comparision result with a C string.
  304. int Compare(const char* str, bool caseSensitive = true) const;
  305. /// Return hash value for HashSet & HashMap.
  306. unsigned ToHash() const
  307. {
  308. unsigned hash = 0;
  309. const char* ptr = buffer_;
  310. while (*ptr)
  311. {
  312. hash = *ptr + (hash << 6) + (hash << 16) - hash;
  313. ++ptr;
  314. }
  315. return hash;
  316. }
  317. /// Position for "not found."
  318. static const unsigned NPOS = 0xffffffff;
  319. /// Initial dynamic allocation size.
  320. static const unsigned MIN_CAPACITY = 8;
  321. private:
  322. /// Move a range of characters within the string.
  323. void MoveRange(unsigned dest, unsigned src, unsigned count)
  324. {
  325. if (count)
  326. memmove(buffer_ + dest, buffer_ + src, count);
  327. }
  328. /// Copy chars from one buffer to another.
  329. static void CopyChars(char* dest, const char* src, unsigned count)
  330. {
  331. #ifdef _MSC_VER
  332. if (count)
  333. memcpy(dest, src, count);
  334. #else
  335. char* end = dest + count;
  336. while (dest != end)
  337. {
  338. *dest = *src;
  339. ++dest;
  340. ++src;
  341. }
  342. #endif
  343. }
  344. /// Return length of a C string.
  345. static unsigned GetCStringLength(const char* str)
  346. {
  347. if (!str)
  348. return 0;
  349. #ifdef _MSC_VER
  350. return strlen(str);
  351. #else
  352. const char* ptr = str;
  353. while (*ptr)
  354. ++ptr;
  355. return ptr - str;
  356. #endif
  357. }
  358. /// Replace a substring with another substring.
  359. void Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength);
  360. /// String length.
  361. unsigned length_;
  362. /// Capacity, zero if buffer not allocated.
  363. unsigned capacity_;
  364. /// String buffer, null if not allocated.
  365. char* buffer_;
  366. /// End zero for empty strings.
  367. static char endZero;
  368. };
  369. /// Add a string to a C string.
  370. inline String operator + (const char* lhs, const String& rhs)
  371. {
  372. String ret(lhs);
  373. ret += rhs;
  374. return ret;
  375. }