Str.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Vector.h"
  24. #include <cstring>
  25. #include <cstdarg>
  26. #include <cctype>
  27. namespace Urho3D
  28. {
  29. static const int CONVERSION_BUFFER_LENGTH = 128;
  30. class WString;
  31. /// %String class.
  32. class URHO3D_API String
  33. {
  34. public:
  35. typedef RandomAccessIterator<char> Iterator;
  36. typedef RandomAccessConstIterator<char> ConstIterator;
  37. /// Construct empty.
  38. String() :
  39. length_(0),
  40. capacity_(0),
  41. buffer_(&endZero)
  42. {
  43. }
  44. /// Construct from another string.
  45. String(const String& str) :
  46. length_(0),
  47. capacity_(0),
  48. buffer_(&endZero)
  49. {
  50. *this = str;
  51. }
  52. /// Construct from a C string.
  53. String(const char* str) :
  54. length_(0),
  55. capacity_(0),
  56. buffer_(&endZero)
  57. {
  58. *this = str;
  59. }
  60. /// Construct from a C string.
  61. String(char* str) :
  62. length_(0),
  63. capacity_(0),
  64. buffer_(&endZero)
  65. {
  66. *this = (const char*)str;
  67. }
  68. /// Construct from a char array and length.
  69. String(const char* str, unsigned length) :
  70. length_(0),
  71. capacity_(0),
  72. buffer_(&endZero)
  73. {
  74. Resize(length);
  75. CopyChars(buffer_, str, length);
  76. }
  77. /// Construct from a null-terminated wide character array.
  78. String(const wchar_t* str) :
  79. length_(0),
  80. capacity_(0),
  81. buffer_(&endZero)
  82. {
  83. SetUTF8FromWChar(str);
  84. }
  85. /// Construct from a null-terminated wide character array.
  86. String(wchar_t* str) :
  87. length_(0),
  88. capacity_(0),
  89. buffer_(&endZero)
  90. {
  91. SetUTF8FromWChar(str);
  92. }
  93. /// Construct from a wide character string.
  94. String(const WString& str);
  95. /// Construct from an integer.
  96. explicit String(int value);
  97. /// Construct from a short integer.
  98. explicit String(short value);
  99. /// Construct from a long integer.
  100. explicit String(long value);
  101. /// Construct from a long long integer.
  102. explicit String(long long value);
  103. /// Construct from an unsigned integer.
  104. explicit String(unsigned value);
  105. /// Construct from an unsigned short integer.
  106. explicit String(unsigned short value);
  107. /// Construct from an unsigned long integer.
  108. explicit String(unsigned long value);
  109. /// Construct from an unsigned long long integer.
  110. explicit String(unsigned long long value);
  111. /// Construct from a float.
  112. explicit String(float value);
  113. /// Construct from a double.
  114. explicit String(double value);
  115. /// Construct from a bool.
  116. explicit String(bool value);
  117. /// Construct from a character.
  118. explicit String(char value);
  119. /// Construct from a character and fill length.
  120. explicit String(char value, unsigned length);
  121. /// Construct from a convertable value.
  122. template <class T> explicit String(const T& value) :
  123. length_(0),
  124. capacity_(0),
  125. buffer_(&endZero)
  126. {
  127. *this = value.ToString();
  128. }
  129. /// Destruct.
  130. ~String()
  131. {
  132. if (capacity_)
  133. delete[] buffer_;
  134. }
  135. /// Assign a string.
  136. String& operator = (const String& rhs)
  137. {
  138. Resize(rhs.length_);
  139. CopyChars(buffer_, rhs.buffer_, rhs.length_);
  140. return *this;
  141. }
  142. /// Assign a C string.
  143. String& operator = (const char* rhs)
  144. {
  145. unsigned rhsLength = CStringLength(rhs);
  146. Resize(rhsLength);
  147. CopyChars(buffer_, rhs, rhsLength);
  148. return *this;
  149. }
  150. /// Add-assign a string.
  151. String& operator += (const String& rhs)
  152. {
  153. unsigned oldLength = length_;
  154. Resize(length_ + rhs.length_);
  155. CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
  156. return *this;
  157. }
  158. /// Add-assign a C string.
  159. String& operator += (const char* rhs)
  160. {
  161. unsigned rhsLength = CStringLength(rhs);
  162. unsigned oldLength = length_;
  163. Resize(length_ + rhsLength);
  164. CopyChars(buffer_ + oldLength, rhs, rhsLength);
  165. return *this;
  166. }
  167. /// Add-assign a character.
  168. String& operator += (char rhs)
  169. {
  170. unsigned oldLength = length_;
  171. Resize(length_ + 1);
  172. buffer_[oldLength] = rhs;
  173. return *this;
  174. }
  175. /// Add-assign an integer.
  176. String& operator += (int rhs);
  177. /// Add-assign a short integer.
  178. String& operator += (short rhs);
  179. /// Add-assign an unsigned integer.
  180. String& operator += (unsigned rhs);
  181. /// Add-assign a short unsigned integer.
  182. String& operator += (unsigned short rhs);
  183. /// Add-assign a float.
  184. String& operator += (float rhs);
  185. /// Add-assign a bool.
  186. String& operator += (bool rhs);
  187. /// Add-assign an arbitraty type.
  188. template <class T> String operator += (const T& rhs) { return *this += rhs.ToString(); }
  189. /// Add a string.
  190. String operator + (const String& rhs) const
  191. {
  192. String ret;
  193. ret.Resize(length_ + rhs.length_);
  194. CopyChars(ret.buffer_, buffer_, length_);
  195. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  196. return ret;
  197. }
  198. /// Add a C string.
  199. String operator + (const char* rhs) const
  200. {
  201. unsigned rhsLength = CStringLength(rhs);
  202. String ret;
  203. ret.Resize(length_ + rhsLength);
  204. CopyChars(ret.buffer_, buffer_, length_);
  205. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  206. return ret;
  207. }
  208. /// Add a character.
  209. String operator + (char rhs) const
  210. {
  211. String ret(*this);
  212. ret += rhs;
  213. return ret;
  214. }
  215. /// Test for equality with another string.
  216. bool operator == (const String& rhs) const { return strcmp(CString(), rhs.CString()) == 0; }
  217. /// Test for inequality with another string.
  218. bool operator != (const String& rhs) const { return strcmp(CString(), rhs.CString()) != 0; }
  219. /// Test if string is less than another string.
  220. bool operator < (const String& rhs) const { return strcmp(CString(), rhs.CString()) < 0; }
  221. /// Test if string is greater than another string.
  222. bool operator > (const String& rhs) const { return strcmp(CString(), rhs.CString()) > 0; }
  223. /// Test for equality with a C string.
  224. bool operator == (const char* rhs) const { return strcmp(CString(), rhs) == 0; }
  225. /// Test for inequality with a C string.
  226. bool operator != (const char* rhs) const { return strcmp(CString(), rhs) != 0; }
  227. /// Test if string is less than a C string.
  228. bool operator < (const char* rhs) const { return strcmp(CString(), rhs) < 0; }
  229. /// Test if string is greater than a C string.
  230. bool operator > (const char* rhs) const { return strcmp(CString(), rhs) > 0; }
  231. /// Return char at index.
  232. char& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  233. /// Return const char at index.
  234. const char& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  235. /// Return char at index.
  236. char& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  237. /// Return const char at index.
  238. const char& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  239. /// Replace all occurrences of a character.
  240. void Replace(char replaceThis, char replaceWith, bool caseSensitive = true);
  241. /// Replace all occurrences of a string.
  242. void Replace(const String& replaceThis, const String& replaceWith, bool caseSensitive = true);
  243. /// Replace a substring.
  244. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  245. /// Replace a substring with a C string.
  246. void Replace(unsigned pos, unsigned length, const char* replaceWith);
  247. /// Replace a substring by iterators.
  248. Iterator Replace(const Iterator& start, const Iterator& end, const String& replaceWith);
  249. /// Return a string with all occurrences of a character replaced.
  250. String Replaced(char replaceThis, char replaceWith, bool caseSensitive = true) const;
  251. /// Return a string with all occurrences of a string replaced.
  252. String Replaced(const String& replaceThis, const String& replaceWith, bool caseSensitive = true) const;
  253. /// Append a string.
  254. String& Append(const String& str);
  255. /// Append a C string.
  256. String& Append(const char* str);
  257. /// Append a character.
  258. String& Append(char c);
  259. /// Append characters.
  260. String& Append(const char* str, unsigned length);
  261. /// Insert a string.
  262. void Insert(unsigned pos, const String& str);
  263. /// Insert a character.
  264. void Insert(unsigned pos, char c);
  265. /// Insert a string by iterator.
  266. Iterator Insert(const Iterator& dest, const String& str);
  267. /// Insert a string partially by iterators.
  268. Iterator Insert(const Iterator& dest, const Iterator& start, const Iterator& end);
  269. /// Insert a character by iterator.
  270. Iterator Insert(const Iterator& dest, char c);
  271. /// Erase a substring.
  272. void Erase(unsigned pos, unsigned length = 1);
  273. /// Erase a character by iterator.
  274. Iterator Erase(const Iterator& it);
  275. /// Erase a substring by iterators.
  276. Iterator Erase(const Iterator& start, const Iterator& end);
  277. /// Resize the string.
  278. void Resize(unsigned newLength);
  279. /// Set new capacity.
  280. void Reserve(unsigned newCapacity);
  281. /// Reallocate so that no extra memory is used.
  282. void Compact();
  283. /// Clear the string.
  284. void Clear();
  285. /// Swap with another string.
  286. void Swap(String& str);
  287. /// Return iterator to the beginning.
  288. Iterator Begin() { return Iterator(buffer_); }
  289. /// Return const iterator to the beginning.
  290. ConstIterator Begin() const { return ConstIterator(buffer_); }
  291. /// Return iterator to the end.
  292. Iterator End() { return Iterator(buffer_ + length_); }
  293. /// Return const iterator to the end.
  294. ConstIterator End() const { return ConstIterator(buffer_ + length_); }
  295. /// Return first char, or 0 if empty.
  296. char Front() const { return buffer_[0]; }
  297. /// Return last char, or 0 if empty.
  298. char Back() const { return length_ ? buffer_[length_ - 1] : buffer_[0]; }
  299. /// Return a substring from position to end.
  300. String Substring(unsigned pos) const;
  301. /// Return a substring with length from position.
  302. String Substring(unsigned pos, unsigned length) const;
  303. /// Return string with whitespace trimmed from the beginning and the end.
  304. String Trimmed() const;
  305. /// Return string in uppercase.
  306. String ToUpper() const;
  307. /// Return string in lowercase.
  308. String ToLower() const;
  309. /// Return substrings split by a separator char.
  310. Vector<String> Split(char separator) const;
  311. /// Join substrings with a 'glue' string.
  312. void Join(const Vector<String>& subStrings, String glue);
  313. /// Return index to the first occurrence of a string, or NPOS if not found.
  314. unsigned Find(const String& str, unsigned startPos = 0, bool caseSensitive = true) const;
  315. /// Return index to the first occurrence of a character, or NPOS if not found.
  316. unsigned Find(char c, unsigned startPos = 0, bool caseSensitive = true) const;
  317. /// Return index to the last occurrence of a string, or NPOS if not found.
  318. unsigned FindLast(const String& str, unsigned startPos = NPOS, bool caseSensitive = true) const;
  319. /// Return index to the last occurrence of a character, or NPOS if not found.
  320. unsigned FindLast(char c, unsigned startPos = NPOS, bool caseSensitive = true) const;
  321. /// Return whether starts with a string.
  322. bool StartsWith(const String& str, bool caseSensitive = true) const;
  323. /// Return whether ends with a string.
  324. bool EndsWith(const String& str, bool casSensitive = true) const;
  325. /// Return the C string.
  326. const char* CString() const { return buffer_; }
  327. /// Return length.
  328. unsigned Length() const { return length_; }
  329. /// Return buffer capacity.
  330. unsigned Capacity() const { return capacity_; }
  331. /// Return whether the string is empty.
  332. bool Empty() const { return length_ == 0; }
  333. /// Return comparision result with a string.
  334. int Compare(const String& str, bool caseSensitive = true) const;
  335. /// Return comparision result with a C string.
  336. int Compare(const char* str, bool caseSensitive = true) const;
  337. /// Return whether contains a specific occurences of string.
  338. bool Contains(const String& str) const { return Find(str) != NPOS; }
  339. /// Return whether contains a specific character.
  340. bool Contains(char c) const { return Find(c) != NPOS; }
  341. /// Construct UTF8 content from Latin1.
  342. void SetUTF8FromLatin1(const char* str);
  343. /// Construct UTF8 content from wide characters.
  344. void SetUTF8FromWChar(const wchar_t* str);
  345. /// Calculate number of characters in UTF8 content.
  346. unsigned LengthUTF8() const;
  347. /// Return byte offset to char in UTF8 content.
  348. unsigned ByteOffsetUTF8(unsigned index) const;
  349. /// Return next Unicode character from UTF8 content and increase byte offset.
  350. unsigned NextUTF8Char(unsigned& byteOffset) const;
  351. /// Return Unicode character at index from UTF8 content.
  352. unsigned AtUTF8(unsigned index) const;
  353. /// Replace Unicode character at index from UTF8 content.
  354. void ReplaceUTF8(unsigned index, unsigned unicodeChar);
  355. /// Append Unicode character at the end as UTF8.
  356. String& AppendUTF8(unsigned unicodeChar);
  357. /// Return a UTF8 substring from position to end.
  358. String SubstringUTF8(unsigned pos) const;
  359. /// Return a UTF8 substring with length from position.
  360. String SubstringUTF8(unsigned pos, unsigned length) const;
  361. /// Return hash value for HashSet & HashMap.
  362. unsigned ToHash() const
  363. {
  364. unsigned hash = 0;
  365. const char* ptr = buffer_;
  366. while (*ptr)
  367. {
  368. hash = *ptr + (hash << 6) + (hash << 16) - hash;
  369. ++ptr;
  370. }
  371. return hash;
  372. }
  373. /// Return substrings split by a separator char.
  374. static Vector<String> Split(const char* str, char separator);
  375. /// Return a string by joining substrings with a 'glue' string.
  376. static String Joined(const Vector<String>& subStrings, String glue);
  377. /// Encode Unicode character to UTF8. Pointer will be incremented.
  378. static void EncodeUTF8(char*& dest, unsigned unicodeChar);
  379. /// Decode Unicode character from UTF8. Pointer will be incremented.
  380. static unsigned DecodeUTF8(const char*& src);
  381. #ifdef WIN32
  382. /// Encode Unicode character to UTF16. Pointer will be incremented.
  383. static void EncodeUTF16(wchar_t*& dest, unsigned unicodeChar);
  384. /// Decode Unicode character from UTF16. Pointer will be incremented.
  385. static unsigned DecodeUTF16(const wchar_t*& src);
  386. #endif
  387. /// Return length of a C string.
  388. static unsigned CStringLength(const char* str)
  389. {
  390. if (!str)
  391. return 0;
  392. #ifdef _MSC_VER
  393. return (unsigned)strlen(str);
  394. #else
  395. const char* ptr = str;
  396. while (*ptr)
  397. ++ptr;
  398. return ptr - str;
  399. #endif
  400. }
  401. /// Append to string using formatting.
  402. String& AppendWithFormat(const char* formatString, ... );
  403. /// Append to string using variable arguments.
  404. String& AppendWithFormatArgs(const char* formatString, va_list args);
  405. /// Compare two C strings.
  406. static int Compare(const char* str1, const char* str2, bool caseSensitive);
  407. /// Position for "not found."
  408. static const unsigned NPOS = 0xffffffff;
  409. /// Initial dynamic allocation size.
  410. static const unsigned MIN_CAPACITY = 8;
  411. /// Empty string.
  412. static const String EMPTY;
  413. private:
  414. /// Move a range of characters within the string.
  415. void MoveRange(unsigned dest, unsigned src, unsigned count)
  416. {
  417. if (count)
  418. memmove(buffer_ + dest, buffer_ + src, count);
  419. }
  420. /// Copy chars from one buffer to another.
  421. static void CopyChars(char* dest, const char* src, unsigned count)
  422. {
  423. #ifdef _MSC_VER
  424. if (count)
  425. memcpy(dest, src, count);
  426. #else
  427. char* end = dest + count;
  428. while (dest != end)
  429. {
  430. *dest = *src;
  431. ++dest;
  432. ++src;
  433. }
  434. #endif
  435. }
  436. /// Replace a substring with another substring.
  437. void Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength);
  438. /// String length.
  439. unsigned length_;
  440. /// Capacity, zero if buffer not allocated.
  441. unsigned capacity_;
  442. /// String buffer, null if not allocated.
  443. char* buffer_;
  444. /// End zero for empty strings.
  445. static char endZero;
  446. };
  447. /// Add a string to a C string.
  448. inline String operator + (const char* lhs, const String& rhs)
  449. {
  450. String ret(lhs);
  451. ret += rhs;
  452. return ret;
  453. }
  454. /// Wide character string. Only meant for converting from String and passing to the operating system where necessary.
  455. class URHO3D_API WString
  456. {
  457. public:
  458. /// Construct empty.
  459. WString();
  460. /// Construct from a string.
  461. WString(const String& str);
  462. /// Destruct.
  463. ~WString();
  464. /// Return char at index.
  465. wchar_t& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
  466. /// Return const char at index.
  467. const wchar_t& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
  468. /// Return char at index.
  469. wchar_t& At(unsigned index) { assert(index < length_); return buffer_[index]; }
  470. /// Return const char at index.
  471. const wchar_t& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
  472. /// Resize the string.
  473. void Resize(unsigned newSize);
  474. /// Return whether the string is empty.
  475. bool Empty() const { return length_ == 0; }
  476. /// Return length.
  477. unsigned Length() const { return length_; }
  478. /// Return character data.
  479. const wchar_t* CString() const { return buffer_; }
  480. private:
  481. /// String length.
  482. unsigned length_;
  483. /// String buffer, null if not allocated.
  484. wchar_t* buffer_;
  485. };
  486. }