Str.cpp 31 KB

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