Str.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. // Copyright (c) 2008-2023 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;
  456. i32 byte_offset = 0;
  457. while (byte_offset < Length())
  458. {
  459. c32 cp = NextUTF8Char(byte_offset);
  460. // Первые символы Юникода (0 - 127) совпадают с 7-ми битной кодировкой US-ASCII
  461. // Коды кириллицы: https://ru.wikipedia.org/wiki/Кириллица_(блок_Юникода)
  462. if ((cp >= 0x41 && cp <= 0x5a) // Английские A - Z
  463. || (cp >= 0x410 && cp <= 0x42f)) // Русские А - Я без Ё
  464. {
  465. ret.AppendUTF8(cp + 32);
  466. }
  467. else if (cp >= 0x400 && cp <= 0x40f) // Другие большие буквы кириллицы
  468. {
  469. ret.AppendUTF8(cp + 80);
  470. }
  471. else
  472. {
  473. ret.AppendUTF8(cp);
  474. }
  475. }
  476. return ret;
  477. }
  478. String String::ToUpper() const
  479. {
  480. String ret;
  481. i32 byte_offset = 0;
  482. while (byte_offset < Length())
  483. {
  484. c32 cp = NextUTF8Char(byte_offset);
  485. // Первые символы Юникода (0 - 127) совпадают с 7-ми битной кодировкой US-ASCII
  486. // Коды кириллицы: https://ru.wikipedia.org/wiki/Кириллица_(блок_Юникода)
  487. if ((cp >= 0x61 && cp <= 0x7a) // Английские a - z
  488. || (cp >= 0x430 && cp <= 0x44f)) // Русские а - я без ё
  489. {
  490. ret.AppendUTF8(cp - 32);
  491. }
  492. else if (cp >= 0x450 && cp <= 0x45f) // Другие маленькие буквы кириллицы
  493. {
  494. ret.AppendUTF8(cp - 80);
  495. }
  496. else
  497. {
  498. ret.AppendUTF8(cp);
  499. }
  500. }
  501. return ret;
  502. }
  503. Vector<String> String::Split(char separator, bool keepEmptyStrings) const
  504. {
  505. return Split(CString(), separator, keepEmptyStrings);
  506. }
  507. void String::Join(const Vector<String>& subStrings, const String& glue)
  508. {
  509. *this = Joined(subStrings, glue);
  510. }
  511. i32 String::Find(char c, i32 startPos, bool caseSensitive) const
  512. {
  513. assert(startPos >= 0);
  514. const char* buffer = GetBuffer();
  515. i32 length = Length();
  516. if (caseSensitive)
  517. {
  518. for (i32 i = startPos; i < length; ++i)
  519. {
  520. if (buffer[i] == c)
  521. return i;
  522. }
  523. }
  524. else
  525. {
  526. c = (char)tolower(c);
  527. for (i32 i = startPos; i < length; ++i)
  528. {
  529. if (tolower(buffer[i]) == c)
  530. return i;
  531. }
  532. }
  533. return NPOS;
  534. }
  535. i32 String::Find(const String& str, i32 startPos, bool caseSensitive) const
  536. {
  537. assert(startPos >= 0);
  538. i32 length = Length();
  539. i32 strLength = str.Length();
  540. if (!strLength || strLength > length)
  541. return NPOS;
  542. const char* buffer = GetBuffer();
  543. const char* strBuffer = str.GetBuffer();
  544. char first = strBuffer[0];
  545. if (!caseSensitive)
  546. first = (char)tolower(first);
  547. for (i32 i = startPos; i <= length - strLength; ++i)
  548. {
  549. char c = buffer[i];
  550. if (!caseSensitive)
  551. c = (char)tolower(c);
  552. if (c == first)
  553. {
  554. i32 skip = NPOS;
  555. bool found = true;
  556. for (i32 j = 1; j < strLength; ++j)
  557. {
  558. c = buffer[i + j];
  559. char d = strBuffer[j];
  560. if (!caseSensitive)
  561. {
  562. c = (char)tolower(c);
  563. d = (char)tolower(d);
  564. }
  565. if (skip == NPOS && c == first)
  566. skip = i + j - 1;
  567. if (c != d)
  568. {
  569. found = false;
  570. if (skip != NPOS)
  571. i = skip;
  572. break;
  573. }
  574. }
  575. if (found)
  576. return i;
  577. }
  578. }
  579. return NPOS;
  580. }
  581. i32 String::FindLast(char c, i32 startPos, bool caseSensitive) const
  582. {
  583. i32 length = Length();
  584. const char* buffer = GetBuffer();
  585. if (startPos == NPOS || startPos >= length)
  586. startPos = length - 1;
  587. if (caseSensitive)
  588. {
  589. for (i32 i = startPos; i >= 0; --i)
  590. {
  591. if (buffer[i] == c)
  592. return i;
  593. }
  594. }
  595. else
  596. {
  597. c = (char)tolower(c);
  598. for (i32 i = startPos; i >= 0; --i)
  599. {
  600. if (tolower(buffer[i]) == c)
  601. return i;
  602. }
  603. }
  604. return NPOS;
  605. }
  606. i32 String::FindLast(const String& str, i32 startPos, bool caseSensitive) const
  607. {
  608. i32 length = Length();
  609. i32 strLength = str.Length();
  610. if (!strLength || strLength > length)
  611. return NPOS;
  612. if (startPos == NPOS || startPos > length - strLength)
  613. startPos = length - strLength;
  614. const char* buffer = GetBuffer();
  615. const char* strBuffer = str.GetBuffer();
  616. char first = strBuffer[0];
  617. if (!caseSensitive)
  618. first = (char)tolower(first);
  619. for (i32 i = startPos; i >= 0; --i)
  620. {
  621. char c = buffer[i];
  622. if (!caseSensitive)
  623. c = (char)tolower(c);
  624. if (c == first)
  625. {
  626. bool found = true;
  627. for (i32 j = 1; j < strLength; ++j)
  628. {
  629. c = buffer[i + j];
  630. char d = strBuffer[j];
  631. if (!caseSensitive)
  632. {
  633. c = (char)tolower(c);
  634. d = (char)tolower(d);
  635. }
  636. if (c != d)
  637. {
  638. found = false;
  639. break;
  640. }
  641. }
  642. if (found)
  643. return i;
  644. }
  645. }
  646. return NPOS;
  647. }
  648. bool String::StartsWith(const String& str, bool caseSensitive) const
  649. {
  650. return Find(str, 0, caseSensitive) == 0;
  651. }
  652. bool String::EndsWith(const String& str, bool caseSensitive) const
  653. {
  654. i32 pos = FindLast(str, Length() - 1, caseSensitive);
  655. return pos != NPOS && pos == Length() - str.Length();
  656. }
  657. int String::Compare(const String& str, bool caseSensitive) const
  658. {
  659. return Compare(CString(), str.CString(), caseSensitive);
  660. }
  661. int String::Compare(const char* str, bool caseSensitive) const
  662. {
  663. return Compare(CString(), str, caseSensitive);
  664. }
  665. void String::SetUTF8FromLatin1(const char* str)
  666. {
  667. char temp[7];
  668. Clear();
  669. if (!str)
  670. return;
  671. while (*str)
  672. {
  673. char* dest = temp;
  674. EncodeUTF8(dest, (c32)*str++);
  675. *dest = 0;
  676. Append(temp);
  677. }
  678. }
  679. void String::SetUTF8FromWChar(const wchar_t* str)
  680. {
  681. char temp[7];
  682. Clear();
  683. if (!str)
  684. return;
  685. #ifdef _WIN32
  686. while (*str)
  687. {
  688. c32 unicodeChar = DecodeUTF16(str);
  689. char* dest = temp;
  690. EncodeUTF8(dest, unicodeChar);
  691. *dest = 0;
  692. Append(temp);
  693. }
  694. #else
  695. while (*str)
  696. {
  697. char* dest = temp;
  698. EncodeUTF8(dest, (c32)*str++);
  699. *dest = 0;
  700. Append(temp);
  701. }
  702. #endif
  703. }
  704. i32 String::LengthUTF8() const
  705. {
  706. i32 ret = 0;
  707. const char* buffer = GetBuffer();
  708. const char* src = buffer;
  709. if (!src)
  710. return ret;
  711. const char* end = buffer + Length();
  712. while (src < end)
  713. {
  714. DecodeUTF8(src);
  715. ++ret;
  716. }
  717. return ret;
  718. }
  719. i32 String::ByteOffsetUTF8(i32 index) const
  720. {
  721. i32 byteOffset = 0;
  722. i32 utfPos = 0;
  723. i32 length = Length();
  724. while (utfPos < index && byteOffset < length)
  725. {
  726. NextUTF8Char(byteOffset);
  727. ++utfPos;
  728. }
  729. return byteOffset;
  730. }
  731. c32 String::NextUTF8Char(i32& byteOffset) const
  732. {
  733. const char* buffer = GetBuffer();
  734. const char* src = buffer + byteOffset;
  735. c32 ret = DecodeUTF8(src);
  736. byteOffset = (i32)(src - buffer);
  737. return ret;
  738. }
  739. c32 String::AtUTF8(i32 index) const
  740. {
  741. i32 byteOffset = ByteOffsetUTF8(index);
  742. return NextUTF8Char(byteOffset);
  743. }
  744. void String::ReplaceUTF8(i32 index, c32 unicodeChar)
  745. {
  746. i32 utfPos = 0;
  747. i32 byteOffset = 0;
  748. i32 length = Length();
  749. while (utfPos < index && byteOffset < length)
  750. {
  751. NextUTF8Char(byteOffset);
  752. ++utfPos;
  753. }
  754. if (utfPos < index)
  755. return;
  756. i32 beginCharPos = byteOffset;
  757. NextUTF8Char(byteOffset);
  758. char temp[7];
  759. char* dest = temp;
  760. EncodeUTF8(dest, unicodeChar);
  761. *dest = 0;
  762. Replace(beginCharPos, byteOffset - beginCharPos, temp, (i32)(dest - temp));
  763. }
  764. String& String::AppendUTF8(c32 unicodeChar)
  765. {
  766. char temp[7];
  767. char* dest = temp;
  768. EncodeUTF8(dest, unicodeChar);
  769. *dest = 0;
  770. return Append(temp);
  771. }
  772. String String::SubstringUTF8(i32 pos) const
  773. {
  774. i32 utf8Length = LengthUTF8();
  775. i32 byteOffset = ByteOffsetUTF8(pos);
  776. String ret;
  777. while (pos < utf8Length)
  778. {
  779. ret.AppendUTF8(NextUTF8Char(byteOffset));
  780. ++pos;
  781. }
  782. return ret;
  783. }
  784. String String::SubstringUTF8(i32 pos, i32 length) const
  785. {
  786. i32 utf8Length = LengthUTF8();
  787. i32 byteOffset = ByteOffsetUTF8(pos);
  788. i32 endPos = pos + length;
  789. String ret;
  790. while (pos < endPos && pos < utf8Length)
  791. {
  792. ret.AppendUTF8(NextUTF8Char(byteOffset));
  793. ++pos;
  794. }
  795. return ret;
  796. }
  797. void String::EncodeUTF8(char*& dest, c32 unicodeChar)
  798. {
  799. if (unicodeChar < 0x80)
  800. *dest++ = (char)unicodeChar;
  801. else if (unicodeChar < 0x800)
  802. {
  803. dest[0] = (char)(0xc0u | ((unicodeChar >> 6u) & 0x1fu));
  804. dest[1] = (char)(0x80u | (unicodeChar & 0x3fu));
  805. dest += 2;
  806. }
  807. else if (unicodeChar < 0x10000)
  808. {
  809. dest[0] = (char)(0xe0u | ((unicodeChar >> 12u) & 0xfu));
  810. dest[1] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
  811. dest[2] = (char)(0x80u | (unicodeChar & 0x3fu));
  812. dest += 3;
  813. }
  814. else if (unicodeChar < 0x200000)
  815. {
  816. dest[0] = (char)(0xf0u | ((unicodeChar >> 18u) & 0x7u));
  817. dest[1] = (char)(0x80u | ((unicodeChar >> 12u) & 0x3fu));
  818. dest[2] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
  819. dest[3] = (char)(0x80u | (unicodeChar & 0x3fu));
  820. dest += 4;
  821. }
  822. else if (unicodeChar < 0x4000000)
  823. {
  824. dest[0] = (char)(0xf8u | ((unicodeChar >> 24u) & 0x3u));
  825. dest[1] = (char)(0x80u | ((unicodeChar >> 18u) & 0x3fu));
  826. dest[2] = (char)(0x80u | ((unicodeChar >> 12u) & 0x3fu));
  827. dest[3] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
  828. dest[4] = (char)(0x80u | (unicodeChar & 0x3fu));
  829. dest += 5;
  830. }
  831. else
  832. {
  833. dest[0] = (char)(0xfcu | ((unicodeChar >> 30u) & 0x1u));
  834. dest[1] = (char)(0x80u | ((unicodeChar >> 24u) & 0x3fu));
  835. dest[2] = (char)(0x80u | ((unicodeChar >> 18u) & 0x3fu));
  836. dest[3] = (char)(0x80u | ((unicodeChar >> 12u) & 0x3fu));
  837. dest[4] = (char)(0x80u | ((unicodeChar >> 6u) & 0x3fu));
  838. dest[5] = (char)(0x80u | (unicodeChar & 0x3fu));
  839. dest += 6;
  840. }
  841. }
  842. #define GET_NEXT_CONTINUATION_BYTE(ptr) *(ptr); if ((u8)*(ptr) < 0x80 || (u8)*(ptr) >= 0xc0) return '?'; else ++(ptr);
  843. c32 String::DecodeUTF8(const char*& src)
  844. {
  845. if (src == nullptr)
  846. return 0;
  847. u8 char1 = *src++;
  848. // Check if we are in the middle of a UTF8 character
  849. if (char1 >= 0x80 && char1 < 0xc0)
  850. {
  851. while ((u8)*src >= 0x80 && (u8)*src < 0xc0)
  852. ++src;
  853. return '?';
  854. }
  855. if (char1 < 0x80)
  856. return char1;
  857. else if (char1 < 0xe0)
  858. {
  859. u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
  860. return (c32)((char2 & 0x3fu) | ((char1 & 0x1fu) << 6u));
  861. }
  862. else if (char1 < 0xf0)
  863. {
  864. u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
  865. u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
  866. return (c32)((char3 & 0x3fu) | ((char2 & 0x3fu) << 6u) | ((char1 & 0xfu) << 12u));
  867. }
  868. else if (char1 < 0xf8)
  869. {
  870. u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
  871. u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
  872. u8 char4 = GET_NEXT_CONTINUATION_BYTE(src);
  873. return (c32)((char4 & 0x3fu) | ((char3 & 0x3fu) << 6u) | ((char2 & 0x3fu) << 12u) | ((char1 & 0x7u) << 18u));
  874. }
  875. else if (char1 < 0xfc)
  876. {
  877. u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
  878. u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
  879. u8 char4 = GET_NEXT_CONTINUATION_BYTE(src);
  880. u8 char5 = GET_NEXT_CONTINUATION_BYTE(src);
  881. return (c32)((char5 & 0x3fu) | ((char4 & 0x3fu) << 6u) | ((char3 & 0x3fu) << 12u) | ((char2 & 0x3fu) << 18u) |
  882. ((char1 & 0x3u) << 24u));
  883. }
  884. else
  885. {
  886. u8 char2 = GET_NEXT_CONTINUATION_BYTE(src);
  887. u8 char3 = GET_NEXT_CONTINUATION_BYTE(src);
  888. u8 char4 = GET_NEXT_CONTINUATION_BYTE(src);
  889. u8 char5 = GET_NEXT_CONTINUATION_BYTE(src);
  890. u8 char6 = GET_NEXT_CONTINUATION_BYTE(src);
  891. return (c32)((char6 & 0x3fu) | ((char5 & 0x3fu) << 6u) | ((char4 & 0x3fu) << 12u) | ((char3 & 0x3fu) << 18u) |
  892. ((char2 & 0x3fu) << 24u) | ((char1 & 0x1u) << 30u));
  893. }
  894. }
  895. #ifdef _WIN32
  896. void String::EncodeUTF16(wchar_t*& dest, c32 unicodeChar)
  897. {
  898. if (unicodeChar < 0x10000)
  899. *dest++ = (wchar_t)unicodeChar;
  900. else
  901. {
  902. unicodeChar -= 0x10000;
  903. *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
  904. *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
  905. }
  906. }
  907. c32 String::DecodeUTF16(const wchar_t*& src)
  908. {
  909. if (src == nullptr)
  910. return 0;
  911. u16 word1 = *src++;
  912. // Check if we are at a low surrogate
  913. if (word1 >= 0xdc00 && word1 < 0xe000)
  914. {
  915. while (*src >= 0xdc00 && *src < 0xe000)
  916. ++src;
  917. return '?';
  918. }
  919. if (word1 < 0xd800 || word1 >= 0xe000)
  920. return word1;
  921. else
  922. {
  923. u16 word2 = *src++;
  924. if (word2 < 0xdc00 || word2 >= 0xe000)
  925. {
  926. --src;
  927. return '?';
  928. }
  929. else
  930. return (((word1 & 0x3ff) << 10) | (word2 & 0x3ff)) + 0x10000;
  931. }
  932. }
  933. #endif
  934. Vector<String> String::Split(const char* str, char separator, bool keepEmptyStrings)
  935. {
  936. Vector<String> ret;
  937. const char* strEnd = str + String::CStringLength(str);
  938. for (const char* splitEnd = str; splitEnd != strEnd; ++splitEnd)
  939. {
  940. if (*splitEnd == separator)
  941. {
  942. const ptrdiff_t splitLen = splitEnd - str;
  943. if (splitLen > 0 || keepEmptyStrings)
  944. ret.Push(String(str, (i32)splitLen));
  945. str = splitEnd + 1;
  946. }
  947. }
  948. const ptrdiff_t splitLen = strEnd - str;
  949. if (splitLen > 0 || keepEmptyStrings)
  950. ret.Push(String(str, (i32)splitLen));
  951. return ret;
  952. }
  953. String String::Joined(const Vector<String>& subStrings, const String& glue)
  954. {
  955. if (subStrings.Empty())
  956. return {};
  957. i32 glueLen = glue.Length();
  958. i32 commonLen = -glueLen;
  959. for (const auto& s : subStrings)
  960. commonLen += s.Length() + glueLen;
  961. String joinedString;
  962. if (commonLen)
  963. {
  964. joinedString.Reserve(commonLen);
  965. joinedString.Resize(commonLen);
  966. const char* strGlue = glue.GetBuffer();
  967. char* dest = joinedString.GetBuffer();
  968. for (i32 i = 0; i < subStrings.Size(); ++i)
  969. {
  970. i32 l = subStrings[i].Length();
  971. if (l)
  972. {
  973. memcpy(dest, subStrings[i].GetBuffer(), l);
  974. dest += l;
  975. }
  976. if (glueLen && i != subStrings.Size() - 1)
  977. {
  978. memcpy(dest, strGlue, glueLen);
  979. dest += glueLen;
  980. }
  981. }
  982. }
  983. return joinedString;
  984. }
  985. String& String::AppendWithFormat(const char* formatString, ...)
  986. {
  987. va_list args;
  988. va_start(args, formatString);
  989. AppendWithFormatArgs(formatString, args);
  990. va_end(args);
  991. return *this;
  992. }
  993. String& String::AppendWithFormatArgs(const char* formatString, va_list args)
  994. {
  995. int pos = 0, lastPos = 0;
  996. i32 length = (i32)strlen(formatString);
  997. while (true)
  998. {
  999. // Scan the format string and find %a argument where a is one of d, f, s ...
  1000. while (pos < length && formatString[pos] != '%') pos++;
  1001. Append(formatString + lastPos, (i32)(pos - lastPos));
  1002. if (pos >= length)
  1003. return *this;
  1004. char format = formatString[pos + 1];
  1005. pos += 2;
  1006. lastPos = pos;
  1007. switch (format)
  1008. {
  1009. // Integer
  1010. case 'd':
  1011. case 'i':
  1012. {
  1013. int arg = va_arg(args, int);
  1014. Append(String(arg));
  1015. break;
  1016. }
  1017. // Unsigned
  1018. case 'u':
  1019. {
  1020. unsigned arg = va_arg(args, unsigned);
  1021. Append(String(arg));
  1022. break;
  1023. }
  1024. // Unsigned long
  1025. case 'l':
  1026. {
  1027. unsigned long arg = va_arg(args, unsigned long);
  1028. Append(String(arg));
  1029. break;
  1030. }
  1031. // Real
  1032. case 'f':
  1033. {
  1034. double arg = va_arg(args, double);
  1035. Append(String(arg));
  1036. break;
  1037. }
  1038. // Character
  1039. case 'c':
  1040. {
  1041. int arg = va_arg(args, int);
  1042. Append((char)arg);
  1043. break;
  1044. }
  1045. // C string
  1046. case 's':
  1047. {
  1048. char* arg = va_arg(args, char*);
  1049. Append(arg);
  1050. break;
  1051. }
  1052. // Hex
  1053. case 'x':
  1054. {
  1055. char buf[CONVERSION_BUFFER_LENGTH];
  1056. int arg = va_arg(args, int);
  1057. int arglen = ::sprintf(buf, "%x", arg);
  1058. Append(buf, arglen);
  1059. break;
  1060. }
  1061. // Pointer
  1062. case 'p':
  1063. {
  1064. char buf[CONVERSION_BUFFER_LENGTH];
  1065. int arg = va_arg(args, int);
  1066. int arglen = ::sprintf(buf, "%p", reinterpret_cast<void*>(arg));
  1067. Append(buf, arglen);
  1068. break;
  1069. }
  1070. case '%':
  1071. {
  1072. Append("%", 1);
  1073. break;
  1074. }
  1075. default:
  1076. URHO3D_LOGWARNINGF("Unsupported format specifier: '%c'", format);
  1077. break;
  1078. }
  1079. }
  1080. }
  1081. int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
  1082. {
  1083. if (!lhs || !rhs)
  1084. return lhs ? 1 : (rhs ? -1 : 0);
  1085. if (caseSensitive)
  1086. return strcmp(lhs, rhs);
  1087. else
  1088. {
  1089. for (;;)
  1090. {
  1091. auto l = (char)tolower(*lhs);
  1092. auto r = (char)tolower(*rhs);
  1093. if (!l || !r)
  1094. return l ? 1 : (r ? -1 : 0);
  1095. if (l < r)
  1096. return -1;
  1097. if (l > r)
  1098. return 1;
  1099. ++lhs;
  1100. ++rhs;
  1101. }
  1102. }
  1103. }
  1104. void String::Replace(i32 pos, i32 length, const char* srcStart, i32 srcLength)
  1105. {
  1106. i32 delta = srcLength - length;
  1107. i32 thisLength = Length();
  1108. if (pos + length < thisLength)
  1109. {
  1110. if (delta < 0)
  1111. {
  1112. MoveRange(pos + srcLength, pos + length, thisLength - pos - length);
  1113. Resize(thisLength + delta);
  1114. }
  1115. if (delta > 0)
  1116. {
  1117. Resize(thisLength + delta);
  1118. thisLength = Length();
  1119. MoveRange(pos + srcLength, pos + length, thisLength - pos - length - delta);
  1120. }
  1121. }
  1122. else
  1123. {
  1124. Resize(thisLength + delta);
  1125. }
  1126. CopyChars(GetBuffer() + pos, srcStart, srcLength);
  1127. }
  1128. WString::WString() :
  1129. length_(0),
  1130. buffer_(nullptr)
  1131. {
  1132. }
  1133. WString::WString(const String& str) :
  1134. length_(0),
  1135. buffer_(nullptr)
  1136. {
  1137. #ifdef _WIN32
  1138. i32 neededSize = 0;
  1139. wchar_t temp[3];
  1140. i32 byteOffset = 0;
  1141. while (byteOffset < str.Length())
  1142. {
  1143. wchar_t* dest = temp;
  1144. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1145. neededSize += (i32)(dest - temp);
  1146. }
  1147. Resize(neededSize);
  1148. byteOffset = 0;
  1149. wchar_t* dest = buffer_;
  1150. while (byteOffset < str.Length())
  1151. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1152. #else
  1153. Resize(str.LengthUTF8());
  1154. i32 byteOffset = 0;
  1155. wchar_t* dest = buffer_;
  1156. while (byteOffset < str.Length())
  1157. *dest++ = (wchar_t)str.NextUTF8Char(byteOffset);
  1158. #endif
  1159. }
  1160. WString::~WString()
  1161. {
  1162. delete[] buffer_;
  1163. }
  1164. void WString::Resize(i32 newLength)
  1165. {
  1166. if (!newLength)
  1167. {
  1168. delete[] buffer_;
  1169. buffer_ = nullptr;
  1170. length_ = 0;
  1171. }
  1172. else
  1173. {
  1174. auto* newBuffer = new wchar_t[newLength + 1];
  1175. if (buffer_)
  1176. {
  1177. i32 copyLength = length_ < newLength ? length_ : newLength;
  1178. memcpy(newBuffer, buffer_, copyLength * sizeof(wchar_t));
  1179. delete[] buffer_;
  1180. }
  1181. newBuffer[newLength] = 0;
  1182. buffer_ = newBuffer;
  1183. length_ = newLength;
  1184. }
  1185. }
  1186. }