Str.cpp 30 KB

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