Str.cpp 31 KB

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