Str.h 21 KB

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