Str.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. //
  2. // Copyright (c) 2008-2016 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 "../Container/Vector.h"
  24. #include <cstdarg>
  25. #include <cstring>
  26. #include <cctype>
  27. namespace Atomic
  28. {
  29. static const int CONVERSION_BUFFER_LENGTH = 128;
  30. static const int MATRIX_CONVERSION_BUFFER_LENGTH = 256;
  31. class WString;
  32. /// %String class.
  33. class ATOMIC_API String
  34. {
  35. public:
  36. typedef RandomAccessIterator<char> Iterator;
  37. typedef RandomAccessConstIterator<char> ConstIterator;
  38. /// Construct empty.
  39. String() :
  40. length_(0),
  41. capacity_(0),
  42. buffer_(&endZero)
  43. {
  44. }
  45. /// Construct from another string.
  46. String(const String& str) :
  47. length_(0),
  48. capacity_(0),
  49. buffer_(&endZero)
  50. {
  51. *this = str;
  52. }
  53. /// Construct from a C string.
  54. String(const char* str) :
  55. length_(0),
  56. capacity_(0),
  57. buffer_(&endZero)
  58. {
  59. *this = str;
  60. }
  61. /// Construct from a C string.
  62. String(char* str) :
  63. length_(0),
  64. capacity_(0),
  65. buffer_(&endZero)
  66. {
  67. *this = (const char*)str;
  68. }
  69. /// Construct from a char array and length.
  70. String(const char* str, unsigned length) :
  71. length_(0),
  72. capacity_(0),
  73. buffer_(&endZero)
  74. {
  75. Resize(length);
  76. CopyChars(buffer_, str, length);
  77. }
  78. /// Construct from a null-terminated wide character array.
  79. String(const wchar_t* str) :
  80. length_(0),
  81. capacity_(0),
  82. buffer_(&endZero)
  83. {
  84. SetUTF8FromWChar(str);
  85. }
  86. /// Construct from a null-terminated wide character array.
  87. String(wchar_t* str) :
  88. length_(0),
  89. capacity_(0),
  90. buffer_(&endZero)
  91. {
  92. SetUTF8FromWChar(str);
  93. }
  94. /// Construct from a wide character string.
  95. String(const WString& str);
  96. /// Construct from an integer.
  97. explicit String(int value);
  98. /// Construct from a short integer.
  99. explicit String(short value);
  100. /// Construct from a long integer.
  101. explicit String(long value);
  102. /// Construct from a long long integer.
  103. explicit String(long long value);
  104. /// Construct from an unsigned integer.
  105. explicit String(unsigned value);
  106. /// Construct from an unsigned short integer.
  107. explicit String(unsigned short value);
  108. /// Construct from an unsigned long integer.
  109. explicit String(unsigned long value);
  110. /// Construct from an unsigned long long integer.
  111. explicit String(unsigned long long value);
  112. /// Construct from a float.
  113. explicit String(float value);
  114. /// Construct from a double.
  115. explicit String(double value);
  116. /// Construct from a bool.
  117. explicit String(bool value);
  118. /// Construct from a character.
  119. explicit String(char value);
  120. /// Construct from a character and fill length.
  121. explicit String(char value, unsigned length);
  122. /// Construct from a convertable value.
  123. template <class T> explicit String(const T& value) :
  124. length_(0),
  125. capacity_(0),
  126. buffer_(&endZero)
  127. {
  128. *this = value.ToString();
  129. }
  130. /// Destruct.
  131. ~String()
  132. {
  133. if (capacity_)
  134. delete[] buffer_;
  135. }
  136. /// Assign a string.
  137. String& operator =(const String& rhs)
  138. {
  139. Resize(rhs.length_);
  140. CopyChars(buffer_, rhs.buffer_, rhs.length_);
  141. return *this;
  142. }
  143. /// Assign a C string.
  144. String& operator =(const char* rhs)
  145. {
  146. unsigned rhsLength = CStringLength(rhs);
  147. Resize(rhsLength);
  148. CopyChars(buffer_, rhs, rhsLength);
  149. return *this;
  150. }
  151. /// Add-assign a string.
  152. String& operator +=(const String& rhs)
  153. {
  154. unsigned oldLength = length_;
  155. Resize(length_ + rhs.length_);
  156. CopyChars(buffer_ + oldLength, rhs.buffer_, rhs.length_);
  157. return *this;
  158. }
  159. /// Add-assign a C string.
  160. String& operator +=(const char* rhs)
  161. {
  162. unsigned rhsLength = CStringLength(rhs);
  163. unsigned oldLength = length_;
  164. Resize(length_ + rhsLength);
  165. CopyChars(buffer_ + oldLength, rhs, rhsLength);
  166. return *this;
  167. }
  168. /// Add-assign a character.
  169. String& operator +=(char rhs)
  170. {
  171. unsigned oldLength = length_;
  172. Resize(length_ + 1);
  173. buffer_[oldLength] = rhs;
  174. return *this;
  175. }
  176. /// Add-assign an integer.
  177. String& operator +=(int rhs);
  178. /// Add-assign a short integer.
  179. String& operator +=(short rhs);
  180. /// Add-assign a long integer.
  181. String& operator +=(long rhs);
  182. /// Add-assign a long long integer.
  183. String& operator +=(long long rhs);
  184. /// Add-assign an unsigned integer.
  185. String& operator +=(unsigned rhs);
  186. /// Add-assign a short unsigned integer.
  187. String& operator +=(unsigned short rhs);
  188. /// Add-assign a long unsigned integer.
  189. String& operator +=(unsigned long rhs);
  190. /// Add-assign a long long unsigned integer.
  191. String& operator +=(unsigned long long rhs);
  192. /// Add-assign a float.
  193. String& operator +=(float rhs);
  194. /// Add-assign a bool.
  195. String& operator +=(bool rhs);
  196. /// Add-assign an arbitraty type.
  197. template <class T> String operator +=(const T& rhs) { return *this += rhs.ToString(); }
  198. /// Add a string.
  199. String operator +(const String& rhs) const
  200. {
  201. String ret;
  202. ret.Resize(length_ + rhs.length_);
  203. CopyChars(ret.buffer_, buffer_, length_);
  204. CopyChars(ret.buffer_ + length_, rhs.buffer_, rhs.length_);
  205. return ret;
  206. }
  207. /// Add a C string.
  208. String operator +(const char* rhs) const
  209. {
  210. unsigned rhsLength = CStringLength(rhs);
  211. String ret;
  212. ret.Resize(length_ + rhsLength);
  213. CopyChars(ret.buffer_, buffer_, length_);
  214. CopyChars(ret.buffer_ + length_, rhs, rhsLength);
  215. return ret;
  216. }
  217. /// Add a character.
  218. String operator +(char rhs) const
  219. {
  220. String ret(*this);
  221. ret += rhs;
  222. return ret;
  223. }
  224. /// Add an integer.
  225. String operator +(int rhs) const
  226. {
  227. return *this + String(rhs);
  228. }
  229. /// Add a short integer.
  230. String operator +(short rhs) const
  231. {
  232. return *this + String(rhs);
  233. }
  234. /// Add a long integer.
  235. String operator +(long rhs) const
  236. {
  237. return *this + String(rhs);
  238. }
  239. /// Add a long long integer.
  240. String operator +(long long rhs) const
  241. {
  242. return *this + String(rhs);
  243. }
  244. /// Add an unsigned integer.
  245. String operator +(unsigned rhs) const
  246. {
  247. return *this + String(rhs);
  248. }
  249. /// Add a short unsigned integer.
  250. String operator +(unsigned short rhs) const
  251. {
  252. return *this + String(rhs);
  253. }
  254. /// Add a long unsigned integer.
  255. String operator +(unsigned long rhs) const
  256. {
  257. return *this + String(rhs);
  258. }
  259. /// Add a long long unsigned integer.
  260. String operator +(unsigned long long rhs) const
  261. {
  262. return *this + String(rhs);
  263. }
  264. /// Add a float.
  265. String operator +(float rhs) const
  266. {
  267. return *this + String(rhs);
  268. }
  269. /// Add a bool.
  270. String operator +(bool rhs) const
  271. {
  272. return *this + String(rhs);
  273. }
  274. /// Test for equality with another string.
  275. bool operator ==(const String& rhs) const { return strcmp(CString(), rhs.CString()) == 0; }
  276. /// Test for inequality with another string.
  277. bool operator !=(const String& rhs) const { return strcmp(CString(), rhs.CString()) != 0; }
  278. /// Test if string is less than another string.
  279. bool operator <(const String& rhs) const { return strcmp(CString(), rhs.CString()) < 0; }
  280. /// Test if string is greater than another string.
  281. bool operator >(const String& rhs) const { return strcmp(CString(), rhs.CString()) > 0; }
  282. /// Test for equality with a C string.
  283. bool operator ==(const char* rhs) const { return strcmp(CString(), rhs) == 0; }
  284. /// Test for inequality with a C string.
  285. bool operator !=(const char* rhs) const { return strcmp(CString(), rhs) != 0; }
  286. /// Test if string is less than a C string.
  287. bool operator <(const char* rhs) const { return strcmp(CString(), rhs) < 0; }
  288. /// Test if string is greater than a C string.
  289. bool operator >(const char* rhs) const { return strcmp(CString(), rhs) > 0; }
  290. /// Return char at index.
  291. char& operator [](unsigned index)
  292. {
  293. assert(index < length_);
  294. return buffer_[index];
  295. }
  296. /// Return const char at index.
  297. const char& operator [](unsigned index) const
  298. {
  299. assert(index < length_);
  300. return buffer_[index];
  301. }
  302. /// Return char at index.
  303. char& At(unsigned index)
  304. {
  305. assert(index < length_);
  306. return buffer_[index];
  307. }
  308. /// Return const char at index.
  309. const char& At(unsigned index) const
  310. {
  311. assert(index < length_);
  312. return buffer_[index];
  313. }
  314. /// Replace all occurrences of a character.
  315. void Replace(char replaceThis, char replaceWith, bool caseSensitive = true);
  316. /// Replace all occurrences of a string.
  317. void Replace(const String& replaceThis, const String& replaceWith, bool caseSensitive = true);
  318. /// Replace a substring.
  319. void Replace(unsigned pos, unsigned length, const String& replaceWith);
  320. /// Replace a substring with a C string.
  321. void Replace(unsigned pos, unsigned length, const char* replaceWith);
  322. /// Replace a substring by iterators.
  323. Iterator Replace(const Iterator& start, const Iterator& end, const String& replaceWith);
  324. /// Return a string with all occurrences of a character replaced.
  325. String Replaced(char replaceThis, char replaceWith, bool caseSensitive = true) const;
  326. /// Return a string with all occurrences of a string replaced.
  327. String Replaced(const String& replaceThis, const String& replaceWith, bool caseSensitive = true) const;
  328. /// Append a string.
  329. String& Append(const String& str);
  330. /// Append a C string.
  331. String& Append(const char* str);
  332. /// Append a character.
  333. String& Append(char c);
  334. /// Append characters.
  335. String& Append(const char* str, unsigned length);
  336. /// Insert a string.
  337. void Insert(unsigned pos, const String& str);
  338. /// Insert a character.
  339. void Insert(unsigned pos, char c);
  340. /// Insert a string by iterator.
  341. Iterator Insert(const Iterator& dest, const String& str);
  342. /// Insert a string partially by iterators.
  343. Iterator Insert(const Iterator& dest, const Iterator& start, const Iterator& end);
  344. /// Insert a character by iterator.
  345. Iterator Insert(const Iterator& dest, char c);
  346. /// Erase a substring.
  347. void Erase(unsigned pos, unsigned length = 1);
  348. /// Erase a character by iterator.
  349. Iterator Erase(const Iterator& it);
  350. /// Erase a substring by iterators.
  351. Iterator Erase(const Iterator& start, const Iterator& end);
  352. /// Resize the string.
  353. void Resize(unsigned newLength);
  354. /// Set new capacity.
  355. void Reserve(unsigned newCapacity);
  356. /// Reallocate so that no extra memory is used.
  357. void Compact();
  358. /// Clear the string.
  359. void Clear();
  360. /// Swap with another string.
  361. void Swap(String& str);
  362. /// Return iterator to the beginning.
  363. Iterator Begin() { return Iterator(buffer_); }
  364. /// Return const iterator to the beginning.
  365. ConstIterator Begin() const { return ConstIterator(buffer_); }
  366. /// Return iterator to the end.
  367. Iterator End() { return Iterator(buffer_ + length_); }
  368. /// Return const iterator to the end.
  369. ConstIterator End() const { return ConstIterator(buffer_ + length_); }
  370. /// Return first char, or 0 if empty.
  371. char Front() const { return buffer_[0]; }
  372. /// Return last char, or 0 if empty.
  373. char Back() const { return length_ ? buffer_[length_ - 1] : buffer_[0]; }
  374. /// Return a substring from position to end.
  375. String Substring(unsigned pos) const;
  376. /// Return a substring with length from position.
  377. String Substring(unsigned pos, unsigned length) const;
  378. /// Return string with whitespace trimmed from the beginning and the end.
  379. String Trimmed() const;
  380. /// Return string in uppercase.
  381. String ToUpper() const;
  382. /// Return string in lowercase.
  383. String ToLower() const;
  384. /// Return substrings split by a separator char. By default don't return empty strings.
  385. Vector<String> Split(char separator, bool keepEmptyStrings = false) const;
  386. /// Join substrings with a 'glue' string.
  387. void Join(const Vector<String>& subStrings, const String& glue);
  388. /// Return index to the first occurrence of a string, or NPOS if not found.
  389. unsigned Find(const String& str, unsigned startPos = 0, bool caseSensitive = true) const;
  390. /// Return index to the first occurrence of a character, or NPOS if not found.
  391. unsigned Find(char c, unsigned startPos = 0, bool caseSensitive = true) const;
  392. /// Return index to the last occurrence of a string, or NPOS if not found.
  393. unsigned FindLast(const String& str, unsigned startPos = NPOS, bool caseSensitive = true) const;
  394. /// Return index to the last occurrence of a character, or NPOS if not found.
  395. unsigned FindLast(char c, unsigned startPos = NPOS, bool caseSensitive = true) const;
  396. /// Return whether starts with a string.
  397. bool StartsWith(const String& str, bool caseSensitive = true) const;
  398. /// Return whether ends with a string.
  399. bool EndsWith(const String& str, bool caseSensitive = true) const;
  400. /// Return the C string.
  401. const char* CString() const { return buffer_; }
  402. /// Return length.
  403. unsigned Length() const { return length_; }
  404. /// Return buffer capacity.
  405. unsigned Capacity() const { return capacity_; }
  406. /// Return whether the string is empty.
  407. bool Empty() const { return length_ == 0; }
  408. /// Return comparison result with a string.
  409. int Compare(const String& str, bool caseSensitive = true) const;
  410. /// Return comparison result with a C string.
  411. int Compare(const char* str, bool caseSensitive = true) const;
  412. /// Return whether contains a specific occurrence of a string.
  413. bool Contains(const String& str, bool caseSensitive = true) const { return Find(str, 0, caseSensitive) != NPOS; }
  414. /// Return whether contains a specific character.
  415. bool Contains(char c, bool caseSensitive = true) const { return Find(c, 0, caseSensitive) != NPOS; }
  416. /// Construct UTF8 content from Latin1.
  417. void SetUTF8FromLatin1(const char* str);
  418. /// Construct UTF8 content from wide characters.
  419. void SetUTF8FromWChar(const wchar_t* str);
  420. /// Calculate number of characters in UTF8 content.
  421. unsigned LengthUTF8() const;
  422. /// Return byte offset to char in UTF8 content.
  423. unsigned ByteOffsetUTF8(unsigned index) const;
  424. /// Return next Unicode character from UTF8 content and increase byte offset.
  425. unsigned NextUTF8Char(unsigned& byteOffset) const;
  426. /// Return Unicode character at index from UTF8 content.
  427. unsigned AtUTF8(unsigned index) const;
  428. /// Replace Unicode character at index from UTF8 content.
  429. void ReplaceUTF8(unsigned index, unsigned unicodeChar);
  430. /// Append Unicode character at the end as UTF8.
  431. String& AppendUTF8(unsigned unicodeChar);
  432. /// Return a UTF8 substring from position to end.
  433. String SubstringUTF8(unsigned pos) const;
  434. /// Return a UTF8 substring with length from position.
  435. String SubstringUTF8(unsigned pos, unsigned length) const;
  436. /// Return hash value for HashSet & HashMap.
  437. unsigned ToHash() const
  438. {
  439. unsigned hash = 0;
  440. const char* ptr = buffer_;
  441. while (*ptr)
  442. {
  443. hash = *ptr + (hash << 6) + (hash << 16) - hash;
  444. ++ptr;
  445. }
  446. return hash;
  447. }
  448. /// Return substrings split by a separator char. By default don't return empty strings.
  449. static Vector<String> Split(const char* str, char separator, bool keepEmptyStrings = false);
  450. /// Return a string by joining substrings with a 'glue' string.
  451. static String Joined(const Vector<String>& subStrings, const String& glue);
  452. /// Encode Unicode character to UTF8. Pointer will be incremented.
  453. static void EncodeUTF8(char*& dest, unsigned unicodeChar);
  454. /// Decode Unicode character from UTF8. Pointer will be incremented.
  455. static unsigned DecodeUTF8(const char*& src);
  456. #ifdef _WIN32
  457. /// Encode Unicode character to UTF16. Pointer will be incremented.
  458. static void EncodeUTF16(wchar_t*& dest, unsigned unicodeChar);
  459. /// Decode Unicode character from UTF16. Pointer will be incremented.
  460. static unsigned DecodeUTF16(const wchar_t*& src);
  461. #endif
  462. /// Return length of a C string.
  463. static unsigned CStringLength(const char* str) { return str ? (unsigned)strlen(str) : 0; }
  464. /// Append to string using formatting.
  465. String& AppendWithFormat(const char* formatString, ...);
  466. /// Append to string using variable arguments.
  467. String& AppendWithFormatArgs(const char* formatString, va_list args);
  468. /// Compare two C strings.
  469. static int Compare(const char* str1, const char* str2, bool caseSensitive);
  470. /// Position for "not found."
  471. static const unsigned NPOS = 0xffffffff;
  472. /// Initial dynamic allocation size.
  473. static const unsigned MIN_CAPACITY = 8;
  474. /// Empty string.
  475. static const String EMPTY;
  476. private:
  477. /// Move a range of characters within the string.
  478. void MoveRange(unsigned dest, unsigned src, unsigned count)
  479. {
  480. if (count)
  481. memmove(buffer_ + dest, buffer_ + src, count);
  482. }
  483. /// Copy chars from one buffer to another.
  484. static void CopyChars(char* dest, const char* src, unsigned count)
  485. {
  486. #ifdef _MSC_VER
  487. if (count)
  488. memcpy(dest, src, count);
  489. #else
  490. char* end = dest + count;
  491. while (dest != end)
  492. {
  493. *dest = *src;
  494. ++dest;
  495. ++src;
  496. }
  497. #endif
  498. }
  499. /// Replace a substring with another substring.
  500. void Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength);
  501. /// String length.
  502. unsigned length_;
  503. /// Capacity, zero if buffer not allocated.
  504. unsigned capacity_;
  505. /// String buffer, null if not allocated.
  506. char* buffer_;
  507. /// End zero for empty strings.
  508. static char endZero;
  509. };
  510. /// Add a string to a C string.
  511. inline String operator +(const char* lhs, const String& rhs)
  512. {
  513. String ret(lhs);
  514. ret += rhs;
  515. return ret;
  516. }
  517. /// Add a string to a wide char C string.
  518. inline String operator +(const wchar_t* lhs, const String& rhs)
  519. {
  520. String ret(lhs);
  521. ret += rhs;
  522. return ret;
  523. }
  524. /// Wide character string. Only meant for converting from String and passing to the operating system where necessary.
  525. class ATOMIC_API WString
  526. {
  527. public:
  528. /// Construct empty.
  529. WString();
  530. /// Construct from a string.
  531. WString(const String& str);
  532. /// Destruct.
  533. ~WString();
  534. /// Return char at index.
  535. wchar_t& operator [](unsigned index)
  536. {
  537. assert(index < length_);
  538. return buffer_[index];
  539. }
  540. /// Return const char at index.
  541. const wchar_t& operator [](unsigned index) const
  542. {
  543. assert(index < length_);
  544. return buffer_[index];
  545. }
  546. /// Return char at index.
  547. wchar_t& At(unsigned index)
  548. {
  549. assert(index < length_);
  550. return buffer_[index];
  551. }
  552. /// Return const char at index.
  553. const wchar_t& At(unsigned index) const
  554. {
  555. assert(index < length_);
  556. return buffer_[index];
  557. }
  558. /// Resize the string.
  559. void Resize(unsigned newLength);
  560. /// Return whether the string is empty.
  561. bool Empty() const { return length_ == 0; }
  562. /// Return length.
  563. unsigned Length() const { return length_; }
  564. /// Return character data.
  565. const wchar_t* CString() const { return buffer_; }
  566. private:
  567. /// String length.
  568. unsigned length_;
  569. /// String buffer, null if not allocated.
  570. wchar_t* buffer_;
  571. };
  572. }