Str.cpp 28 KB

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