StringBase.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 "Iterator.h"
  26. #include <cstring>
  27. #include <ctype.h>
  28. /// String class
  29. class String
  30. {
  31. public:
  32. typedef RandomAccessIterator<char> Iterator;
  33. typedef RandomAccessConstIterator<char> ConstIterator;
  34. /// Construct empty
  35. String() :
  36. length_(0),
  37. capacity_(0),
  38. buffer_(&endZero)
  39. {
  40. }
  41. /// Construct from another string
  42. String(const String& str) :
  43. length_(0),
  44. capacity_(0),
  45. buffer_(&endZero)
  46. {
  47. *this = str;
  48. }
  49. /// Construct from a C string
  50. String(const char* str) :
  51. length_(0),
  52. capacity_(0),
  53. buffer_(&endZero)
  54. {
  55. *this = str;
  56. }
  57. /// Construct from a char array and length
  58. String(const char* str, unsigned length) :
  59. length_(0),
  60. capacity_(0),
  61. buffer_(&endZero)
  62. {
  63. Resize(length);
  64. CopyChars(buffer_, str, length);
  65. }
  66. /// Destruct
  67. ~String()
  68. {
  69. if (capacity_)
  70. delete[] buffer_;
  71. }
  72. /// Assign a string
  73. String& operator = (const String& rhs)
  74. {
  75. Resize(rhs.length_);
  76. CopyChars(buffer_, rhs.buffer_, rhs.length_);
  77. return *this;
  78. }
  79. /// Assign a C string
  80. String& operator = (const char* rhs)
  81. {
  82. unsigned rhsLength = GetCStringLength(rhs);
  83. Resize(rhsLength);
  84. CopyChars(buffer_, rhs, rhsLength);
  85. return *this;
  86. }
  87. /// Add-assign a string
  88. String& operator += (const String& rhs)
  89. {
  90. if (rhs.length_)
  91. {
  92. unsigned oldLength = length_;
  93. Resize(length_ + rhs.length_);
  94. CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
  95. }
  96. return *this;
  97. }
  98. /// Add-assign a C string
  99. String& operator += (const char* rhs)
  100. {
  101. unsigned rhsLength = GetCStringLength(rhs);
  102. if (rhsLength)
  103. {
  104. unsigned oldLength = length_;
  105. Resize(length_ + rhsLength);
  106. CopyChars(buffer_ + oldLength, rhs, rhsLength);
  107. }
  108. return *this;
  109. }
  110. /// Add-assign a character
  111. String& operator += (char rhs)
  112. {
  113. unsigned oldLength = length_;
  114. Resize(length_ + 1);
  115. buffer_[oldLength] = rhs;
  116. return *this;
  117. }
  118. /// Add a string
  119. String operator + (const String& rhs) const
  120. {
  121. String ret;
  122. ret.Resize(length_ + rhs.length_);
  123. CopyChars(ret.buffer_, buffer_, length_);
  124. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  125. return ret;
  126. }
  127. /// Add a C string
  128. String operator + (const char* rhs) const
  129. {
  130. if (!rhs)
  131. return String(*this);
  132. unsigned rhsLength = strlen(rhs);
  133. if (!rhsLength)
  134. return String(*this);
  135. String ret;
  136. ret.Resize(length_ + rhsLength);
  137. CopyChars(ret.buffer_, buffer_, length_);
  138. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  139. return ret;
  140. }
  141. /// Add a character
  142. String operator + (char rhs) const
  143. {
  144. String ret(*this);
  145. ret += rhs;
  146. return ret;
  147. }
  148. /// Test for equality with another string
  149. bool operator == (const String& rhs) const
  150. {
  151. return strcmp(CString(), rhs.CString()) == 0;
  152. }
  153. /// Test for inequality with another string
  154. bool operator != (const String& rhs) const
  155. {
  156. return strcmp(CString(), rhs.CString()) != 0;
  157. }
  158. /// Test if string is less than another string
  159. bool operator < (const String& rhs) const
  160. {
  161. return strcmp(CString(), rhs.CString()) < 0;
  162. }
  163. /// Test if string is greater than another string
  164. bool operator > (const String& rhs) const
  165. {
  166. return strcmp(CString(), rhs.CString()) > 0;
  167. }
  168. /// Test for equality with a C string
  169. bool operator == (const char* rhs) const
  170. {
  171. return strcmp(CString(), rhs) == 0;
  172. }
  173. /// Test for inequality with a C string
  174. bool operator != (const char* rhs) const
  175. {
  176. return strcmp(CString(), rhs) != 0;
  177. }
  178. /// Test if string is less than a C string
  179. bool operator < (const char* rhs) const
  180. {
  181. return strcmp(CString(), rhs) < 0;
  182. }
  183. /// Test if string is greater than a C string
  184. bool operator > (const char* rhs) const
  185. {
  186. return strcmp(CString(), rhs) > 0;
  187. }
  188. /// Return char at index
  189. char& operator [] (unsigned pos) { return buffer_[pos]; }
  190. /// Return const char at index
  191. const char& operator [] (unsigned pos) const { return buffer_[pos]; }
  192. /// Replace all occurrences of a character
  193. void ReplaceInPlace(char replaceThis, char replaceWith);
  194. /// Replace all occurrences of a string
  195. void ReplaceInPlace(const String& replaceThis, const String& replaceWith);
  196. /// Replace a substring
  197. void ReplaceInPlace(unsigned pos, unsigned length, const String& replaceWith);
  198. /// Replace a substring using iterators
  199. Iterator ReplaceInPlace(const Iterator& start, const Iterator& end, const String& replaceWith);
  200. /// Insert a string
  201. void Insert(unsigned pos, const String& str);
  202. /// Insert a character
  203. void Insert(unsigned pos, char c);
  204. /// Insert a string using an iterator
  205. Iterator Insert(const Iterator& dest, const String& str);
  206. /// Insert a string partially using iterators
  207. Iterator Insert(const Iterator& dest, const Iterator& start, const Iterator& end);
  208. /// Insert a character using an iterator
  209. Iterator Insert(const Iterator& dest, char c);
  210. /// Erase a substring
  211. void Erase(unsigned pos, unsigned length = 1);
  212. /// Erase a character using an iterator
  213. Iterator Erase(const Iterator& it);
  214. /// Erase a substring using iterators
  215. Iterator Erase(const Iterator& start, const Iterator& end);
  216. /// Resize the string
  217. void Resize(unsigned newLength);
  218. /// Set new capacity
  219. void Reserve(unsigned newCapacity);
  220. /// Reallocate so that no extra memory is used
  221. void Compact();
  222. /// Clear the string
  223. void Clear();
  224. /// Swap with another string
  225. void Swap(String& str);
  226. /// Return iterator to the beginning
  227. Iterator Begin() { return Iterator(buffer_); }
  228. /// Return const iterator to the beginning
  229. ConstIterator Begin() const { return ConstIterator(buffer_); }
  230. /// Return iterator to the end
  231. Iterator End() { return Iterator(buffer_ + length_); }
  232. /// Return const iterator to the end
  233. ConstIterator End() const { return ConstIterator(buffer_ + length_); }
  234. /// Return string with all occurrences of a character replaced
  235. String Replace(char replaceThis, char replaceWith) const;
  236. /// Return string with all occurrences of a string replaced
  237. String Replace(const String& replaceThis, const String& replaceWith) const;
  238. /// Return string with a substring replaced
  239. String Replace(unsigned pos, unsigned length, const String& replaceWith) const;
  240. /// Return a substring from position to end
  241. String Substring(unsigned pos) const;
  242. /// Return a substring with length from position
  243. String Substring(unsigned pos, unsigned length) const;
  244. /// Return string with whitespace trimmed from the beginning and the end
  245. String Trim() const;
  246. /// Return string in uppercase
  247. String ToUpper() const;
  248. /// Return string in lowercase
  249. String ToLower() const;
  250. /// Find the first occurrence of a string, or NPOS if not found
  251. unsigned Find(const String& str, unsigned startPos = 0) const;
  252. /// Find the first occurrence of a character, or NPOS if not found
  253. unsigned Find(char c, unsigned startPos = 0) const;
  254. /// Find the last occurrence of a string, or NPOS if not found
  255. unsigned FindLast(const String& str) const;
  256. /// Find the last occurrence of a character, or NPOS if not found
  257. unsigned FindLast(char c) const;
  258. /// Return the C string
  259. const char* CString() const { return buffer_; }
  260. /// Return length
  261. unsigned Length() const { return length_; }
  262. /// Return buffer capacity
  263. unsigned Capacity() const { return capacity_; }
  264. /// Return whether the string is empty
  265. bool Empty() const { return length_ == 0; }
  266. /// Position for "not found"
  267. static const unsigned NPOS = 0xffffffff;
  268. /// Initial dynamic allocation size
  269. static const unsigned MIN_CAPACITY = 8;
  270. private:
  271. /// Move a range of characters within the string
  272. void MoveRange(unsigned dest, unsigned src, unsigned count)
  273. {
  274. if (count)
  275. memmove(buffer_ + dest, buffer_ + src, count);
  276. }
  277. /// Copy chars from one buffer to another
  278. static void CopyChars(char* dest, const char* src, unsigned count)
  279. {
  280. #ifdef _MSC_VER
  281. if (count)
  282. memcpy(dest, src, count);
  283. #else
  284. char* end = dest + count;
  285. while (dest != end)
  286. {
  287. *dest = *src;
  288. ++dest;
  289. ++src;
  290. }
  291. #endif
  292. }
  293. /// Return length of a C string
  294. static unsigned GetCStringLength(const char* str)
  295. {
  296. if (!str)
  297. return 0;
  298. #ifdef _MSC_VER
  299. return strlen(str);
  300. #else
  301. const char* ptr = str;
  302. while (*ptr)
  303. ++ptr;
  304. return ptr - str;
  305. #endif
  306. }
  307. /// Replace a substring with another substring
  308. void ReplaceInPlace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength);
  309. /// String length
  310. unsigned length_;
  311. /// Capacity, zero if buffer not allocated
  312. unsigned capacity_;
  313. /// String buffer, null if not allocated
  314. char* buffer_;
  315. /// End zero for empty strings
  316. static char endZero;
  317. };
  318. /// Add a string to a C string
  319. inline String operator + (const char* lhs, const String& rhs)
  320. {
  321. String ret(lhs);
  322. ret += rhs;
  323. return ret;
  324. }