Str.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. //
  2. // Copyright (c) 2008-2016 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 Atomic
  30. {
  31. char String::endZero = 0;
  32. const String String::EMPTY;
  33. String::String(const WString& str) :
  34. length_(0),
  35. capacity_(0),
  36. buffer_(&endZero)
  37. {
  38. SetUTF8FromWChar(str.CString());
  39. }
  40. String::String(int value) :
  41. length_(0),
  42. capacity_(0),
  43. buffer_(&endZero)
  44. {
  45. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  46. sprintf(tempBuffer, "%d", value);
  47. *this = tempBuffer;
  48. }
  49. String::String(short value) :
  50. length_(0),
  51. capacity_(0),
  52. buffer_(&endZero)
  53. {
  54. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  55. sprintf(tempBuffer, "%d", value);
  56. *this = tempBuffer;
  57. }
  58. String::String(long value) :
  59. length_(0),
  60. capacity_(0),
  61. buffer_(&endZero)
  62. {
  63. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  64. sprintf(tempBuffer, "%ld", value);
  65. *this = tempBuffer;
  66. }
  67. String::String(long long value) :
  68. length_(0),
  69. capacity_(0),
  70. buffer_(&endZero)
  71. {
  72. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  73. sprintf(tempBuffer, "%lld", value);
  74. *this = tempBuffer;
  75. }
  76. String::String(unsigned value) :
  77. length_(0),
  78. capacity_(0),
  79. buffer_(&endZero)
  80. {
  81. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  82. sprintf(tempBuffer, "%u", value);
  83. *this = tempBuffer;
  84. }
  85. String::String(unsigned short value) :
  86. length_(0),
  87. capacity_(0),
  88. buffer_(&endZero)
  89. {
  90. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  91. sprintf(tempBuffer, "%u", value);
  92. *this = tempBuffer;
  93. }
  94. String::String(unsigned long value) :
  95. length_(0),
  96. capacity_(0),
  97. buffer_(&endZero)
  98. {
  99. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  100. sprintf(tempBuffer, "%lu", value);
  101. *this = tempBuffer;
  102. }
  103. String::String(unsigned long long value) :
  104. length_(0),
  105. capacity_(0),
  106. buffer_(&endZero)
  107. {
  108. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  109. sprintf(tempBuffer, "%llu", value);
  110. *this = tempBuffer;
  111. }
  112. String::String(float value) :
  113. length_(0),
  114. capacity_(0),
  115. buffer_(&endZero)
  116. {
  117. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  118. sprintf(tempBuffer, "%g", value);
  119. *this = tempBuffer;
  120. }
  121. String::String(double value) :
  122. length_(0),
  123. capacity_(0),
  124. buffer_(&endZero)
  125. {
  126. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  127. sprintf(tempBuffer, "%.15g", value);
  128. *this = tempBuffer;
  129. }
  130. String::String(bool value) :
  131. length_(0),
  132. capacity_(0),
  133. buffer_(&endZero)
  134. {
  135. if (value)
  136. *this = "true";
  137. else
  138. *this = "false";
  139. }
  140. String::String(char value) :
  141. length_(0),
  142. capacity_(0),
  143. buffer_(&endZero)
  144. {
  145. Resize(1);
  146. buffer_[0] = value;
  147. }
  148. String::String(char value, unsigned length) :
  149. length_(0),
  150. capacity_(0),
  151. buffer_(&endZero)
  152. {
  153. Resize(length);
  154. for (unsigned i = 0; i < length; ++i)
  155. buffer_[i] = value;
  156. }
  157. String& String::operator +=(int rhs)
  158. {
  159. return *this += String(rhs);
  160. }
  161. String& String::operator +=(short rhs)
  162. {
  163. return *this += String(rhs);
  164. }
  165. String& String::operator +=(long rhs)
  166. {
  167. return *this += String(rhs);
  168. }
  169. String& String::operator +=(long long rhs)
  170. {
  171. return *this += String(rhs);
  172. }
  173. String& String::operator +=(unsigned rhs)
  174. {
  175. return *this += String(rhs);
  176. }
  177. String& String::operator +=(unsigned short rhs)
  178. {
  179. return *this += String(rhs);
  180. }
  181. String& String::operator +=(unsigned long rhs)
  182. {
  183. return *this += String(rhs);
  184. }
  185. String& String::operator +=(unsigned long long rhs)
  186. {
  187. return *this += String(rhs);
  188. }
  189. String& String::operator +=(float rhs)
  190. {
  191. return *this += String(rhs);
  192. }
  193. String& String::operator +=(bool rhs)
  194. {
  195. return *this += String(rhs);
  196. }
  197. void String::Replace(char replaceThis, char replaceWith, bool caseSensitive)
  198. {
  199. if (caseSensitive)
  200. {
  201. for (unsigned i = 0; i < length_; ++i)
  202. {
  203. if (buffer_[i] == replaceThis)
  204. buffer_[i] = replaceWith;
  205. }
  206. }
  207. else
  208. {
  209. replaceThis = (char)tolower(replaceThis);
  210. for (unsigned i = 0; i < length_; ++i)
  211. {
  212. if (tolower(buffer_[i]) == replaceThis)
  213. buffer_[i] = replaceWith;
  214. }
  215. }
  216. }
  217. void String::Replace(const String& replaceThis, const String& replaceWith, bool caseSensitive)
  218. {
  219. unsigned nextPos = 0;
  220. while (nextPos < length_)
  221. {
  222. unsigned pos = Find(replaceThis, nextPos, caseSensitive);
  223. if (pos == NPOS)
  224. break;
  225. Replace(pos, replaceThis.length_, replaceWith);
  226. nextPos = pos + replaceWith.length_;
  227. }
  228. }
  229. void String::Replace(unsigned pos, unsigned length, const String& replaceWith)
  230. {
  231. // If substring is illegal, do nothing
  232. if (pos + length > length_)
  233. return;
  234. Replace(pos, length, replaceWith.buffer_, replaceWith.length_);
  235. }
  236. void String::Replace(unsigned pos, unsigned length, const char* replaceWith)
  237. {
  238. // If substring is illegal, do nothing
  239. if (pos + length > length_)
  240. return;
  241. Replace(pos, length, replaceWith, CStringLength(replaceWith));
  242. }
  243. String::Iterator String::Replace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
  244. {
  245. unsigned pos = (unsigned)(start - Begin());
  246. if (pos >= length_)
  247. return End();
  248. unsigned length = (unsigned)(end - start);
  249. Replace(pos, length, replaceWith);
  250. return Begin() + pos;
  251. }
  252. String String::Replaced(char replaceThis, char replaceWith, bool caseSensitive) const
  253. {
  254. String ret(*this);
  255. ret.Replace(replaceThis, replaceWith, caseSensitive);
  256. return ret;
  257. }
  258. String String::Replaced(const String& replaceThis, const String& replaceWith, bool caseSensitive) const
  259. {
  260. String ret(*this);
  261. ret.Replace(replaceThis, replaceWith, caseSensitive);
  262. return ret;
  263. }
  264. String& String::Append(const String& str)
  265. {
  266. return *this += str;
  267. }
  268. String& String::Append(const char* str)
  269. {
  270. return *this += str;
  271. }
  272. String& String::Append(char c)
  273. {
  274. return *this += c;
  275. }
  276. String& String::Append(const char* str, unsigned length)
  277. {
  278. if (str)
  279. {
  280. unsigned oldLength = length_;
  281. Resize(oldLength + length);
  282. CopyChars(&buffer_[oldLength], str, length);
  283. }
  284. return *this;
  285. }
  286. void String::Insert(unsigned pos, const String& str)
  287. {
  288. if (pos > length_)
  289. pos = length_;
  290. if (pos == length_)
  291. (*this) += str;
  292. else
  293. Replace(pos, 0, str);
  294. }
  295. void String::Insert(unsigned pos, char c)
  296. {
  297. if (pos > length_)
  298. pos = length_;
  299. if (pos == length_)
  300. (*this) += c;
  301. else
  302. {
  303. unsigned oldLength = length_;
  304. Resize(length_ + 1);
  305. MoveRange(pos + 1, pos, oldLength - pos);
  306. buffer_[pos] = c;
  307. }
  308. }
  309. String::Iterator String::Insert(const String::Iterator& dest, const String& str)
  310. {
  311. unsigned pos = (unsigned)(dest - Begin());
  312. if (pos > length_)
  313. pos = length_;
  314. Insert(pos, str);
  315. return Begin() + pos;
  316. }
  317. String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
  318. {
  319. unsigned pos = (unsigned)(dest - Begin());
  320. if (pos > length_)
  321. pos = length_;
  322. unsigned length = (unsigned)(end - start);
  323. Replace(pos, 0, &(*start), length);
  324. return Begin() + pos;
  325. }
  326. String::Iterator String::Insert(const String::Iterator& dest, char c)
  327. {
  328. unsigned pos = (unsigned)(dest - Begin());
  329. if (pos > length_)
  330. pos = length_;
  331. Insert(pos, c);
  332. return Begin() + pos;
  333. }
  334. void String::Erase(unsigned pos, unsigned length)
  335. {
  336. Replace(pos, length, String::EMPTY);
  337. }
  338. String::Iterator String::Erase(const String::Iterator& it)
  339. {
  340. unsigned pos = (unsigned)(it - Begin());
  341. if (pos >= length_)
  342. return End();
  343. Erase(pos);
  344. return Begin() + pos;
  345. }
  346. String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
  347. {
  348. unsigned pos = (unsigned)(start - Begin());
  349. if (pos >= length_)
  350. return End();
  351. unsigned length = (unsigned)(end - start);
  352. Erase(pos, length);
  353. return Begin() + pos;
  354. }
  355. void String::Resize(unsigned newLength)
  356. {
  357. if (!capacity_)
  358. {
  359. // If zero length requested, do not allocate buffer yet
  360. if (!newLength)
  361. return;
  362. // Calculate initial capacity
  363. capacity_ = newLength + 1;
  364. if (capacity_ < MIN_CAPACITY)
  365. capacity_ = MIN_CAPACITY;
  366. buffer_ = new char[capacity_];
  367. }
  368. else
  369. {
  370. if (newLength && capacity_ < newLength + 1)
  371. {
  372. // Increase the capacity with half each time it is exceeded
  373. while (capacity_ < newLength + 1)
  374. capacity_ += (capacity_ + 1) >> 1;
  375. char* newBuffer = new char[capacity_];
  376. // Move the existing data to the new buffer, then delete the old buffer
  377. if (length_)
  378. CopyChars(newBuffer, buffer_, length_);
  379. delete[] buffer_;
  380. buffer_ = newBuffer;
  381. }
  382. }
  383. buffer_[newLength] = 0;
  384. length_ = newLength;
  385. }
  386. void String::Reserve(unsigned newCapacity)
  387. {
  388. if (newCapacity < length_ + 1)
  389. newCapacity = length_ + 1;
  390. if (newCapacity == capacity_)
  391. return;
  392. char* newBuffer = new char[newCapacity];
  393. // Move the existing data to the new buffer, then delete the old buffer
  394. CopyChars(newBuffer, buffer_, length_ + 1);
  395. if (capacity_)
  396. delete[] buffer_;
  397. capacity_ = newCapacity;
  398. buffer_ = newBuffer;
  399. }
  400. void String::Compact()
  401. {
  402. if (capacity_)
  403. Reserve(length_ + 1);
  404. }
  405. void String::Clear()
  406. {
  407. Resize(0);
  408. }
  409. void String::Swap(String& str)
  410. {
  411. Atomic::Swap(length_, str.length_);
  412. Atomic::Swap(capacity_, str.capacity_);
  413. Atomic::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 == 0)
  802. return 0;
  803. unsigned char char1 = *src++;
  804. // Check if we are in the middle of a UTF8 character
  805. if (char1 >= 0x80 && char1 < 0xc0)
  806. {
  807. while ((unsigned char)*src >= 0x80 && (unsigned char)*src < 0xc0)
  808. ++src;
  809. return '?';
  810. }
  811. if (char1 < 0x80)
  812. return char1;
  813. else if (char1 < 0xe0)
  814. {
  815. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  816. return (unsigned)((char2 & 0x3f) | ((char1 & 0x1f) << 6));
  817. }
  818. else if (char1 < 0xf0)
  819. {
  820. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  821. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  822. return (unsigned)((char3 & 0x3f) | ((char2 & 0x3f) << 6) | ((char1 & 0xf) << 12));
  823. }
  824. else if (char1 < 0xf8)
  825. {
  826. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  827. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  828. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  829. return (unsigned)((char4 & 0x3f) | ((char3 & 0x3f) << 6) | ((char2 & 0x3f) << 12) | ((char1 & 0x7) << 18));
  830. }
  831. else if (char1 < 0xfc)
  832. {
  833. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  834. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  835. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  836. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  837. return (unsigned)((char5 & 0x3f) | ((char4 & 0x3f) << 6) | ((char3 & 0x3f) << 12) | ((char2 & 0x3f) << 18) |
  838. ((char1 & 0x3) << 24));
  839. }
  840. else
  841. {
  842. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  843. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  844. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  845. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  846. unsigned char char6 = GET_NEXT_CONTINUATION_BYTE(src);
  847. return (unsigned)((char6 & 0x3f) | ((char5 & 0x3f) << 6) | ((char4 & 0x3f) << 12) | ((char3 & 0x3f) << 18) |
  848. ((char2 & 0x3f) << 24) | ((char1 & 0x1) << 30));
  849. }
  850. }
  851. #ifdef _WIN32
  852. void String::EncodeUTF16(wchar_t*& dest, unsigned unicodeChar)
  853. {
  854. if (unicodeChar < 0x10000)
  855. *dest++ = unicodeChar;
  856. else
  857. {
  858. unicodeChar -= 0x10000;
  859. *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
  860. *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
  861. }
  862. }
  863. unsigned String::DecodeUTF16(const wchar_t*& src)
  864. {
  865. if (src == 0)
  866. return 0;
  867. unsigned short word1 = *src;
  868. // Check if we are at a low surrogate
  869. word1 = *src++;
  870. if (word1 >= 0xdc00 && word1 < 0xe000)
  871. {
  872. while (*src >= 0xdc00 && *src < 0xe000)
  873. ++src;
  874. return '?';
  875. }
  876. if (word1 < 0xd800 || word1 >= 0xe000)
  877. return word1;
  878. else
  879. {
  880. unsigned short word2 = *src++;
  881. if (word2 < 0xdc00 || word2 >= 0xe000)
  882. {
  883. --src;
  884. return '?';
  885. }
  886. else
  887. return (((word1 & 0x3ff) << 10) | (word2 & 0x3ff)) + 0x10000;
  888. }
  889. }
  890. #endif
  891. Vector<String> String::Split(const char* str, char separator, bool keepEmptyStrings)
  892. {
  893. Vector<String> ret;
  894. const char* strEnd = str + String::CStringLength(str);
  895. for (const char* splitEnd = str; splitEnd != strEnd; ++splitEnd)
  896. {
  897. if (*splitEnd == separator)
  898. {
  899. const ptrdiff_t splitLen = splitEnd - str;
  900. if (splitLen > 0 || keepEmptyStrings)
  901. ret.Push(String(str, splitLen));
  902. str = splitEnd + 1;
  903. }
  904. }
  905. const ptrdiff_t splitLen = strEnd - str;
  906. if (splitLen > 0 || keepEmptyStrings)
  907. ret.Push(String(str, splitLen));
  908. return ret;
  909. }
  910. String String::Joined(const Vector<String>& subStrings, const String& glue)
  911. {
  912. if (subStrings.Empty())
  913. return String();
  914. String joinedString(subStrings[0]);
  915. for (unsigned i = 1; i < subStrings.Size(); ++i)
  916. joinedString.Append(glue).Append(subStrings[i]);
  917. return joinedString;
  918. }
  919. String& String::AppendWithFormat(const char* formatString, ...)
  920. {
  921. va_list args;
  922. va_start(args, formatString);
  923. AppendWithFormatArgs(formatString, args);
  924. va_end(args);
  925. return *this;
  926. }
  927. String& String::AppendWithFormatArgs(const char* formatString, va_list args)
  928. {
  929. int pos = 0, lastPos = 0;
  930. int length = (int)strlen(formatString);
  931. while (true)
  932. {
  933. // Scan the format string and find %a argument where a is one of d, f, s ...
  934. while (pos < length && formatString[pos] != '%') pos++;
  935. Append(formatString + lastPos, (unsigned)(pos - lastPos));
  936. if (pos >= length)
  937. return *this;
  938. char format = formatString[pos + 1];
  939. pos += 2;
  940. lastPos = pos;
  941. switch (format)
  942. {
  943. // Integer
  944. case 'd':
  945. case 'i':
  946. {
  947. int arg = va_arg(args, int);
  948. Append(String(arg));
  949. break;
  950. }
  951. // Unsigned
  952. case 'u':
  953. {
  954. unsigned arg = va_arg(args, unsigned);
  955. Append(String(arg));
  956. break;
  957. }
  958. // Unsigned long
  959. case 'l':
  960. {
  961. unsigned long arg = va_arg(args, unsigned long);
  962. Append(String(arg));
  963. break;
  964. }
  965. // Real
  966. case 'f':
  967. {
  968. double arg = va_arg(args, double);
  969. Append(String(arg));
  970. break;
  971. }
  972. // Character
  973. case 'c':
  974. {
  975. int arg = va_arg(args, int);
  976. Append((char)arg);
  977. break;
  978. }
  979. // C string
  980. case 's':
  981. {
  982. char* arg = va_arg(args, char*);
  983. Append(arg);
  984. break;
  985. }
  986. // Hex
  987. case 'x':
  988. {
  989. char buf[CONVERSION_BUFFER_LENGTH];
  990. int arg = va_arg(args, int);
  991. int arglen = ::sprintf(buf, "%x", arg);
  992. Append(buf, (unsigned)arglen);
  993. break;
  994. }
  995. // Pointer
  996. case 'p':
  997. {
  998. char buf[CONVERSION_BUFFER_LENGTH];
  999. int arg = va_arg(args, int);
  1000. int arglen = ::sprintf(buf, "%p", reinterpret_cast<void*>(arg));
  1001. Append(buf, (unsigned)arglen);
  1002. break;
  1003. }
  1004. case '%':
  1005. {
  1006. Append("%", 1);
  1007. break;
  1008. }
  1009. default:
  1010. ATOMIC_LOGWARNINGF("Unsupported format specifier: '%c'", format);
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
  1016. {
  1017. if (!lhs || !rhs)
  1018. return lhs ? 1 : (rhs ? -1 : 0);
  1019. if (caseSensitive)
  1020. return strcmp(lhs, rhs);
  1021. else
  1022. {
  1023. for (;;)
  1024. {
  1025. char l = (char)tolower(*lhs);
  1026. char r = (char)tolower(*rhs);
  1027. if (!l || !r)
  1028. return l ? 1 : (r ? -1 : 0);
  1029. if (l < r)
  1030. return -1;
  1031. if (l > r)
  1032. return 1;
  1033. ++lhs;
  1034. ++rhs;
  1035. }
  1036. }
  1037. }
  1038. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  1039. {
  1040. int delta = (int)srcLength - (int)length;
  1041. if (pos + length < length_)
  1042. {
  1043. if (delta < 0)
  1044. {
  1045. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  1046. Resize(length_ + delta);
  1047. }
  1048. if (delta > 0)
  1049. {
  1050. Resize(length_ + delta);
  1051. MoveRange(pos + srcLength, pos + length, length_ - pos - length - delta);
  1052. }
  1053. }
  1054. else
  1055. Resize(length_ + delta);
  1056. CopyChars(buffer_ + pos, srcStart, srcLength);
  1057. }
  1058. WString::WString() :
  1059. length_(0),
  1060. buffer_(0)
  1061. {
  1062. }
  1063. WString::WString(const String& str) :
  1064. length_(0),
  1065. buffer_(0)
  1066. {
  1067. #ifdef _WIN32
  1068. unsigned neededSize = 0;
  1069. wchar_t temp[3];
  1070. unsigned byteOffset = 0;
  1071. while (byteOffset < str.Length())
  1072. {
  1073. wchar_t* dest = temp;
  1074. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1075. neededSize += dest - temp;
  1076. }
  1077. Resize(neededSize);
  1078. byteOffset = 0;
  1079. wchar_t* dest = buffer_;
  1080. while (byteOffset < str.Length())
  1081. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1082. #else
  1083. Resize(str.LengthUTF8());
  1084. unsigned byteOffset = 0;
  1085. wchar_t* dest = buffer_;
  1086. while (byteOffset < str.Length())
  1087. *dest++ = (wchar_t)str.NextUTF8Char(byteOffset);
  1088. #endif
  1089. }
  1090. WString::~WString()
  1091. {
  1092. delete[] buffer_;
  1093. }
  1094. void WString::Resize(unsigned newLength)
  1095. {
  1096. if (!newLength)
  1097. {
  1098. delete[] buffer_;
  1099. buffer_ = 0;
  1100. length_ = 0;
  1101. }
  1102. else
  1103. {
  1104. wchar_t* newBuffer = new wchar_t[newLength + 1];
  1105. if (buffer_)
  1106. {
  1107. unsigned copyLength = length_ < newLength ? length_ : newLength;
  1108. memcpy(newBuffer, buffer_, copyLength * sizeof(wchar_t));
  1109. delete[] buffer_;
  1110. }
  1111. newBuffer[newLength] = 0;
  1112. buffer_ = newBuffer;
  1113. length_ = newLength;
  1114. }
  1115. }
  1116. }