CoreString.h 11 KB

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