Str.cpp 31 KB

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