Str.cpp 31 KB

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