Str.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  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(unsigned value) :
  57. length_(0),
  58. capacity_(0),
  59. buffer_(&endZero)
  60. {
  61. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  62. sprintf(tempBuffer, "%u", value);
  63. *this = tempBuffer;
  64. }
  65. String::String(unsigned short value) :
  66. length_(0),
  67. capacity_(0),
  68. buffer_(&endZero)
  69. {
  70. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  71. sprintf(tempBuffer, "%u", value);
  72. *this = tempBuffer;
  73. }
  74. String::String(float value) :
  75. length_(0),
  76. capacity_(0),
  77. buffer_(&endZero)
  78. {
  79. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  80. sprintf(tempBuffer, "%g", value);
  81. *this = tempBuffer;
  82. }
  83. String::String(double value) :
  84. length_(0),
  85. capacity_(0),
  86. buffer_(&endZero)
  87. {
  88. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  89. sprintf(tempBuffer, "%g", value);
  90. *this = tempBuffer;
  91. }
  92. String::String(bool value) :
  93. length_(0),
  94. capacity_(0),
  95. buffer_(&endZero)
  96. {
  97. if (value)
  98. *this = "true";
  99. else
  100. *this = "false";
  101. }
  102. String::String(char value) :
  103. length_(0),
  104. capacity_(0),
  105. buffer_(&endZero)
  106. {
  107. Resize(1);
  108. buffer_[0] = value;
  109. }
  110. String::String(char value, unsigned length) :
  111. length_(0),
  112. capacity_(0),
  113. buffer_(&endZero)
  114. {
  115. Resize(length);
  116. for (unsigned i = 0; i < length; ++i)
  117. buffer_[i] = value;
  118. }
  119. String& String::operator += (int rhs)
  120. {
  121. return *this += String(rhs);
  122. }
  123. String& String::operator += (short rhs)
  124. {
  125. return *this += String(rhs);
  126. }
  127. String& String::operator += (unsigned rhs)
  128. {
  129. return *this += String(rhs);
  130. }
  131. String& String::operator += (unsigned short rhs)
  132. {
  133. return *this += String(rhs);
  134. }
  135. String& String::operator += (float rhs)
  136. {
  137. return *this += String(rhs);
  138. }
  139. String& String::operator += (bool rhs)
  140. {
  141. return *this += String(rhs);
  142. }
  143. void String::Replace(char replaceThis, char replaceWith)
  144. {
  145. for (unsigned i = 0; i < length_; ++i)
  146. {
  147. if (buffer_[i] == replaceThis)
  148. buffer_[i] = replaceWith;
  149. }
  150. }
  151. void String::Replace(const String& replaceThis, const String& replaceWith)
  152. {
  153. unsigned nextPos = 0;
  154. while (nextPos < length_)
  155. {
  156. unsigned pos = Find(replaceThis, nextPos);
  157. if (pos == NPOS)
  158. break;
  159. Replace(pos, replaceThis.length_, replaceWith);
  160. nextPos = pos + replaceWith.length_;
  161. }
  162. }
  163. void String::Replace(unsigned pos, unsigned length, const String& str)
  164. {
  165. // If substring is illegal, do nothing
  166. if (pos + length > length_)
  167. return;
  168. Replace(pos, length, str.buffer_, str.length_);
  169. }
  170. String::Iterator String::Replace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
  171. {
  172. unsigned pos = start - Begin();
  173. if (pos >= length_)
  174. return End();
  175. unsigned length = end - start;
  176. Replace(pos, length, replaceWith);
  177. return Begin() + pos;
  178. }
  179. String String::Replaced(char replaceThis, char replaceWith) const
  180. {
  181. String ret(*this);
  182. ret.Replace(replaceThis, replaceWith);
  183. return ret;
  184. }
  185. String String::Replaced(const String& replaceThis, const String& replaceWith) const
  186. {
  187. String ret(*this);
  188. ret.Replace(replaceThis, replaceWith);
  189. return ret;
  190. }
  191. void String::Append(const String& str)
  192. {
  193. *this += str;
  194. }
  195. void String::Append(const char* str)
  196. {
  197. *this += str;
  198. }
  199. void String::Append(char c)
  200. {
  201. *this += c;
  202. }
  203. void String::Append(const char* str, unsigned length)
  204. {
  205. if (!str)
  206. return;
  207. unsigned oldLength = length_;
  208. Resize(oldLength + length);
  209. CopyChars(&buffer_[oldLength], str, length);
  210. }
  211. void String::Insert(unsigned pos, const String& str)
  212. {
  213. if (pos > length_)
  214. pos = length_;
  215. if (pos == length_)
  216. (*this) += str;
  217. else
  218. Replace(pos, 0, str);
  219. }
  220. void String::Insert(unsigned pos, char c)
  221. {
  222. if (pos > length_)
  223. pos = length_;
  224. if (pos == length_)
  225. (*this) += c;
  226. else
  227. {
  228. unsigned oldLength = length_;
  229. Resize(length_ + 1);
  230. MoveRange(pos + 1, pos, oldLength - pos);
  231. buffer_[pos] = c;
  232. }
  233. }
  234. String::Iterator String::Insert(const String::Iterator& dest, const String& str)
  235. {
  236. unsigned pos = dest - Begin();
  237. if (pos > length_)
  238. pos = length_;
  239. Insert(pos, str);
  240. return Begin() + pos;
  241. }
  242. String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
  243. {
  244. unsigned pos = dest - Begin();
  245. if (pos > length_)
  246. pos = length_;
  247. unsigned length = end - start;
  248. Replace(pos, 0, &(*start), length);
  249. return Begin() + pos;
  250. }
  251. String::Iterator String::Insert(const String::Iterator& dest, char c)
  252. {
  253. unsigned pos = dest - Begin();
  254. if (pos > length_)
  255. pos = length_;
  256. Insert(pos, c);
  257. return Begin() + pos;
  258. }
  259. void String::Erase(unsigned pos, unsigned length)
  260. {
  261. Replace(pos, length, String());
  262. }
  263. String::Iterator String::Erase(const String::Iterator& it)
  264. {
  265. unsigned pos = it - Begin();
  266. if (pos >= length_)
  267. return End();
  268. Erase(pos);
  269. return Begin() + pos;
  270. }
  271. String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
  272. {
  273. unsigned pos = start - Begin();
  274. if (pos >= length_)
  275. return End();
  276. unsigned length = end - start;
  277. Erase(pos, length);
  278. return Begin() + pos;
  279. }
  280. void String::Resize(unsigned newLength)
  281. {
  282. if (!capacity_)
  283. {
  284. // Calculate initial capacity
  285. capacity_ = newLength + 1;
  286. if (capacity_ < MIN_CAPACITY)
  287. capacity_ = MIN_CAPACITY;
  288. buffer_ = new char[capacity_];
  289. }
  290. else
  291. {
  292. if (newLength && capacity_ < newLength + 1)
  293. {
  294. // Increase the capacity with half each time it is exceeded
  295. while (capacity_ < newLength + 1)
  296. capacity_ += (capacity_ + 1) >> 1;
  297. char* newBuffer = new char[capacity_];
  298. // Move the existing data to the new buffer, then delete the old buffer
  299. if (length_)
  300. CopyChars(newBuffer, buffer_, length_);
  301. delete[] buffer_;
  302. buffer_ = newBuffer;
  303. }
  304. }
  305. buffer_[newLength] = 0;
  306. length_ = newLength;
  307. }
  308. void String::Reserve(unsigned newCapacity)
  309. {
  310. if (newCapacity < length_ + 1)
  311. newCapacity = length_ + 1;
  312. if (newCapacity == capacity_)
  313. return;
  314. char* newBuffer = new char[newCapacity];
  315. // Move the existing data to the new buffer, then delete the old buffer
  316. CopyChars(newBuffer, buffer_, length_ + 1);
  317. if (capacity_)
  318. delete[] buffer_;
  319. capacity_ = newCapacity;
  320. buffer_ = newBuffer;
  321. }
  322. void String::Compact()
  323. {
  324. if (capacity_)
  325. Reserve(length_ + 1);
  326. }
  327. void String::Clear()
  328. {
  329. Resize(0);
  330. }
  331. void String::Swap(String& str)
  332. {
  333. Urho3D::Swap(length_, str.length_);
  334. Urho3D::Swap(capacity_, str.capacity_);
  335. Urho3D::Swap(buffer_, str.buffer_);
  336. }
  337. String String::Substring(unsigned pos) const
  338. {
  339. if (pos < length_)
  340. {
  341. String ret;
  342. ret.Resize(length_ - pos);
  343. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  344. return ret;
  345. }
  346. else
  347. return String();
  348. }
  349. String String::Substring(unsigned pos, unsigned length) const
  350. {
  351. if (pos < length_)
  352. {
  353. String ret;
  354. if (pos + length > length_)
  355. length = length_ - pos;
  356. ret.Resize(length);
  357. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  358. return ret;
  359. }
  360. else
  361. return String();
  362. }
  363. String String::Trimmed() const
  364. {
  365. unsigned trimStart = 0;
  366. unsigned trimEnd = length_;
  367. while (trimStart < trimEnd)
  368. {
  369. char c = buffer_[trimStart];
  370. if (c != ' ' && c != 9)
  371. break;
  372. ++trimStart;
  373. }
  374. while (trimEnd > trimStart)
  375. {
  376. char c = buffer_[trimEnd - 1];
  377. if (c != ' ' && c != 9)
  378. break;
  379. --trimEnd;
  380. }
  381. return Substring(trimStart, trimEnd - trimStart);
  382. }
  383. String String::ToLower() const
  384. {
  385. String ret(*this);
  386. for (unsigned i = 0; i < ret.length_; ++i)
  387. ret[i] = tolower(buffer_[i]);
  388. return ret;
  389. }
  390. String String::ToUpper() const
  391. {
  392. String ret(*this);
  393. for (unsigned i = 0; i < ret.length_; ++i)
  394. ret[i] = toupper(buffer_[i]);
  395. return ret;
  396. }
  397. Vector<String> String::Split(char separator) const
  398. {
  399. return Split(CString(), separator);
  400. }
  401. unsigned String::Find(char c, unsigned startPos) const
  402. {
  403. for (unsigned i = startPos; i < length_; ++i)
  404. {
  405. if (buffer_[i] == c)
  406. return i;
  407. }
  408. return NPOS;
  409. }
  410. unsigned String::Find(const String& str, unsigned startPos) const
  411. {
  412. if (!str.length_ || str.length_ > length_)
  413. return NPOS;
  414. char first = str.buffer_[0];
  415. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  416. {
  417. if (buffer_[i] == first)
  418. {
  419. unsigned skip = NPOS;
  420. bool found = true;
  421. for (unsigned j = 1; j < str.length_; ++j)
  422. {
  423. char c = buffer_[i + j];
  424. if (skip == NPOS && c == first)
  425. skip = i + j - 1;
  426. if (c != str.buffer_[j])
  427. {
  428. found = false;
  429. if (skip != NPOS)
  430. i = skip;
  431. break;
  432. }
  433. }
  434. if (found)
  435. return i;
  436. }
  437. }
  438. return NPOS;
  439. }
  440. unsigned String::FindLast(char c, unsigned startPos) const
  441. {
  442. if (startPos >= length_)
  443. startPos = length_ - 1;
  444. for (unsigned i = startPos; i < length_; --i)
  445. {
  446. if (buffer_[i] == c)
  447. return i;
  448. }
  449. return NPOS;
  450. }
  451. unsigned String::FindLast(const String& str, unsigned startPos) const
  452. {
  453. if (!str.length_ || str.length_ > length_)
  454. return NPOS;
  455. if (startPos > length_ - str.length_)
  456. startPos = length_ - str.length_;
  457. char first = str.buffer_[0];
  458. for (unsigned i = startPos; i < length_; --i)
  459. {
  460. if (buffer_[i] == first)
  461. {
  462. bool found = true;
  463. for (unsigned j = 1; j < str.length_; ++j)
  464. {
  465. char c = buffer_[i + j];
  466. if (c != str.buffer_[j])
  467. {
  468. found = false;
  469. break;
  470. }
  471. }
  472. if (found)
  473. return i;
  474. }
  475. }
  476. return NPOS;
  477. }
  478. bool String::StartsWith(const String& str) const
  479. {
  480. return Find(str) == 0;
  481. }
  482. bool String::EndsWith(const String& str) const
  483. {
  484. return FindLast(str) == Length() - str.Length();
  485. }
  486. int String::Compare(const String& str, bool caseSensitive) const
  487. {
  488. return Compare(CString(), str.CString(), caseSensitive);
  489. }
  490. int String::Compare(const char* str, bool caseSensitive) const
  491. {
  492. return Compare(CString(), str, caseSensitive);
  493. }
  494. void String::SetUTF8FromLatin1(const char* str)
  495. {
  496. char temp[7];
  497. Clear();
  498. if (!str)
  499. return;
  500. while (*str)
  501. {
  502. char* dest = temp;
  503. EncodeUTF8(dest, *str++);
  504. *dest = 0;
  505. Append(temp);
  506. }
  507. }
  508. void String::SetUTF8FromWChar(const wchar_t* str)
  509. {
  510. char temp[7];
  511. Clear();
  512. if (!str)
  513. return;
  514. #ifdef WIN32
  515. while (*str)
  516. {
  517. unsigned unicodeChar = DecodeUTF16(str);
  518. char* dest = temp;
  519. EncodeUTF8(dest, unicodeChar);
  520. *dest = 0;
  521. Append(temp);
  522. }
  523. #else
  524. while (*str)
  525. {
  526. char* dest = temp;
  527. EncodeUTF8(dest, *str++);
  528. *dest = 0;
  529. Append(temp);
  530. }
  531. #endif
  532. }
  533. unsigned String::LengthUTF8() const
  534. {
  535. unsigned ret = 0;
  536. const char* src = buffer_;
  537. if (!src)
  538. return ret;
  539. const char* end = buffer_ + length_;
  540. while (src < end)
  541. {
  542. DecodeUTF8(src);
  543. ++ret;
  544. }
  545. return ret;
  546. }
  547. unsigned String::ByteOffsetUTF8(unsigned index) const
  548. {
  549. unsigned byteOffset = 0;
  550. unsigned utfPos = 0;
  551. while (utfPos < index && byteOffset < length_)
  552. {
  553. NextUTF8Char(byteOffset);
  554. ++utfPos;
  555. }
  556. return byteOffset;
  557. }
  558. unsigned String::NextUTF8Char(unsigned& byteOffset) const
  559. {
  560. if (!buffer_)
  561. return 0;
  562. const char* src = buffer_ + byteOffset;
  563. unsigned ret = DecodeUTF8(src);
  564. byteOffset = src - buffer_;
  565. return ret;
  566. }
  567. unsigned String::AtUTF8(unsigned index) const
  568. {
  569. unsigned byteOffset = ByteOffsetUTF8(index);
  570. return NextUTF8Char(byteOffset);
  571. }
  572. void String::ReplaceUTF8(unsigned index, unsigned unicodeChar)
  573. {
  574. unsigned utfPos = 0;
  575. unsigned byteOffset = 0;
  576. while (utfPos < index && byteOffset < length_)
  577. {
  578. NextUTF8Char(byteOffset);
  579. ++utfPos;
  580. }
  581. if (utfPos < index)
  582. return;
  583. unsigned beginCharPos = byteOffset;
  584. NextUTF8Char(byteOffset);
  585. char temp[7];
  586. char* dest = temp;
  587. EncodeUTF8(dest, unicodeChar);
  588. *dest = 0;
  589. Replace(beginCharPos, byteOffset - beginCharPos, temp, dest - temp);
  590. }
  591. void String::AppendUTF8(unsigned unicodeChar)
  592. {
  593. char temp[7];
  594. char* dest = temp;
  595. EncodeUTF8(dest, unicodeChar);
  596. *dest = 0;
  597. Append(temp);
  598. }
  599. String String::SubstringUTF8(unsigned pos) const
  600. {
  601. unsigned utf8Length = LengthUTF8();
  602. unsigned byteOffset = ByteOffsetUTF8(pos);
  603. String ret;
  604. while (pos < utf8Length)
  605. {
  606. ret.AppendUTF8(NextUTF8Char(byteOffset));
  607. ++pos;
  608. }
  609. return ret;
  610. }
  611. String String::SubstringUTF8(unsigned pos, unsigned length) const
  612. {
  613. unsigned utf8Length = LengthUTF8();
  614. unsigned byteOffset = ByteOffsetUTF8(pos);
  615. unsigned endPos = pos + length;
  616. String ret;
  617. while (pos < endPos && pos < utf8Length)
  618. {
  619. ret.AppendUTF8(NextUTF8Char(byteOffset));
  620. ++pos;
  621. }
  622. return ret;
  623. }
  624. void String::EncodeUTF8(char*& dest, unsigned unicodeChar)
  625. {
  626. if (unicodeChar < 0x80)
  627. *dest++ = unicodeChar;
  628. else if (unicodeChar < 0x800)
  629. {
  630. *dest++ = 0xc0 | ((unicodeChar >> 6) & 0x1f);
  631. *dest++ = 0x80 | (unicodeChar & 0x3f);
  632. }
  633. else if (unicodeChar < 0x10000)
  634. {
  635. *dest++ = 0xe0 | ((unicodeChar >> 12) & 0xf);
  636. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  637. *dest++ = 0x80 | (unicodeChar & 0x3f);
  638. }
  639. else if (unicodeChar < 0x200000)
  640. {
  641. *dest++ = 0xf0 | ((unicodeChar >> 18) & 0x7);
  642. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  643. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  644. *dest++ = 0x80 | (unicodeChar & 0x3f);
  645. }
  646. else if (unicodeChar < 0x4000000)
  647. {
  648. *dest++ = 0xf8 | ((unicodeChar >> 24) & 0x3);
  649. *dest++ = 0x80 | ((unicodeChar >> 18) & 0x3f);
  650. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  651. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  652. *dest++ = 0x80 | (unicodeChar & 0x3f);
  653. }
  654. else
  655. {
  656. *dest++ = 0xfc | ((unicodeChar >> 30) & 0x1);
  657. *dest++ = 0x80 | ((unicodeChar >> 24) & 0x3f);
  658. *dest++ = 0x80 | ((unicodeChar >> 18) & 0x3f);
  659. *dest++ = 0x80 | ((unicodeChar >> 12) & 0x3f);
  660. *dest++ = 0x80 | ((unicodeChar >> 6) & 0x3f);
  661. *dest++ = 0x80 | (unicodeChar & 0x3f);
  662. }
  663. }
  664. #define GET_NEXT_CONTINUATION_BYTE(ptr) *ptr; if ((unsigned char)*ptr < 0x80 || (unsigned char)*ptr >= 0xc0) return '?'; else ++ptr;
  665. unsigned String::DecodeUTF8(const char*& src)
  666. {
  667. if (src == 0)
  668. return 0;
  669. unsigned char char1 = *src++;
  670. // Check if we are in the middle of a UTF8 character
  671. if (char1 >= 0x80 && char1 < 0xc0)
  672. {
  673. while ((unsigned char)*src >= 0x80 && (unsigned char)*src < 0xc0)
  674. ++src;
  675. return '?';
  676. }
  677. if (char1 < 0x80)
  678. return char1;
  679. else if (char1 < 0xe0)
  680. {
  681. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  682. return (char2 & 0x3f) | ((char1 & 0x1f) << 6);
  683. }
  684. else if (char1 < 0xf0)
  685. {
  686. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  687. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  688. return (char3 & 0x3f) | ((char2 & 0x3f) << 6) | ((char1 & 0xf) << 12);
  689. }
  690. else if (char1 < 0xf8)
  691. {
  692. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  693. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  694. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  695. return (char4 & 0x3f) | ((char3 & 0x3f) << 6) | ((char2 & 0x3f) << 12) | ((char1 & 0x7) << 18);
  696. }
  697. else if (char1 < 0xfc)
  698. {
  699. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  700. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  701. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  702. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  703. return (char5 & 0x3f) | ((char4 & 0x3f) << 6) | ((char3 & 0x3f) << 12) | ((char2 & 0x3f) << 18) | ((char1 & 0x3) << 24);
  704. }
  705. else
  706. {
  707. unsigned char char2 = GET_NEXT_CONTINUATION_BYTE(src);
  708. unsigned char char3 = GET_NEXT_CONTINUATION_BYTE(src);
  709. unsigned char char4 = GET_NEXT_CONTINUATION_BYTE(src);
  710. unsigned char char5 = GET_NEXT_CONTINUATION_BYTE(src);
  711. unsigned char char6 = GET_NEXT_CONTINUATION_BYTE(src);
  712. return (char6 & 0x3f) | ((char5 & 0x3f) << 6) | ((char4 & 0x3f) << 12) | ((char3 & 0x3f) << 18) | ((char2 & 0x3f) << 24) |
  713. ((char1 & 0x1) << 30);
  714. }
  715. }
  716. #ifdef WIN32
  717. void String::EncodeUTF16(wchar_t*& dest, unsigned unicodeChar)
  718. {
  719. if (unicodeChar < 0x10000)
  720. *dest++ = unicodeChar;
  721. else
  722. {
  723. unicodeChar -= 0x10000;
  724. *dest++ = 0xd800 | ((unicodeChar >> 10) & 0x3ff);
  725. *dest++ = 0xdc00 | (unicodeChar & 0x3ff);
  726. }
  727. }
  728. unsigned String::DecodeUTF16(const wchar_t*& src)
  729. {
  730. if (src == 0)
  731. return 0;
  732. unsigned short word1 = *src;
  733. // Check if we are at a low surrogate
  734. word1 = *src++;
  735. if (word1 >= 0xdc00 && word1 < 0xe000)
  736. {
  737. while (*src >= 0xdc00 && *src < 0xe000)
  738. ++src;
  739. return '?';
  740. }
  741. if (word1 < 0xd800 || word1 >= 0xe00)
  742. return word1;
  743. else
  744. {
  745. unsigned short word2 = *src++;
  746. if (word2 < 0xdc00 || word2 >= 0xe000)
  747. {
  748. --src;
  749. return '?';
  750. }
  751. else
  752. return ((word1 & 0x3ff) << 10) | (word2 & 0x3ff) | 0x10000;
  753. }
  754. }
  755. #endif
  756. Vector<String> String::Split(const char* str, char separator)
  757. {
  758. Vector<String> ret;
  759. unsigned pos = 0;
  760. unsigned length = CStringLength(str);
  761. while (pos < length)
  762. {
  763. if (str[pos] != separator)
  764. break;
  765. ++pos;
  766. }
  767. while (pos < length)
  768. {
  769. unsigned start = pos;
  770. while (start < length)
  771. {
  772. if (str[start] == separator)
  773. break;
  774. ++start;
  775. }
  776. if (start == length)
  777. {
  778. ret.Push(String(&str[pos]));
  779. break;
  780. }
  781. unsigned end = start;
  782. while (end < length)
  783. {
  784. if (str[end] != separator)
  785. break;
  786. ++end;
  787. }
  788. ret.Push(String(&str[pos], start - pos));
  789. pos = end;
  790. }
  791. return ret;
  792. }
  793. int String::Compare(const char* lhs, const char* rhs, bool caseSensitive)
  794. {
  795. if (!lhs || !rhs)
  796. return lhs ? 1 : (rhs ? -1 : 0);
  797. if (caseSensitive)
  798. return strcmp(lhs, rhs);
  799. else
  800. {
  801. for (;;)
  802. {
  803. char l = tolower(*lhs);
  804. char r = tolower(*rhs);
  805. if (!l || !r)
  806. return l ? 1 : (r ? -1 : 0);
  807. if (l < r)
  808. return -1;
  809. if (l > r)
  810. return 1;
  811. ++lhs;
  812. ++rhs;
  813. }
  814. }
  815. }
  816. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  817. {
  818. int delta = (int)srcLength - (int)length;
  819. if (pos + length < length_)
  820. {
  821. if (delta < 0)
  822. {
  823. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  824. Resize(length_ + delta);
  825. }
  826. if (delta > 0)
  827. {
  828. Resize(length_ + delta);
  829. MoveRange(pos + srcLength, pos + length, length_ - pos - length - delta);
  830. }
  831. }
  832. else
  833. Resize(length_ + delta);
  834. CopyChars(buffer_ + pos, srcStart, srcLength);
  835. }
  836. WString::WString() :
  837. length_(0),
  838. buffer_(0)
  839. {
  840. }
  841. WString::WString(const String& str) :
  842. length_(0),
  843. buffer_(0)
  844. {
  845. #ifdef WIN32
  846. unsigned neededSize = 0;
  847. wchar_t temp[3];
  848. unsigned byteOffset = 0;
  849. while (byteOffset < str.Length())
  850. {
  851. wchar_t* dest = temp;
  852. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  853. neededSize += dest - temp;
  854. }
  855. Resize(neededSize);
  856. byteOffset = 0;
  857. wchar_t* dest = buffer_;
  858. while (byteOffset < str.Length())
  859. String::EncodeUTF16(dest, str.NextUTF8Char(byteOffset));
  860. #else
  861. Resize(str.LengthUTF8());
  862. unsigned byteOffset = 0;
  863. wchar_t* dest = buffer_;
  864. while (byteOffset < str.Length())
  865. *dest++ = str.NextUTF8Char(byteOffset);
  866. #endif
  867. }
  868. WString::~WString()
  869. {
  870. delete[] buffer_;
  871. }
  872. void WString::Resize(unsigned newSize)
  873. {
  874. if (!newSize)
  875. {
  876. delete[] buffer_;
  877. buffer_ = 0;
  878. length_ = 0;
  879. }
  880. else
  881. {
  882. wchar_t* newBuffer = new wchar_t[newSize + 1];
  883. if (buffer_)
  884. memcpy(newBuffer, buffer_, length_ * sizeof(wchar_t));
  885. newBuffer[newSize] = 0;
  886. buffer_ = newBuffer;
  887. length_ = newSize;
  888. }
  889. }
  890. }