Str.h 18 KB

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