Str.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Vector.h"
  25. #include <cstring>
  26. #include <ctype.h>
  27. static const int CONVERSION_BUFFER_LENGTH = 128;
  28. class WString;
  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 a null-terminated wide character array.
  76. String(const wchar_t* str) :
  77. length_(0),
  78. capacity_(0),
  79. buffer_(&endZero)
  80. {
  81. SetUTF8FromWChar(str);
  82. }
  83. /// Construct from a wide character string.
  84. String(const WString& str);
  85. /// Construct from an integer.
  86. explicit String(int value);
  87. /// Construct from a short integer.
  88. explicit String(short value);
  89. /// Construct from an unsigned integer.
  90. explicit String(unsigned value);
  91. /// Construct from an unsigned short integer.
  92. explicit String(unsigned short value);
  93. /// Construct from a float.
  94. explicit String(float value);
  95. /// Construct from a double.
  96. explicit String(double value);
  97. /// Construct from a bool.
  98. explicit String(bool value);
  99. /// Construct from a character.
  100. explicit String(char value);
  101. /// Construct from a character and fill length.
  102. explicit String(char value, unsigned length);
  103. /// Construct from a convertable value.
  104. template <class T> explicit String(const T& value) :
  105. length_(0),
  106. capacity_(0),
  107. buffer_(&endZero)
  108. {
  109. *this = value.ToString();
  110. }
  111. /// Destruct.
  112. ~String()
  113. {
  114. if (capacity_)
  115. delete[] buffer_;
  116. }
  117. /// Assign a string.
  118. String& operator = (const String& rhs)
  119. {
  120. Resize(rhs.length_);
  121. CopyChars(buffer_, rhs.buffer_, rhs.length_);
  122. return *this;
  123. }
  124. /// Assign a C string.
  125. String& operator = (const char* rhs)
  126. {
  127. unsigned rhsLength = CStringLength(rhs);
  128. Resize(rhsLength);
  129. CopyChars(buffer_, rhs, rhsLength);
  130. return *this;
  131. }
  132. /// Add-assign a string.
  133. String& operator += (const String& rhs)
  134. {
  135. unsigned oldLength = length_;
  136. Resize(length_ + rhs.length_);
  137. CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
  138. return *this;
  139. }
  140. /// Add-assign a C string.
  141. String& operator += (const char* rhs)
  142. {
  143. unsigned rhsLength = CStringLength(rhs);
  144. unsigned oldLength = length_;
  145. Resize(length_ + rhsLength);
  146. CopyChars(buffer_ + oldLength, rhs, rhsLength);
  147. return *this;
  148. }
  149. /// Add-assign a character.
  150. String& operator += (char rhs)
  151. {
  152. unsigned oldLength = length_;
  153. Resize(length_ + 1);
  154. buffer_[oldLength] = rhs;
  155. return *this;
  156. }
  157. /// Add-assign an integer.
  158. String& operator += (int rhs);
  159. /// Add-assign a short integer.
  160. String& operator += (short rhs);
  161. /// Add-assign an unsigned integer.
  162. String& operator += (unsigned rhs);
  163. /// Add-assign a short unsigned integer.
  164. String& operator += (unsigned short rhs);
  165. /// Add-assign a float.
  166. String& operator += (float rhs);
  167. /// Add-assign a bool.
  168. String& operator += (bool rhs);
  169. /// Add-assign an arbitraty type.
  170. template <class T> String operator += (const T& rhs) { return *this += rhs.ToString(); }
  171. /// Add a string.
  172. String operator + (const String& rhs) const
  173. {
  174. String ret;
  175. ret.Resize(length_ + rhs.length_);
  176. CopyChars(ret.buffer_, buffer_, length_);
  177. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  178. return ret;
  179. }
  180. /// Add a C string.
  181. String operator + (const char* rhs) const
  182. {
  183. unsigned rhsLength = CStringLength(rhs);
  184. String ret;
  185. ret.Resize(length_ + rhsLength);
  186. CopyChars(ret.buffer_, buffer_, length_);
  187. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  188. return ret;
  189. }
  190. /// Add a character.
  191. String operator + (char rhs) const
  192. {
  193. String ret(*this);
  194. ret += rhs;
  195. return ret;
  196. }
  197. /// Test for equality with another string.
  198. bool operator == (const String& rhs) const { return strcmp(CString(), rhs.CString()) == 0; }
  199. /// Test for inequality with another string.
  200. bool operator != (const String& rhs) const { return strcmp(CString(), rhs.CString()) != 0; }
  201. /// Test if string is less than another string.
  202. bool operator < (const String& rhs) const { return strcmp(CString(), rhs.CString()) < 0; }
  203. /// Test if string is greater than another string.
  204. bool operator > (const String& rhs) const { return strcmp(CString(), rhs.CString()) > 0; }
  205. /// Test for equality with a C string.
  206. bool operator == (const char* rhs) const { return strcmp(CString(), rhs) == 0; }
  207. /// Test for inequality with a C string.
  208. bool operator != (const char* rhs) const { return strcmp(CString(), rhs) != 0; }
  209. /// Test if string is less than a C string.
  210. bool operator < (const char* rhs) const { return strcmp(CString(), rhs) < 0; }
  211. /// Test if string is greater than a C string.
  212. bool operator > (const char* rhs) const { return strcmp(CString(), rhs) > 0; }
  213. /// Return char at index.
  214. char& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  215. /// Return const char at index.
  216. const char& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  217. /// Return char at index.
  218. char& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  219. /// Return const char at index.
  220. const char& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  221. /// Replace all occurrences of a character.
  222. void Replace(char replaceThis, char replaceWith);
  223. /// Replace all occurrences of a string.
  224. void Replace(const String& replaceThis, const String& replaceWith);
  225. /// Replace a substring.
  226. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  227. /// Replace a substring by iterators.
  228. Iterator Replace(const Iterator& start, const Iterator& end, const String& replaceWith);
  229. /// Return a string with all occurrences of a character replaced.
  230. String Replaced(char replaceThis, char replaceWith) const;
  231. /// Return a string with all occurrences of a string replaced.
  232. String Replaced(const String& replaceThis, const String& replaceWith) const;
  233. /// Append a string.
  234. void Append(const String& str);
  235. /// Append a C string.
  236. void Append(const char* str);
  237. /// Append a character.
  238. void Append(char c);
  239. /// Append characters.
  240. void Append(const char* str, unsigned length);
  241. /// Insert a string.
  242. void Insert(unsigned pos, const String& str);
  243. /// Insert a character.
  244. void Insert(unsigned pos, char c);
  245. /// Insert a string using an iterator.
  246. Iterator Insert(const Iterator& dest, const String& str);
  247. /// Insert a string partially by iterators.
  248. Iterator Insert(const Iterator& dest, const Iterator& start, const Iterator& end);
  249. /// Insert a character using an iterator.
  250. Iterator Insert(const Iterator& dest, char c);
  251. /// Erase a substring.
  252. void Erase(unsigned pos, unsigned length = 1);
  253. /// Erase a character by iterator.
  254. Iterator Erase(const Iterator& it);
  255. /// Erase a substring by iterators.
  256. Iterator Erase(const Iterator& start, const Iterator& end);
  257. /// Resize the string.
  258. void Resize(unsigned newLength);
  259. /// %Set new capacity.
  260. void Reserve(unsigned newCapacity);
  261. /// Reallocate so that no extra memory is used.
  262. void Compact();
  263. /// Clear the string.
  264. void Clear();
  265. /// Swap with another string.
  266. void Swap(String& str);
  267. /// Return iterator to the beginning.
  268. Iterator Begin() { return Iterator(buffer_); }
  269. /// Return const iterator to the beginning.
  270. ConstIterator Begin() const { return ConstIterator(buffer_); }
  271. /// Return iterator to the end.
  272. Iterator End() { return Iterator(buffer_ + length_); }
  273. /// Return const iterator to the end.
  274. ConstIterator End() const { return ConstIterator(buffer_ + length_); }
  275. /// Return first char, or 0 if empty.
  276. char Front() const { return buffer_[0]; }
  277. /// Return last char, or 0 if empty.
  278. char Back() const { return length_ ? buffer_[length_ - 1] : buffer_[0]; }
  279. /// Return a substring from position to end.
  280. String Substring(unsigned pos) const;
  281. /// Return a substring with length from position.
  282. String Substring(unsigned pos, unsigned length) const;
  283. /// Return string with whitespace trimmed from the beginning and the end.
  284. String Trimmed() const;
  285. /// Return string in uppercase.
  286. String ToUpper() const;
  287. /// Return string in lowercase.
  288. String ToLower() const;
  289. /// Return substrings split by a separator char.
  290. Vector<String> Split(char separator) const;
  291. /// Return index to the first occurrence of a string, or NPOS if not found.
  292. unsigned Find(const String& str, unsigned startPos = 0) const;
  293. /// Return index to the first occurrence of a character, or NPOS if not found.
  294. unsigned Find(char c, unsigned startPos = 0) const;
  295. /// Return index to the last occurrence of a string, or NPOS if not found.
  296. unsigned FindLast(const String& str, unsigned startPos = NPOS) const;
  297. /// Return index to the last occurrence of a character, or NPOS if not found.
  298. unsigned FindLast(char c, unsigned startPos = NPOS) const;
  299. /// Return whether starts with a string.
  300. bool StartsWith(const String& str) const;
  301. /// Return whether ends with a string.
  302. bool EndsWith(const String& str) const;
  303. /// Return the C string.
  304. const char* CString() const { return buffer_; }
  305. /// Return length.
  306. unsigned Length() const { return length_; }
  307. /// Return buffer capacity.
  308. unsigned Capacity() const { return capacity_; }
  309. /// Return whether the string is empty.
  310. bool Empty() const { return length_ == 0; }
  311. /// Return comparision result with a string.
  312. int Compare(const String& str, bool caseSensitive = true) const;
  313. /// Return comparision result with a C string.
  314. int Compare(const char* str, bool caseSensitive = true) const;
  315. /// Construct UTF8 content from Latin1.
  316. void SetUTF8FromLatin1(const char* str);
  317. /// Construct UTF8 content from wide characters.
  318. void SetUTF8FromWChar(const wchar_t* str);
  319. /// Calculate number of characters in UTF8 content.
  320. unsigned LengthUTF8() const;
  321. /// Return byte offset to char in UTF8 content.
  322. unsigned ByteOffsetUTF8(unsigned index) const;
  323. /// Return next Unicode character from UTF8 content and increase byte offset.
  324. unsigned NextUTF8Char(unsigned& byteOffset) const;
  325. /// Return Unicode character at index from UTF8 content.
  326. unsigned AtUTF8(unsigned index) const;
  327. /// Replace Unicode character at index from UTF8 content.
  328. void ReplaceUTF8(unsigned index, unsigned unicodeChar);
  329. /// Append Unicode character at the end as UTF8.
  330. void AppendUTF8(unsigned unicodeChar);
  331. /// Return a UTF8 substring from position to end.
  332. String SubstringUTF8(unsigned pos) const;
  333. /// Return a UTF8 substring with length from position.
  334. String SubstringUTF8(unsigned pos, unsigned length) const;
  335. /// Return hash value for HashSet & HashMap.
  336. unsigned ToHash() const
  337. {
  338. unsigned hash = 0;
  339. const char* ptr = buffer_;
  340. while (*ptr)
  341. {
  342. hash = *ptr + (hash << 6) + (hash << 16) - hash;
  343. ++ptr;
  344. }
  345. return hash;
  346. }
  347. /// Return substrings split by a separator char.
  348. static Vector<String> Split(const char* str, char separator);
  349. /// Encode Unicode character to UTF8. Pointer will be incremented.
  350. static void EncodeUTF8(char*& dest, unsigned unicodeChar);
  351. /// Decode Unicode character from UTF8. Pointer will be incremented.
  352. static unsigned DecodeUTF8(const char*& src);
  353. #ifdef WIN32
  354. /// Encode Unicode character to UTF16. Pointer will be incremented.
  355. static void EncodeUTF16(wchar_t*& dest, unsigned unicodeChar);
  356. /// Decode Unicode character from UTF16. Pointer will be incremented.
  357. static unsigned DecodeUTF16(const wchar_t*& src);
  358. #endif
  359. /// Return length of a C string.
  360. static unsigned CStringLength(const char* str)
  361. {
  362. if (!str)
  363. return 0;
  364. #ifdef _MSC_VER
  365. return strlen(str);
  366. #else
  367. const char* ptr = str;
  368. while (*ptr)
  369. ++ptr;
  370. return ptr - str;
  371. #endif
  372. }
  373. /// Position for "not found."
  374. static const unsigned NPOS = 0xffffffff;
  375. /// Initial dynamic allocation size.
  376. static const unsigned MIN_CAPACITY = 8;
  377. private:
  378. /// Move a range of characters within the string.
  379. void MoveRange(unsigned dest, unsigned src, unsigned count)
  380. {
  381. if (count)
  382. memmove(buffer_ + dest, buffer_ + src, count);
  383. }
  384. /// Copy chars from one buffer to another.
  385. static void CopyChars(char* dest, const char* src, unsigned count)
  386. {
  387. #ifdef _MSC_VER
  388. if (count)
  389. memcpy(dest, src, count);
  390. #else
  391. char* end = dest + count;
  392. while (dest != end)
  393. {
  394. *dest = *src;
  395. ++dest;
  396. ++src;
  397. }
  398. #endif
  399. }
  400. /// Replace a substring with another substring.
  401. void Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength);
  402. /// String length.
  403. unsigned length_;
  404. /// Capacity, zero if buffer not allocated.
  405. unsigned capacity_;
  406. /// String buffer, null if not allocated.
  407. char* buffer_;
  408. /// End zero for empty strings.
  409. static char endZero;
  410. };
  411. /// Add a string to a C string.
  412. inline String operator + (const char* lhs, const String& rhs)
  413. {
  414. String ret(lhs);
  415. ret += rhs;
  416. return ret;
  417. }
  418. /// Wide character string. Only meant for converting from String and passing to the operating system where necessary.
  419. class WString
  420. {
  421. public:
  422. /// Construct empty.
  423. WString();
  424. /// Construct from a string.
  425. WString(const String& str);
  426. /// Destruct.
  427. ~WString();
  428. /// Return char at index.
  429. wchar_t& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  430. /// Return const char at index.
  431. const wchar_t& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  432. /// Return char at index.
  433. wchar_t& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  434. /// Return const char at index.
  435. const wchar_t& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  436. /// Resize the string.
  437. void Resize(unsigned newSize);
  438. /// Return whether the string is empty.
  439. bool Empty() const { return length_ == 0; }
  440. /// Return length.
  441. unsigned Length() const { return length_; }
  442. /// Return character data.
  443. const wchar_t* CString() const { return buffer_; }
  444. private:
  445. /// String length.
  446. unsigned length_;
  447. /// String buffer, null if not allocated.
  448. wchar_t* buffer_;
  449. };