Str.cpp 30 KB

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