Str.cpp 28 KB

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