Str.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. //
  2. // Copyright (c) 2008-2017 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. auto 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. auto 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. auto 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. auto* 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. auto* 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, bool keepEmptyStrings) const
  476. {
  477. return Split(CString(), separator, keepEmptyStrings);
  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 == nullptr)
  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 == nullptr)
  866. return 0;
  867. unsigned short word1 = *src++;
  868. // Check if we are at a low surrogate
  869. if (word1 >= 0xdc00 && word1 < 0xe000)
  870. {
  871. while (*src >= 0xdc00 && *src < 0xe000)
  872. ++src;
  873. return '?';
  874. }
  875. if (word1 < 0xd800 || word1 >= 0xe000)
  876. return word1;
  877. else
  878. {
  879. unsigned short word2 = *src++;
  880. if (word2 < 0xdc00 || word2 >= 0xe000)
  881. {
  882. --src;
  883. return '?';
  884. }
  885. else
  886. return (((word1 & 0x3ff) << 10) | (word2 & 0x3ff)) + 0x10000;
  887. }
  888. }
  889. #endif
  890. Vector<String> String::Split(const char* str, char separator, bool keepEmptyStrings)
  891. {
  892. Vector<String> ret;
  893. const char* strEnd = str + String::CStringLength(str);
  894. for (const char* splitEnd = str; splitEnd != strEnd; ++splitEnd)
  895. {
  896. if (*splitEnd == separator)
  897. {
  898. const ptrdiff_t splitLen = splitEnd - str;
  899. if (splitLen > 0 || keepEmptyStrings)
  900. ret.Push(String(str, splitLen));
  901. str = splitEnd + 1;
  902. }
  903. }
  904. const ptrdiff_t splitLen = strEnd - str;
  905. if (splitLen > 0 || keepEmptyStrings)
  906. ret.Push(String(str, splitLen));
  907. return ret;
  908. }
  909. String String::Joined(const Vector<String>& subStrings, const String& glue)
  910. {
  911. if (subStrings.Empty())
  912. return String();
  913. String joinedString(subStrings[0]);
  914. for (unsigned i = 1; i < subStrings.Size(); ++i)
  915. joinedString.Append(glue).Append(subStrings[i]);
  916. return joinedString;
  917. }
  918. String& String::AppendWithFormat(const char* formatString, ...)
  919. {
  920. va_list args;
  921. va_start(args, formatString);
  922. AppendWithFormatArgs(formatString, args);
  923. va_end(args);
  924. return *this;
  925. }
  926. String& String::AppendWithFormatArgs(const char* formatString, va_list args)
  927. {
  928. int pos = 0, lastPos = 0;
  929. auto length = (int)strlen(formatString);
  930. while (true)
  931. {
  932. // Scan the format string and find %a argument where a is one of d, f, s ...
  933. while (pos < length && formatString[pos] != '%') pos++;
  934. Append(formatString + lastPos, (unsigned)(pos - lastPos));
  935. if (pos >= length)
  936. return *this;
  937. char format = formatString[pos + 1];
  938. pos += 2;
  939. lastPos = pos;
  940. switch (format)
  941. {
  942. // Integer
  943. case 'd':
  944. case 'i':
  945. {
  946. int arg = va_arg(args, int);
  947. Append(String(arg));
  948. break;
  949. }
  950. // Unsigned
  951. case 'u':
  952. {
  953. unsigned arg = va_arg(args, unsigned);
  954. Append(String(arg));
  955. break;
  956. }
  957. // Unsigned long
  958. case 'l':
  959. {
  960. unsigned long arg = va_arg(args, unsigned long);
  961. Append(String(arg));
  962. break;
  963. }
  964. // Real
  965. case 'f':
  966. {
  967. double arg = va_arg(args, double);
  968. Append(String(arg));
  969. break;
  970. }
  971. // Character
  972. case 'c':
  973. {
  974. int arg = va_arg(args, int);
  975. Append((char)arg);
  976. break;
  977. }
  978. // C string
  979. case 's':
  980. {
  981. char* arg = va_arg(args, char*);
  982. Append(arg);
  983. break;
  984. }
  985. // Hex
  986. case 'x':
  987. {
  988. char buf[CONVERSION_BUFFER_LENGTH];
  989. int arg = va_arg(args, int);
  990. int arglen = ::sprintf(buf, "%x", arg);
  991. Append(buf, (unsigned)arglen);
  992. break;
  993. }
  994. // Pointer
  995. case 'p':
  996. {
  997. char buf[CONVERSION_BUFFER_LENGTH];
  998. int arg = va_arg(args, int);
  999. int arglen = ::sprintf(buf, "%p", reinterpret_cast<void*>(arg));
  1000. Append(buf, (unsigned)arglen);
  1001. break;
  1002. }
  1003. case '%':
  1004. {
  1005. Append("%", 1);
  1006. break;
  1007. }
  1008. default:
  1009. URHO3D_LOGWARNINGF("Unsupported format specifier: '%c'", format);
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
  1015. {
  1016. if (!lhs || !rhs)
  1017. return lhs ? 1 : (rhs ? -1 : 0);
  1018. if (caseSensitive)
  1019. return strcmp(lhs, rhs);
  1020. else
  1021. {
  1022. for (;;)
  1023. {
  1024. auto l = (char)tolower(*lhs);
  1025. auto r = (char)tolower(*rhs);
  1026. if (!l || !r)
  1027. return l ? 1 : (r ? -1 : 0);
  1028. if (l < r)
  1029. return -1;
  1030. if (l > r)
  1031. return 1;
  1032. ++lhs;
  1033. ++rhs;
  1034. }
  1035. }
  1036. }
  1037. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  1038. {
  1039. int delta = (int)srcLength - (int)length;
  1040. if (pos + length < length_)
  1041. {
  1042. if (delta < 0)
  1043. {
  1044. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  1045. Resize(length_ + delta);
  1046. }
  1047. if (delta > 0)
  1048. {
  1049. Resize(length_ + delta);
  1050. MoveRange(pos + srcLength, pos + length, length_ - pos - length - delta);
  1051. }
  1052. }
  1053. else
  1054. Resize(length_ + delta);
  1055. CopyChars(buffer_ + pos, srcStart, srcLength);
  1056. }
  1057. WString::WString() :
  1058. length_(0),
  1059. buffer_(nullptr)
  1060. {
  1061. }
  1062. WString::WString(const String& str) :
  1063. length_(0),
  1064. buffer_(nullptr)
  1065. {
  1066. #ifdef _WIN32
  1067. unsigned neededSize = 0;
  1068. wchar_t temp[3];
  1069. unsigned byteOffset = 0;
  1070. while (byteOffset < str.Length())
  1071. {
  1072. wchar_t* dest = temp;
  1073. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1074. neededSize += dest - temp;
  1075. }
  1076. Resize(neededSize);
  1077. byteOffset = 0;
  1078. wchar_t* dest = buffer_;
  1079. while (byteOffset < str.Length())
  1080. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1081. #else
  1082. Resize(str.LengthUTF8());
  1083. unsigned byteOffset = 0;
  1084. wchar_t* dest = buffer_;
  1085. while (byteOffset < str.Length())
  1086. *dest++ = (wchar_t)str.NextUTF8Char(byteOffset);
  1087. #endif
  1088. }
  1089. WString::~WString()
  1090. {
  1091. delete[] buffer_;
  1092. }
  1093. void WString::Resize(unsigned newLength)
  1094. {
  1095. if (!newLength)
  1096. {
  1097. delete[] buffer_;
  1098. buffer_ = nullptr;
  1099. length_ = 0;
  1100. }
  1101. else
  1102. {
  1103. auto* newBuffer = new wchar_t[newLength + 1];
  1104. if (buffer_)
  1105. {
  1106. unsigned copyLength = length_ < newLength ? length_ : newLength;
  1107. memcpy(newBuffer, buffer_, copyLength * sizeof(wchar_t));
  1108. delete[] buffer_;
  1109. }
  1110. newBuffer[newLength] = 0;
  1111. buffer_ = newBuffer;
  1112. length_ = newLength;
  1113. }
  1114. }
  1115. }