Str.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. #include "Str.h"
  23. #include "Swap.h"
  24. #include <cstdio>
  25. #include "DebugNew.h"
  26. namespace Urho3D
  27. {
  28. char String::endZero = 0;
  29. const String String::EMPTY;
  30. void PrintArgs(const char *formatString, va_list args);
  31. String::String(const WString& str) :
  32. length_(0),
  33. capacity_(0),
  34. buffer_(&endZero)
  35. {
  36. SetUTF8FromWChar(str.CString());
  37. }
  38. String::String(int value) :
  39. length_(0),
  40. capacity_(0),
  41. buffer_(&endZero)
  42. {
  43. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  44. sprintf(tempBuffer, "%d", value);
  45. *this = tempBuffer;
  46. }
  47. String::String(short value) :
  48. length_(0),
  49. capacity_(0),
  50. buffer_(&endZero)
  51. {
  52. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  53. sprintf(tempBuffer, "%d", value);
  54. *this = tempBuffer;
  55. }
  56. String::String(unsigned value) :
  57. length_(0),
  58. capacity_(0),
  59. buffer_(&endZero)
  60. {
  61. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  62. sprintf(tempBuffer, "%u", value);
  63. *this = tempBuffer;
  64. }
  65. String::String(unsigned short value) :
  66. length_(0),
  67. capacity_(0),
  68. buffer_(&endZero)
  69. {
  70. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  71. sprintf(tempBuffer, "%u", value);
  72. *this = tempBuffer;
  73. }
  74. String::String(float value) :
  75. length_(0),
  76. capacity_(0),
  77. buffer_(&endZero)
  78. {
  79. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  80. sprintf(tempBuffer, "%g", value);
  81. *this = tempBuffer;
  82. }
  83. String::String(double value) :
  84. length_(0),
  85. capacity_(0),
  86. buffer_(&endZero)
  87. {
  88. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  89. sprintf(tempBuffer, "%g", value);
  90. *this = tempBuffer;
  91. }
  92. String::String(bool value) :
  93. length_(0),
  94. capacity_(0),
  95. buffer_(&endZero)
  96. {
  97. if (value)
  98. *this = "true";
  99. else
  100. *this = "false";
  101. }
  102. String::String(char value) :
  103. length_(0),
  104. capacity_(0),
  105. buffer_(&endZero)
  106. {
  107. Resize(1);
  108. buffer_[0] = value;
  109. }
  110. String::String(char value, unsigned length) :
  111. length_(0),
  112. capacity_(0),
  113. buffer_(&endZero)
  114. {
  115. Resize(length);
  116. for (unsigned i = 0; i < length; ++i)
  117. buffer_[i] = value;
  118. }
  119. String& String::operator += (int rhs)
  120. {
  121. return *this += String(rhs);
  122. }
  123. String& String::operator += (short rhs)
  124. {
  125. return *this += String(rhs);
  126. }
  127. String& String::operator += (unsigned rhs)
  128. {
  129. return *this += String(rhs);
  130. }
  131. String& String::operator += (unsigned short rhs)
  132. {
  133. return *this += String(rhs);
  134. }
  135. String& String::operator += (float rhs)
  136. {
  137. return *this += String(rhs);
  138. }
  139. String& String::operator += (bool rhs)
  140. {
  141. return *this += String(rhs);
  142. }
  143. void String::Replace(char replaceThis, char replaceWith)
  144. {
  145. for (unsigned i = 0; i < length_; ++i)
  146. {
  147. if (buffer_[i] == replaceThis)
  148. buffer_[i] = replaceWith;
  149. }
  150. }
  151. void String::Replace(const String& replaceThis, const String& replaceWith)
  152. {
  153. unsigned nextPos = 0;
  154. while (nextPos < length_)
  155. {
  156. unsigned pos = Find(replaceThis, nextPos);
  157. if (pos == NPOS)
  158. break;
  159. Replace(pos, replaceThis.length_, replaceWith);
  160. nextPos = pos + replaceWith.length_;
  161. }
  162. }
  163. void String::Replace(unsigned pos, unsigned length, const String& str)
  164. {
  165. // If substring is illegal, do nothing
  166. if (pos + length > length_)
  167. return;
  168. Replace(pos, length, str.buffer_, str.length_);
  169. }
  170. String::Iterator String::Replace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
  171. {
  172. unsigned pos = start - Begin();
  173. if (pos >= length_)
  174. return End();
  175. unsigned length = end - start;
  176. Replace(pos, length, replaceWith);
  177. return Begin() + pos;
  178. }
  179. String String::Replaced(char replaceThis, char replaceWith) const
  180. {
  181. String ret(*this);
  182. ret.Replace(replaceThis, replaceWith);
  183. return ret;
  184. }
  185. String String::Replaced(const String& replaceThis, const String& replaceWith) const
  186. {
  187. String ret(*this);
  188. ret.Replace(replaceThis, replaceWith);
  189. return ret;
  190. }
  191. void String::Append(const String& str)
  192. {
  193. *this += str;
  194. }
  195. void String::Append(const char* str)
  196. {
  197. *this += str;
  198. }
  199. void String::Append(char c)
  200. {
  201. *this += c;
  202. }
  203. void String::Append(const char* str, unsigned length)
  204. {
  205. if (!str)
  206. return;
  207. unsigned oldLength = length_;
  208. Resize(oldLength + length);
  209. CopyChars(&buffer_[oldLength], str, length);
  210. }
  211. void String::Insert(unsigned pos, const String& str)
  212. {
  213. if (pos > length_)
  214. pos = length_;
  215. if (pos == length_)
  216. (*this) += str;
  217. else
  218. Replace(pos, 0, str);
  219. }
  220. void String::Insert(unsigned pos, char c)
  221. {
  222. if (pos > length_)
  223. pos = length_;
  224. if (pos == length_)
  225. (*this) += c;
  226. else
  227. {
  228. unsigned oldLength = length_;
  229. Resize(length_ + 1);
  230. MoveRange(pos + 1, pos, oldLength - pos);
  231. buffer_[pos] = c;
  232. }
  233. }
  234. String::Iterator String::Insert(const String::Iterator& dest, const String& str)
  235. {
  236. unsigned pos = dest - Begin();
  237. if (pos > length_)
  238. pos = length_;
  239. Insert(pos, str);
  240. return Begin() + pos;
  241. }
  242. String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
  243. {
  244. unsigned pos = dest - Begin();
  245. if (pos > length_)
  246. pos = length_;
  247. unsigned length = end - start;
  248. Replace(pos, 0, &(*start), length);
  249. return Begin() + pos;
  250. }
  251. String::Iterator String::Insert(const String::Iterator& dest, char c)
  252. {
  253. unsigned pos = dest - Begin();
  254. if (pos > length_)
  255. pos = length_;
  256. Insert(pos, c);
  257. return Begin() + pos;
  258. }
  259. void String::Erase(unsigned pos, unsigned length)
  260. {
  261. Replace(pos, length, String());
  262. }
  263. String::Iterator String::Erase(const String::Iterator& it)
  264. {
  265. unsigned pos = it - Begin();
  266. if (pos >= length_)
  267. return End();
  268. Erase(pos);
  269. return Begin() + pos;
  270. }
  271. String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
  272. {
  273. unsigned pos = start - Begin();
  274. if (pos >= length_)
  275. return End();
  276. unsigned length = end - start;
  277. Erase(pos, length);
  278. return Begin() + pos;
  279. }
  280. void String::Resize(unsigned newLength)
  281. {
  282. if (!capacity_)
  283. {
  284. // Calculate initial capacity
  285. capacity_ = newLength + 1;
  286. if (capacity_ < MIN_CAPACITY)
  287. capacity_ = MIN_CAPACITY;
  288. buffer_ = new char[capacity_];
  289. }
  290. else
  291. {
  292. if (newLength && capacity_ < newLength + 1)
  293. {
  294. // Increase the capacity with half each time it is exceeded
  295. while (capacity_ < newLength + 1)
  296. capacity_ += (capacity_ + 1) >> 1;
  297. char* newBuffer = new char[capacity_];
  298. // Move the existing data to the new buffer, then delete the old buffer
  299. if (length_)
  300. CopyChars(newBuffer, buffer_, length_);
  301. delete[] buffer_;
  302. buffer_ = newBuffer;
  303. }
  304. }
  305. buffer_[newLength] = 0;
  306. length_ = newLength;
  307. }
  308. void String::Reserve(unsigned newCapacity)
  309. {
  310. if (newCapacity < length_ + 1)
  311. newCapacity = length_ + 1;
  312. if (newCapacity == capacity_)
  313. return;
  314. char* newBuffer = new char[newCapacity];
  315. // Move the existing data to the new buffer, then delete the old buffer
  316. CopyChars(newBuffer, buffer_, length_ + 1);
  317. if (capacity_)
  318. delete[] buffer_;
  319. capacity_ = newCapacity;
  320. buffer_ = newBuffer;
  321. }
  322. void String::Compact()
  323. {
  324. if (capacity_)
  325. Reserve(length_ + 1);
  326. }
  327. void String::Clear()
  328. {
  329. Resize(0);
  330. }
  331. void String::Swap(String& str)
  332. {
  333. Urho3D::Swap(length_, str.length_);
  334. Urho3D::Swap(capacity_, str.capacity_);
  335. Urho3D::Swap(buffer_, str.buffer_);
  336. }
  337. String String::Substring(unsigned pos) const
  338. {
  339. if (pos < length_)
  340. {
  341. String ret;
  342. ret.Resize(length_ - pos);
  343. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  344. return ret;
  345. }
  346. else
  347. return String();
  348. }
  349. String String::Substring(unsigned pos, unsigned length) const
  350. {
  351. if (pos < length_)
  352. {
  353. String ret;
  354. if (pos + length > length_)
  355. length = length_ - pos;
  356. ret.Resize(length);
  357. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  358. return ret;
  359. }
  360. else
  361. return String();
  362. }
  363. String String::Trimmed() const
  364. {
  365. unsigned trimStart = 0;
  366. unsigned trimEnd = length_;
  367. while (trimStart < trimEnd)
  368. {
  369. char c = buffer_[trimStart];
  370. if (c != ' ' && c != 9)
  371. break;
  372. ++trimStart;
  373. }
  374. while (trimEnd > trimStart)
  375. {
  376. char c = buffer_[trimEnd - 1];
  377. if (c != ' ' && c != 9)
  378. break;
  379. --trimEnd;
  380. }
  381. return Substring(trimStart, trimEnd - trimStart);
  382. }
  383. String String::ToLower() const
  384. {
  385. String ret(*this);
  386. for (unsigned i = 0; i < ret.length_; ++i)
  387. ret[i] = tolower(buffer_[i]);
  388. return ret;
  389. }
  390. String String::ToUpper() const
  391. {
  392. String ret(*this);
  393. for (unsigned i = 0; i < ret.length_; ++i)
  394. ret[i] = toupper(buffer_[i]);
  395. return ret;
  396. }
  397. Vector<String> String::Split(char separator) const
  398. {
  399. return Split(CString(), separator);
  400. }
  401. unsigned String::Find(char c, unsigned startPos) const
  402. {
  403. for (unsigned i = startPos; i < length_; ++i)
  404. {
  405. if (buffer_[i] == c)
  406. return i;
  407. }
  408. return NPOS;
  409. }
  410. unsigned String::Find(const String& str, unsigned startPos) const
  411. {
  412. if (!str.length_ || str.length_ > length_)
  413. return NPOS;
  414. char first = str.buffer_[0];
  415. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  416. {
  417. if (buffer_[i] == first)
  418. {
  419. unsigned skip = NPOS;
  420. bool found = true;
  421. for (unsigned j = 1; j < str.length_; ++j)
  422. {
  423. char c = buffer_[i + j];
  424. if (skip == NPOS && c == first)
  425. skip = i + j - 1;
  426. if (c != str.buffer_[j])
  427. {
  428. found = false;
  429. if (skip != NPOS)
  430. i = skip;
  431. break;
  432. }
  433. }
  434. if (found)
  435. return i;
  436. }
  437. }
  438. return NPOS;
  439. }
  440. unsigned String::FindLast(char c, unsigned startPos) const
  441. {
  442. if (startPos >= length_)
  443. startPos = length_ - 1;
  444. for (unsigned i = startPos; i < length_; --i)
  445. {
  446. if (buffer_[i] == c)
  447. return i;
  448. }
  449. return NPOS;
  450. }
  451. unsigned String::FindLast(const String& str, unsigned startPos) const
  452. {
  453. if (!str.length_ || str.length_ > length_)
  454. return NPOS;
  455. if (startPos > length_ - str.length_)
  456. startPos = length_ - str.length_;
  457. char first = str.buffer_[0];
  458. for (unsigned i = startPos; i < length_; --i)
  459. {
  460. if (buffer_[i] == first)
  461. {
  462. bool found = true;
  463. for (unsigned j = 1; j < str.length_; ++j)
  464. {
  465. char c = buffer_[i + j];
  466. if (c != str.buffer_[j])
  467. {
  468. found = false;
  469. break;
  470. }
  471. }
  472. if (found)
  473. return i;
  474. }
  475. }
  476. return NPOS;
  477. }
  478. bool String::StartsWith(const String& str) const
  479. {
  480. return Find(str) == 0;
  481. }
  482. bool String::EndsWith(const String& str) const
  483. {
  484. return FindLast(str) == Length() - str.Length();
  485. }
  486. int String::Compare(const String& str, bool caseSensitive) const
  487. {
  488. return Compare(CString(), str.CString(), caseSensitive);
  489. }
  490. int String::Compare(const char* str, bool caseSensitive) const
  491. {
  492. return Compare(CString(), str, caseSensitive);
  493. }
  494. void String::SetUTF8FromLatin1(const char* str)
  495. {
  496. char temp[7];
  497. Clear();
  498. if (!str)
  499. return;
  500. while (*str)
  501. {
  502. char* dest = temp;
  503. EncodeUTF8(dest, *str++);
  504. *dest = 0;
  505. Append(temp);
  506. }
  507. }
  508. void String::SetUTF8FromWChar(const wchar_t* str)
  509. {
  510. char temp[7];
  511. Clear();
  512. if (!str)
  513. return;
  514. #ifdef WIN32
  515. while (*str)
  516. {
  517. unsigned unicodeChar = DecodeUTF16(str);
  518. char* dest = temp;
  519. EncodeUTF8(dest, unicodeChar);
  520. *dest = 0;
  521. Append(temp);
  522. }
  523. #else
  524. while (*str)
  525. {
  526. char* dest = temp;
  527. EncodeUTF8(dest, *str++);
  528. *dest = 0;
  529. Append(temp);
  530. }
  531. #endif
  532. }
  533. unsigned String::LengthUTF8() const
  534. {
  535. unsigned ret = 0;
  536. const char* src = buffer_;
  537. if (!src)
  538. return ret;
  539. const char* end = buffer_ + length_;
  540. while (src < end)
  541. {
  542. DecodeUTF8(src);
  543. ++ret;
  544. }
  545. return ret;
  546. }
  547. unsigned String::ByteOffsetUTF8(unsigned index) const
  548. {
  549. unsigned byteOffset = 0;
  550. unsigned utfPos = 0;
  551. while (utfPos < index && byteOffset < length_)
  552. {
  553. NextUTF8Char(byteOffset);
  554. ++utfPos;
  555. }
  556. return byteOffset;
  557. }
  558. unsigned String::NextUTF8Char(unsigned& byteOffset) const
  559. {
  560. if (!buffer_)
  561. return 0;
  562. const char* src = buffer_ + byteOffset;
  563. unsigned ret = DecodeUTF8(src);
  564. byteOffset = src - buffer_;
  565. return ret;
  566. }
  567. unsigned String::AtUTF8(unsigned index) const
  568. {
  569. unsigned byteOffset = ByteOffsetUTF8(index);
  570. return NextUTF8Char(byteOffset);
  571. }
  572. void String::ReplaceUTF8(unsigned index, unsigned unicodeChar)
  573. {
  574. unsigned utfPos = 0;
  575. unsigned byteOffset = 0;
  576. while (utfPos < index && byteOffset < length_)
  577. {
  578. NextUTF8Char(byteOffset);
  579. ++utfPos;
  580. }
  581. if (utfPos < index)
  582. return;
  583. unsigned beginCharPos = byteOffset;
  584. NextUTF8Char(byteOffset);
  585. char temp[7];
  586. char* dest = temp;
  587. EncodeUTF8(dest, unicodeChar);
  588. *dest = 0;
  589. Replace(beginCharPos, byteOffset - beginCharPos, temp, dest - temp);
  590. }
  591. void String::AppendUTF8(unsigned unicodeChar)
  592. {
  593. char temp[7];
  594. char* dest = temp;
  595. EncodeUTF8(dest, unicodeChar);
  596. *dest = 0;
  597. Append(temp);
  598. }
  599. String String::SubstringUTF8(unsigned pos) const
  600. {
  601. unsigned utf8Length = LengthUTF8();
  602. unsigned byteOffset = ByteOffsetUTF8(pos);
  603. String ret;
  604. while (pos < utf8Length)
  605. {
  606. ret.AppendUTF8(NextUTF8Char(byteOffset));
  607. ++pos;
  608. }
  609. return ret;
  610. }
  611. String String::SubstringUTF8(unsigned pos, unsigned length) const
  612. {
  613. unsigned utf8Length = LengthUTF8();
  614. unsigned byteOffset = ByteOffsetUTF8(pos);
  615. unsigned endPos = pos + length;
  616. String ret;
  617. while (pos < endPos && pos < utf8Length)
  618. {
  619. ret.AppendUTF8(NextUTF8Char(byteOffset));
  620. ++pos;
  621. }
  622. return ret;
  623. }
  624. void String::EncodeUTF8(char*& dest, unsigned unicodeChar)
  625. {
  626. if (unicodeChar < 0x80)
  627. *dest++ = unicodeChar;
  628. else if (unicodeChar < 0x800)
  629. {
  630. *dest++ = 0xc0 | ((unicodeChar >> 6) & 0x1f);
  631. *dest++ = 0x80 | (unicodeChar & 0x3f);
  632. }
  633. else if (unicodeChar < 0x10000)
  634. {
  635. *dest++ = 0xe0 | ((unicodeChar >> 12) & 0xf);
  636. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  637. *dest++ = 0x80 | (unicodeChar & 0x3f);
  638. }
  639. else if (unicodeChar < 0x200000)
  640. {
  641. *dest++ = 0xf0 | ((unicodeChar >> 18) & 0x7);
  642. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  643. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  644. *dest++ = 0x80 | (unicodeChar & 0x3f);
  645. }
  646. else if (unicodeChar < 0x4000000)
  647. {
  648. *dest++ = 0xf8 | ((unicodeChar >> 24) & 0x3);
  649. *dest++ = 0x80 | ((unicodeChar >> 18) & 0x3f);
  650. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  651. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  652. *dest++ = 0x80 | (unicodeChar & 0x3f);
  653. }
  654. else
  655. {
  656. *dest++ = 0xfc | ((unicodeChar >> 30) & 0x1);
  657. *dest++ = 0x80 | ((unicodeChar >> 24) & 0x3f);
  658. *dest++ = 0x80 | ((unicodeChar >> 18) & 0x3f);
  659. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  660. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  661. *dest++ = 0x80 | (unicodeChar & 0x3f);
  662. }
  663. }
  664. #define GET_NEXT_CONTINUATION_BYTE(ptr) *ptr; if ((unsigned char)*ptr < 0x80 || (unsigned char)*ptr >= 0xc0) return '?'; else ++ptr;
  665. unsigned String::DecodeUTF8(const char*& src)
  666. {
  667. if (src == 0)
  668. return 0;
  669. unsigned char char1 = *src++;
  670. // Check if we are in the middle of a UTF8 character
  671. if (char1 >= 0x80 && char1 < 0xc0)
  672. {
  673. while ((unsigned char)*src >= 0x80 && (unsigned char)*src < 0xc0)
  674. ++src;
  675. return '?';
  676. }
  677. if (char1 < 0x80)
  678. return char1;
  679. else if (char1 < 0xe0)
  680. {
  681. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  682. return (char2 & 0x3f) | ((char1 & 0x1f) << 6);
  683. }
  684. else if (char1 < 0xf0)
  685. {
  686. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  687. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  688. return (char3 & 0x3f) | ((char2 & 0x3f) << 6) | ((char1 & 0xf) << 12);
  689. }
  690. else if (char1 < 0xf8)
  691. {
  692. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  693. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  694. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  695. return (char4 & 0x3f) | ((char3 & 0x3f) << 6) | ((char2 & 0x3f) << 12) | ((char1 & 0x7) << 18);
  696. }
  697. else if (char1 < 0xfc)
  698. {
  699. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  700. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  701. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  702. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  703. return (char5 & 0x3f) | ((char4 & 0x3f) << 6) | ((char3 & 0x3f) << 12) | ((char2 & 0x3f) << 18) | ((char1 & 0x3) << 24);
  704. }
  705. else
  706. {
  707. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  708. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  709. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  710. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  711. unsigned char char6 = GET_NEXT_CONTINUATION_BYTE(src);
  712. return (char6 & 0x3f) | ((char5 & 0x3f) << 6) | ((char4 & 0x3f) << 12) | ((char3 & 0x3f) << 18) | ((char2 & 0x3f) << 24) |
  713. ((char1 & 0x1) << 30);
  714. }
  715. }
  716. #ifdef WIN32
  717. void String::EncodeUTF16(wchar_t*& dest, unsigned unicodeChar)
  718. {
  719. if (unicodeChar < 0x10000)
  720. *dest++ = unicodeChar;
  721. else
  722. {
  723. unicodeChar -= 0x10000;
  724. *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
  725. *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
  726. }
  727. }
  728. unsigned String::DecodeUTF16(const wchar_t*& src)
  729. {
  730. if (src == 0)
  731. return 0;
  732. unsigned short word1 = *src;
  733. // Check if we are at a low surrogate
  734. word1 = *src++;
  735. if (word1 >= 0xdc00 && word1 < 0xe000)
  736. {
  737. while (*src >= 0xdc00 && *src < 0xe000)
  738. ++src;
  739. return '?';
  740. }
  741. if (word1 < 0xd800 || word1 >= 0xe00)
  742. return word1;
  743. else
  744. {
  745. unsigned short word2 = *src++;
  746. if (word2 < 0xdc00 || word2 >= 0xe000)
  747. {
  748. --src;
  749. return '?';
  750. }
  751. else
  752. return ((word1 & 0x3ff) << 10) | (word2 & 0x3ff) | 0x10000;
  753. }
  754. }
  755. #endif
  756. Vector<String> String::Split(const char* str, char separator)
  757. {
  758. Vector<String> ret;
  759. unsigned pos = 0;
  760. unsigned length = CStringLength(str);
  761. while (pos < length)
  762. {
  763. if (str[pos] != separator)
  764. break;
  765. ++pos;
  766. }
  767. while (pos < length)
  768. {
  769. unsigned start = pos;
  770. while (start < length)
  771. {
  772. if (str[start] == separator)
  773. break;
  774. ++start;
  775. }
  776. if (start == length)
  777. {
  778. ret.Push(String(&str[pos]));
  779. break;
  780. }
  781. unsigned end = start;
  782. while (end < length)
  783. {
  784. if (str[end] != separator)
  785. break;
  786. ++end;
  787. }
  788. ret.Push(String(&str[pos], start - pos));
  789. pos = end;
  790. }
  791. return ret;
  792. }
  793. void String::AppendWithFormat(const char* formatString, ... )
  794. {
  795. va_list args;
  796. va_start(args, formatString);
  797. AppendWithFormatArgs(formatString, args);
  798. va_end(args);
  799. }
  800. void String::AppendWithFormatArgs(const char* formatString, va_list args)
  801. {
  802. int pos = 0, lastPos = 0;
  803. int length = strlen(formatString);
  804. while (true)
  805. {
  806. // Scan the format string and find %a argument where a is one of d, f, s ...
  807. while (pos < length && formatString[pos] != '%') pos++;
  808. Append(formatString + lastPos, pos - lastPos);
  809. if (pos >= length)
  810. return;
  811. char arg = formatString[pos + 1];
  812. pos += 2;
  813. lastPos = pos;
  814. switch (arg)
  815. {
  816. // Integer
  817. case 'd':
  818. case 'i':
  819. {
  820. int arg = va_arg(args, int);
  821. Append(String(arg));
  822. break;
  823. }
  824. // Unsigned
  825. case 'u':
  826. {
  827. unsigned arg = va_arg(args, unsigned);
  828. Append(String(arg));
  829. break;
  830. }
  831. // Real
  832. case 'f':
  833. {
  834. double arg = va_arg(args, double);
  835. Append(String(arg));
  836. break;
  837. }
  838. // Character
  839. case 'c':
  840. {
  841. int arg = va_arg(args, int);
  842. Append(arg);
  843. break;
  844. }
  845. // C string
  846. case 's':
  847. {
  848. char* arg = va_arg(args, char*);
  849. Append(arg);
  850. break;
  851. }
  852. // Hex
  853. case 'x':
  854. {
  855. char buf[CONVERSION_BUFFER_LENGTH];
  856. int arg = va_arg(args, int);
  857. int arglen = ::sprintf(buf, "%x", arg);
  858. Append(buf, arglen);
  859. break;
  860. }
  861. // Pointer
  862. case 'p':
  863. {
  864. char buf[CONVERSION_BUFFER_LENGTH];
  865. int arg = va_arg(args, int);
  866. int arglen = ::sprintf(buf, "%p", reinterpret_cast<void*>(arg));
  867. Append(buf, arglen);
  868. break;
  869. }
  870. case '%':
  871. {
  872. Append("%", 1);
  873. break;
  874. }
  875. }
  876. }
  877. }
  878. int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
  879. {
  880. if (!lhs || !rhs)
  881. return lhs ? 1 : (rhs ? -1 : 0);
  882. if (caseSensitive)
  883. return strcmp(lhs, rhs);
  884. else
  885. {
  886. for (;;)
  887. {
  888. char l = tolower(*lhs);
  889. char r = tolower(*rhs);
  890. if (!l || !r)
  891. return l ? 1 : (r ? -1 : 0);
  892. if (l < r)
  893. return -1;
  894. if (l > r)
  895. return 1;
  896. ++lhs;
  897. ++rhs;
  898. }
  899. }
  900. }
  901. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  902. {
  903. int delta = (int)srcLength - (int)length;
  904. if (pos + length < length_)
  905. {
  906. if (delta < 0)
  907. {
  908. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  909. Resize(length_ + delta);
  910. }
  911. if (delta > 0)
  912. {
  913. Resize(length_ + delta);
  914. MoveRange(pos + srcLength, pos + length, length_ - pos - length - delta);
  915. }
  916. }
  917. else
  918. Resize(length_ + delta);
  919. CopyChars(buffer_ + pos, srcStart, srcLength);
  920. }
  921. WString::WString() :
  922. length_(0),
  923. buffer_(0)
  924. {
  925. }
  926. WString::WString(const String& str) :
  927. length_(0),
  928. buffer_(0)
  929. {
  930. #ifdef WIN32
  931. unsigned neededSize = 0;
  932. wchar_t temp[3];
  933. unsigned byteOffset = 0;
  934. while (byteOffset < str.Length())
  935. {
  936. wchar_t* dest = temp;
  937. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  938. neededSize += dest - temp;
  939. }
  940. Resize(neededSize);
  941. byteOffset = 0;
  942. wchar_t* dest = buffer_;
  943. while (byteOffset < str.Length())
  944. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  945. #else
  946. Resize(str.LengthUTF8());
  947. unsigned byteOffset = 0;
  948. wchar_t* dest = buffer_;
  949. while (byteOffset < str.Length())
  950. *dest++ = str.NextUTF8Char(byteOffset);
  951. #endif
  952. }
  953. WString::~WString()
  954. {
  955. delete[] buffer_;
  956. }
  957. void WString::Resize(unsigned newSize)
  958. {
  959. if (!newSize)
  960. {
  961. delete[] buffer_;
  962. buffer_ = 0;
  963. length_ = 0;
  964. }
  965. else
  966. {
  967. wchar_t* newBuffer = new wchar_t[newSize + 1];
  968. if (buffer_)
  969. memcpy(newBuffer, buffer_, length_ * sizeof(wchar_t));
  970. newBuffer[newSize] = 0;
  971. buffer_ = newBuffer;
  972. length_ = newSize;
  973. }
  974. }
  975. }