Str.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. // If zero length requested, do not allocate buffer yet
  342. if (!newLength)
  343. return;
  344. // Calculate initial capacity
  345. capacity_ = newLength + 1;
  346. if (capacity_ < MIN_CAPACITY)
  347. capacity_ = MIN_CAPACITY;
  348. buffer_ = new char[capacity_];
  349. }
  350. else
  351. {
  352. if (newLength && capacity_ < newLength + 1)
  353. {
  354. // Increase the capacity with half each time it is exceeded
  355. while (capacity_ < newLength + 1)
  356. capacity_ += (capacity_ + 1) >> 1;
  357. char* newBuffer = new char[capacity_];
  358. // Move the existing data to the new buffer, then delete the old buffer
  359. if (length_)
  360. CopyChars(newBuffer, buffer_, length_);
  361. delete[] buffer_;
  362. buffer_ = newBuffer;
  363. }
  364. }
  365. buffer_[newLength] = 0;
  366. length_ = newLength;
  367. }
  368. void String::Reserve(unsigned newCapacity)
  369. {
  370. if (newCapacity < length_ + 1)
  371. newCapacity = length_ + 1;
  372. if (newCapacity == capacity_)
  373. return;
  374. char* newBuffer = new char[newCapacity];
  375. // Move the existing data to the new buffer, then delete the old buffer
  376. CopyChars(newBuffer, buffer_, length_ + 1);
  377. if (capacity_)
  378. delete[] buffer_;
  379. capacity_ = newCapacity;
  380. buffer_ = newBuffer;
  381. }
  382. void String::Compact()
  383. {
  384. if (capacity_)
  385. Reserve(length_ + 1);
  386. }
  387. void String::Clear()
  388. {
  389. Resize(0);
  390. }
  391. void String::Swap(String& str)
  392. {
  393. Urho3D::Swap(length_, str.length_);
  394. Urho3D::Swap(capacity_, str.capacity_);
  395. Urho3D::Swap(buffer_, str.buffer_);
  396. }
  397. String String::Substring(unsigned pos) const
  398. {
  399. if (pos < length_)
  400. {
  401. String ret;
  402. ret.Resize(length_ - pos);
  403. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  404. return ret;
  405. }
  406. else
  407. return String();
  408. }
  409. String String::Substring(unsigned pos, unsigned length) const
  410. {
  411. if (pos < length_)
  412. {
  413. String ret;
  414. if (pos + length > length_)
  415. length = length_ - pos;
  416. ret.Resize(length);
  417. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  418. return ret;
  419. }
  420. else
  421. return String();
  422. }
  423. String String::Trimmed() const
  424. {
  425. unsigned trimStart = 0;
  426. unsigned trimEnd = length_;
  427. while (trimStart < trimEnd)
  428. {
  429. char c = buffer_[trimStart];
  430. if (c != ' ' && c != 9)
  431. break;
  432. ++trimStart;
  433. }
  434. while (trimEnd > trimStart)
  435. {
  436. char c = buffer_[trimEnd - 1];
  437. if (c != ' ' && c != 9)
  438. break;
  439. --trimEnd;
  440. }
  441. return Substring(trimStart, trimEnd - trimStart);
  442. }
  443. String String::ToLower() const
  444. {
  445. String ret(*this);
  446. for (unsigned i = 0; i < ret.length_; ++i)
  447. ret[i] = tolower(buffer_[i]);
  448. return ret;
  449. }
  450. String String::ToUpper() const
  451. {
  452. String ret(*this);
  453. for (unsigned i = 0; i < ret.length_; ++i)
  454. ret[i] = toupper(buffer_[i]);
  455. return ret;
  456. }
  457. Vector<String> String::Split(char separator) const
  458. {
  459. return Split(CString(), separator);
  460. }
  461. void String::Join(const Vector<String>& subStrings, String glue)
  462. {
  463. *this = Joined(subStrings, glue);
  464. }
  465. unsigned String::Find(char c, unsigned startPos, bool caseSensitive) const
  466. {
  467. if (caseSensitive)
  468. {
  469. for (unsigned i = startPos; i < length_; ++i)
  470. {
  471. if (buffer_[i] == c)
  472. return i;
  473. }
  474. }
  475. else
  476. {
  477. c = tolower(c);
  478. for (unsigned i = startPos; i < length_; ++i)
  479. {
  480. if (tolower(buffer_[i]) == c)
  481. return i;
  482. }
  483. }
  484. return NPOS;
  485. }
  486. unsigned String::Find(const String& str, unsigned startPos, bool caseSensitive) const
  487. {
  488. if (!str.length_ || str.length_ > length_)
  489. return NPOS;
  490. char first = str.buffer_[0];
  491. if (!caseSensitive)
  492. first = tolower(first);
  493. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  494. {
  495. char c = buffer_[i];
  496. if (!caseSensitive)
  497. c = tolower(c);
  498. if (c == first)
  499. {
  500. unsigned skip = NPOS;
  501. bool found = true;
  502. for (unsigned j = 1; j < str.length_; ++j)
  503. {
  504. c = buffer_[i + j];
  505. char d = str.buffer_[j];
  506. if (!caseSensitive)
  507. {
  508. c = tolower(c);
  509. d = tolower(d);
  510. }
  511. if (skip == NPOS && c == first)
  512. skip = i + j - 1;
  513. if (c != d)
  514. {
  515. found = false;
  516. if (skip != NPOS)
  517. i = skip;
  518. break;
  519. }
  520. }
  521. if (found)
  522. return i;
  523. }
  524. }
  525. return NPOS;
  526. }
  527. unsigned String::FindLast(char c, unsigned startPos, bool caseSensitive) const
  528. {
  529. if (startPos >= length_)
  530. startPos = length_ - 1;
  531. if (caseSensitive)
  532. {
  533. for (unsigned i = startPos; i < length_; --i)
  534. {
  535. if (buffer_[i] == c)
  536. return i;
  537. }
  538. }
  539. else
  540. {
  541. c = tolower(c);
  542. for (unsigned i = startPos; i < length_; --i)
  543. {
  544. if (tolower(buffer_[i]) == c)
  545. return i;
  546. }
  547. }
  548. return NPOS;
  549. }
  550. unsigned String::FindLast(const String& str, unsigned startPos, bool caseSensitive) const
  551. {
  552. if (!str.length_ || str.length_ > length_)
  553. return NPOS;
  554. if (startPos > length_ - str.length_)
  555. startPos = length_ - str.length_;
  556. char first = str.buffer_[0];
  557. if (!caseSensitive)
  558. first = tolower(first);
  559. for (unsigned i = startPos; i < length_; --i)
  560. {
  561. char c = buffer_[i];
  562. if (!caseSensitive)
  563. c = tolower(c);
  564. if (c == first)
  565. {
  566. bool found = true;
  567. for (unsigned j = 1; j < str.length_; ++j)
  568. {
  569. c = buffer_[i + j];
  570. char d = str.buffer_[j];
  571. if (!caseSensitive)
  572. {
  573. c = tolower(c);
  574. d = tolower(d);
  575. }
  576. if (c != d)
  577. {
  578. found = false;
  579. break;
  580. }
  581. }
  582. if (found)
  583. return i;
  584. }
  585. }
  586. return NPOS;
  587. }
  588. bool String::StartsWith(const String& str, bool caseSensitive) const
  589. {
  590. return Find(str, 0, caseSensitive) == 0;
  591. }
  592. bool String::EndsWith(const String& str, bool caseSensitive) const
  593. {
  594. unsigned pos = FindLast(str, Length() - 1, caseSensitive);
  595. return pos != NPOS && pos == Length() - str.Length();
  596. }
  597. int String::Compare(const String& str, bool caseSensitive) const
  598. {
  599. return Compare(CString(), str.CString(), caseSensitive);
  600. }
  601. int String::Compare(const char* str, bool caseSensitive) const
  602. {
  603. return Compare(CString(), str, caseSensitive);
  604. }
  605. void String::SetUTF8FromLatin1(const char* str)
  606. {
  607. char temp[7];
  608. Clear();
  609. if (!str)
  610. return;
  611. while (*str)
  612. {
  613. char* dest = temp;
  614. EncodeUTF8(dest, *str++);
  615. *dest = 0;
  616. Append(temp);
  617. }
  618. }
  619. void String::SetUTF8FromWChar(const wchar_t* str)
  620. {
  621. char temp[7];
  622. Clear();
  623. if (!str)
  624. return;
  625. #ifdef WIN32
  626. while (*str)
  627. {
  628. unsigned unicodeChar = DecodeUTF16(str);
  629. char* dest = temp;
  630. EncodeUTF8(dest, unicodeChar);
  631. *dest = 0;
  632. Append(temp);
  633. }
  634. #else
  635. while (*str)
  636. {
  637. char* dest = temp;
  638. EncodeUTF8(dest, *str++);
  639. *dest = 0;
  640. Append(temp);
  641. }
  642. #endif
  643. }
  644. unsigned String::LengthUTF8() const
  645. {
  646. unsigned ret = 0;
  647. const char* src = buffer_;
  648. if (!src)
  649. return ret;
  650. const char* end = buffer_ + length_;
  651. while (src < end)
  652. {
  653. DecodeUTF8(src);
  654. ++ret;
  655. }
  656. return ret;
  657. }
  658. unsigned String::ByteOffsetUTF8(unsigned index) const
  659. {
  660. unsigned byteOffset = 0;
  661. unsigned utfPos = 0;
  662. while (utfPos < index && byteOffset < length_)
  663. {
  664. NextUTF8Char(byteOffset);
  665. ++utfPos;
  666. }
  667. return byteOffset;
  668. }
  669. unsigned String::NextUTF8Char(unsigned& byteOffset) const
  670. {
  671. if (!buffer_)
  672. return 0;
  673. const char* src = buffer_ + byteOffset;
  674. unsigned ret = DecodeUTF8(src);
  675. byteOffset = src - buffer_;
  676. return ret;
  677. }
  678. unsigned String::AtUTF8(unsigned index) const
  679. {
  680. unsigned byteOffset = ByteOffsetUTF8(index);
  681. return NextUTF8Char(byteOffset);
  682. }
  683. void String::ReplaceUTF8(unsigned index, unsigned unicodeChar)
  684. {
  685. unsigned utfPos = 0;
  686. unsigned byteOffset = 0;
  687. while (utfPos < index && byteOffset < length_)
  688. {
  689. NextUTF8Char(byteOffset);
  690. ++utfPos;
  691. }
  692. if (utfPos < index)
  693. return;
  694. unsigned beginCharPos = byteOffset;
  695. NextUTF8Char(byteOffset);
  696. char temp[7];
  697. char* dest = temp;
  698. EncodeUTF8(dest, unicodeChar);
  699. *dest = 0;
  700. Replace(beginCharPos, byteOffset - beginCharPos, temp, dest - temp);
  701. }
  702. String& String::AppendUTF8(unsigned unicodeChar)
  703. {
  704. char temp[7];
  705. char* dest = temp;
  706. EncodeUTF8(dest, unicodeChar);
  707. *dest = 0;
  708. return Append(temp);
  709. }
  710. String String::SubstringUTF8(unsigned pos) const
  711. {
  712. unsigned utf8Length = LengthUTF8();
  713. unsigned byteOffset = ByteOffsetUTF8(pos);
  714. String ret;
  715. while (pos < utf8Length)
  716. {
  717. ret.AppendUTF8(NextUTF8Char(byteOffset));
  718. ++pos;
  719. }
  720. return ret;
  721. }
  722. String String::SubstringUTF8(unsigned pos, unsigned length) const
  723. {
  724. unsigned utf8Length = LengthUTF8();
  725. unsigned byteOffset = ByteOffsetUTF8(pos);
  726. unsigned endPos = pos + length;
  727. String ret;
  728. while (pos < endPos && pos < utf8Length)
  729. {
  730. ret.AppendUTF8(NextUTF8Char(byteOffset));
  731. ++pos;
  732. }
  733. return ret;
  734. }
  735. void String::EncodeUTF8(char*& dest, unsigned unicodeChar)
  736. {
  737. if (unicodeChar < 0x80)
  738. *dest++ = unicodeChar;
  739. else if (unicodeChar < 0x800)
  740. {
  741. *dest++ = 0xc0 | ((unicodeChar >> 6) & 0x1f);
  742. *dest++ = 0x80 | (unicodeChar & 0x3f);
  743. }
  744. else if (unicodeChar < 0x10000)
  745. {
  746. *dest++ = 0xe0 | ((unicodeChar >> 12) & 0xf);
  747. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  748. *dest++ = 0x80 | (unicodeChar & 0x3f);
  749. }
  750. else if (unicodeChar < 0x200000)
  751. {
  752. *dest++ = 0xf0 | ((unicodeChar >> 18) & 0x7);
  753. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  754. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  755. *dest++ = 0x80 | (unicodeChar & 0x3f);
  756. }
  757. else if (unicodeChar < 0x4000000)
  758. {
  759. *dest++ = 0xf8 | ((unicodeChar >> 24) & 0x3);
  760. *dest++ = 0x80 | ((unicodeChar >> 18) & 0x3f);
  761. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  762. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  763. *dest++ = 0x80 | (unicodeChar & 0x3f);
  764. }
  765. else
  766. {
  767. *dest++ = 0xfc | ((unicodeChar >> 30) & 0x1);
  768. *dest++ = 0x80 | ((unicodeChar >> 24) & 0x3f);
  769. *dest++ = 0x80 | ((unicodeChar >> 18) & 0x3f);
  770. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  771. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  772. *dest++ = 0x80 | (unicodeChar & 0x3f);
  773. }
  774. }
  775. #define GET_NEXT_CONTINUATION_BYTE(ptr) *ptr; if ((unsigned char)*ptr < 0x80 || (unsigned char)*ptr >= 0xc0) return '?'; else ++ptr;
  776. unsigned String::DecodeUTF8(const char*& src)
  777. {
  778. if (src == 0)
  779. return 0;
  780. unsigned char char1 = *src++;
  781. // Check if we are in the middle of a UTF8 character
  782. if (char1 >= 0x80 && char1 < 0xc0)
  783. {
  784. while ((unsigned char)*src >= 0x80 && (unsigned char)*src < 0xc0)
  785. ++src;
  786. return '?';
  787. }
  788. if (char1 < 0x80)
  789. return char1;
  790. else if (char1 < 0xe0)
  791. {
  792. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  793. return (char2 & 0x3f) | ((char1 & 0x1f) << 6);
  794. }
  795. else if (char1 < 0xf0)
  796. {
  797. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  798. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  799. return (char3 & 0x3f) | ((char2 & 0x3f) << 6) | ((char1 & 0xf) << 12);
  800. }
  801. else if (char1 < 0xf8)
  802. {
  803. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  804. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  805. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  806. return (char4 & 0x3f) | ((char3 & 0x3f) << 6) | ((char2 & 0x3f) << 12) | ((char1 & 0x7) << 18);
  807. }
  808. else if (char1 < 0xfc)
  809. {
  810. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  811. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  812. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  813. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  814. return (char5 & 0x3f) | ((char4 & 0x3f) << 6) | ((char3 & 0x3f) << 12) | ((char2 & 0x3f) << 18) | ((char1 & 0x3) << 24);
  815. }
  816. else
  817. {
  818. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  819. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  820. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  821. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  822. unsigned char char6 = GET_NEXT_CONTINUATION_BYTE(src);
  823. return (char6 & 0x3f) | ((char5 & 0x3f) << 6) | ((char4 & 0x3f) << 12) | ((char3 & 0x3f) << 18) | ((char2 & 0x3f) << 24) |
  824. ((char1 & 0x1) << 30);
  825. }
  826. }
  827. #ifdef WIN32
  828. void String::EncodeUTF16(wchar_t*& dest, unsigned unicodeChar)
  829. {
  830. if (unicodeChar < 0x10000)
  831. *dest++ = unicodeChar;
  832. else
  833. {
  834. unicodeChar -= 0x10000;
  835. *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
  836. *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
  837. }
  838. }
  839. unsigned String::DecodeUTF16(const wchar_t*& src)
  840. {
  841. if (src == 0)
  842. return 0;
  843. unsigned short word1 = *src;
  844. // Check if we are at a low surrogate
  845. word1 = *src++;
  846. if (word1 >= 0xdc00 && word1 < 0xe000)
  847. {
  848. while (*src >= 0xdc00 && *src < 0xe000)
  849. ++src;
  850. return '?';
  851. }
  852. if (word1 < 0xd800 || word1 >= 0xe00)
  853. return word1;
  854. else
  855. {
  856. unsigned short word2 = *src++;
  857. if (word2 < 0xdc00 || word2 >= 0xe000)
  858. {
  859. --src;
  860. return '?';
  861. }
  862. else
  863. return ((word1 & 0x3ff) << 10) | (word2 & 0x3ff) | 0x10000;
  864. }
  865. }
  866. #endif
  867. Vector<String> String::Split(const char* str, char separator)
  868. {
  869. Vector<String> ret;
  870. unsigned pos = 0;
  871. unsigned length = CStringLength(str);
  872. while (pos < length)
  873. {
  874. if (str[pos] != separator)
  875. break;
  876. ++pos;
  877. }
  878. while (pos < length)
  879. {
  880. unsigned start = pos;
  881. while (start < length)
  882. {
  883. if (str[start] == separator)
  884. break;
  885. ++start;
  886. }
  887. if (start == length)
  888. {
  889. ret.Push(String(&str[pos]));
  890. break;
  891. }
  892. unsigned end = start;
  893. while (end < length)
  894. {
  895. if (str[end] != separator)
  896. break;
  897. ++end;
  898. }
  899. ret.Push(String(&str[pos], start - pos));
  900. pos = end;
  901. }
  902. return ret;
  903. }
  904. String String::Joined(const Vector<String>& subStrings, String glue)
  905. {
  906. if (subStrings.Empty())
  907. return String();
  908. String joinedString(subStrings[0]);
  909. for (unsigned i = 1; i < subStrings.Size(); ++i)
  910. joinedString.Append(glue).Append(subStrings[i]);
  911. return joinedString;
  912. }
  913. String& String::AppendWithFormat(const char* formatString, ... )
  914. {
  915. va_list args;
  916. va_start(args, formatString);
  917. AppendWithFormatArgs(formatString, args);
  918. va_end(args);
  919. return *this;
  920. }
  921. String& String::AppendWithFormatArgs(const char* formatString, va_list args)
  922. {
  923. int pos = 0, lastPos = 0;
  924. int length = strlen(formatString);
  925. while (true)
  926. {
  927. // Scan the format string and find %a argument where a is one of d, f, s ...
  928. while (pos < length && formatString[pos] != '%') pos++;
  929. Append(formatString + lastPos, pos - lastPos);
  930. if (pos >= length)
  931. return *this;
  932. char arg = formatString[pos + 1];
  933. pos += 2;
  934. lastPos = pos;
  935. switch (arg)
  936. {
  937. // Integer
  938. case 'd':
  939. case 'i':
  940. {
  941. int arg = va_arg(args, int);
  942. Append(String(arg));
  943. break;
  944. }
  945. // Unsigned
  946. case 'u':
  947. {
  948. unsigned arg = va_arg(args, unsigned);
  949. Append(String(arg));
  950. break;
  951. }
  952. // Real
  953. case 'f':
  954. {
  955. double arg = va_arg(args, double);
  956. Append(String(arg));
  957. break;
  958. }
  959. // Character
  960. case 'c':
  961. {
  962. int arg = va_arg(args, int);
  963. Append(arg);
  964. break;
  965. }
  966. // C string
  967. case 's':
  968. {
  969. char* arg = va_arg(args, char*);
  970. Append(arg);
  971. break;
  972. }
  973. // Hex
  974. case 'x':
  975. {
  976. char buf[CONVERSION_BUFFER_LENGTH];
  977. int arg = va_arg(args, int);
  978. int arglen = ::sprintf(buf, "%x", arg);
  979. Append(buf, arglen);
  980. break;
  981. }
  982. // Pointer
  983. case 'p':
  984. {
  985. char buf[CONVERSION_BUFFER_LENGTH];
  986. int arg = va_arg(args, int);
  987. int arglen = ::sprintf(buf, "%p", reinterpret_cast<void*>(arg));
  988. Append(buf, arglen);
  989. break;
  990. }
  991. case '%':
  992. {
  993. Append("%", 1);
  994. break;
  995. }
  996. }
  997. }
  998. return *this;
  999. }
  1000. int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
  1001. {
  1002. if (!lhs || !rhs)
  1003. return lhs ? 1 : (rhs ? -1 : 0);
  1004. if (caseSensitive)
  1005. return strcmp(lhs, rhs);
  1006. else
  1007. {
  1008. for (;;)
  1009. {
  1010. char l = tolower(*lhs);
  1011. char r = tolower(*rhs);
  1012. if (!l || !r)
  1013. return l ? 1 : (r ? -1 : 0);
  1014. if (l < r)
  1015. return -1;
  1016. if (l > r)
  1017. return 1;
  1018. ++lhs;
  1019. ++rhs;
  1020. }
  1021. }
  1022. }
  1023. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  1024. {
  1025. int delta = (int)srcLength - (int)length;
  1026. if (pos + length < length_)
  1027. {
  1028. if (delta < 0)
  1029. {
  1030. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  1031. Resize(length_ + delta);
  1032. }
  1033. if (delta > 0)
  1034. {
  1035. Resize(length_ + delta);
  1036. MoveRange(pos + srcLength, pos + length, length_ - pos - length - delta);
  1037. }
  1038. }
  1039. else
  1040. Resize(length_ + delta);
  1041. CopyChars(buffer_ + pos, srcStart, srcLength);
  1042. }
  1043. WString::WString() :
  1044. length_(0),
  1045. buffer_(0)
  1046. {
  1047. }
  1048. WString::WString(const String& str) :
  1049. length_(0),
  1050. buffer_(0)
  1051. {
  1052. #ifdef WIN32
  1053. unsigned neededSize = 0;
  1054. wchar_t temp[3];
  1055. unsigned byteOffset = 0;
  1056. while (byteOffset < str.Length())
  1057. {
  1058. wchar_t* dest = temp;
  1059. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1060. neededSize += dest - temp;
  1061. }
  1062. Resize(neededSize);
  1063. byteOffset = 0;
  1064. wchar_t* dest = buffer_;
  1065. while (byteOffset < str.Length())
  1066. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  1067. #else
  1068. Resize(str.LengthUTF8());
  1069. unsigned byteOffset = 0;
  1070. wchar_t* dest = buffer_;
  1071. while (byteOffset < str.Length())
  1072. *dest++ = str.NextUTF8Char(byteOffset);
  1073. #endif
  1074. }
  1075. WString::~WString()
  1076. {
  1077. delete[] buffer_;
  1078. }
  1079. void WString::Resize(unsigned newLength)
  1080. {
  1081. if (!newLength)
  1082. {
  1083. delete[] buffer_;
  1084. buffer_ = 0;
  1085. length_ = 0;
  1086. }
  1087. else
  1088. {
  1089. wchar_t* newBuffer = new wchar_t[newLength + 1];
  1090. if (buffer_)
  1091. {
  1092. unsigned copyLength = length_ < newLength ? length_ : newLength;
  1093. memcpy(newBuffer, buffer_, copyLength * sizeof(wchar_t));
  1094. delete[] buffer_;
  1095. }
  1096. newBuffer[newLength] = 0;
  1097. buffer_ = newBuffer;
  1098. length_ = newLength;
  1099. }
  1100. }
  1101. }