String.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 "StringBase.h"
  24. #include "Swap.h"
  25. #include <cstdio>
  26. char String::endZero = 0;
  27. String::String(int value) :
  28. length_(0),
  29. capacity_(0),
  30. buffer_(&endZero)
  31. {
  32. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  33. sprintf(tempBuffer, "%d", value);
  34. *this = tempBuffer;
  35. }
  36. String::String(short value) :
  37. length_(0),
  38. capacity_(0),
  39. buffer_(&endZero)
  40. {
  41. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  42. sprintf(tempBuffer, "%d", value);
  43. *this = tempBuffer;
  44. }
  45. String::String(unsigned value) :
  46. length_(0),
  47. capacity_(0),
  48. buffer_(&endZero)
  49. {
  50. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  51. sprintf(tempBuffer, "%u", value);
  52. *this = tempBuffer;
  53. }
  54. String::String(unsigned short value) :
  55. length_(0),
  56. capacity_(0),
  57. buffer_(&endZero)
  58. {
  59. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  60. sprintf(tempBuffer, "%u", value);
  61. *this = tempBuffer;
  62. }
  63. String::String(float value) :
  64. length_(0),
  65. capacity_(0),
  66. buffer_(&endZero)
  67. {
  68. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  69. sprintf(tempBuffer, "%g", value);
  70. *this = tempBuffer;
  71. }
  72. String::String(double value) :
  73. length_(0),
  74. capacity_(0),
  75. buffer_(&endZero)
  76. {
  77. char tempBuffer[CONVERSION_BUFFER_LENGTH];
  78. sprintf(tempBuffer, "%g", value);
  79. *this = tempBuffer;
  80. }
  81. String::String(bool value) :
  82. length_(0),
  83. capacity_(0),
  84. buffer_(&endZero)
  85. {
  86. if (value)
  87. *this = "true";
  88. else
  89. *this = "false";
  90. }
  91. String::String(char value) :
  92. length_(0),
  93. capacity_(0),
  94. buffer_(&endZero)
  95. {
  96. Resize(1);
  97. buffer_[0] = value;
  98. }
  99. String::String(char value, unsigned length) :
  100. length_(0),
  101. capacity_(0),
  102. buffer_(&endZero)
  103. {
  104. Resize(length);
  105. for (unsigned i = 0; i < length; ++i)
  106. buffer_[i] = value;
  107. }
  108. String& String::operator += (int rhs)
  109. {
  110. return *this += String(rhs);
  111. }
  112. String& String::operator += (short rhs)
  113. {
  114. return *this += String(rhs);
  115. }
  116. String& String::operator += (unsigned rhs)
  117. {
  118. return *this += String(rhs);
  119. }
  120. String& String::operator += (unsigned short rhs)
  121. {
  122. return *this += String(rhs);
  123. }
  124. String& String::operator += (float rhs)
  125. {
  126. return *this += String(rhs);
  127. }
  128. String& String::operator += (bool rhs)
  129. {
  130. return *this += String(rhs);
  131. }
  132. void String::Replace(char replaceThis, char replaceWith)
  133. {
  134. for (unsigned i = 0; i < length_; ++i)
  135. {
  136. if (buffer_[i] == replaceThis)
  137. buffer_[i] = replaceWith;
  138. }
  139. }
  140. void String::Replace(const String& replaceThis, const String& replaceWith)
  141. {
  142. unsigned nextPos = 0;
  143. while (nextPos < length_)
  144. {
  145. unsigned pos = Find(replaceThis, nextPos);
  146. if (pos == NPOS)
  147. break;
  148. Replace(pos, replaceThis.length_, replaceWith);
  149. nextPos = pos + replaceWith.length_;
  150. }
  151. }
  152. void String::Replace(unsigned pos, unsigned length, const String& str)
  153. {
  154. // If substring is illegal, do nothing
  155. if (pos + length > length_)
  156. return;
  157. Replace(pos, length, str.buffer_, str.length_);
  158. }
  159. String::Iterator String::Replace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
  160. {
  161. unsigned pos = start - Begin();
  162. if (pos >= length_)
  163. return End();
  164. unsigned length = end - start;
  165. Replace(pos, length, replaceWith);
  166. return Begin() + pos;
  167. }
  168. String String::Replaced(char replaceThis, char replaceWith) const
  169. {
  170. String ret(*this);
  171. ret.Replace(replaceThis, replaceWith);
  172. return ret;
  173. }
  174. String String::Replaced(const String& replaceThis, const String& replaceWith) const
  175. {
  176. String ret(*this);
  177. ret.Replace(replaceThis, replaceWith);
  178. return ret;
  179. }
  180. void String::Append(const String& str)
  181. {
  182. *this += str;
  183. }
  184. void String::Append(const char* str)
  185. {
  186. *this += str;
  187. }
  188. void String::Append(char c)
  189. {
  190. *this += c;
  191. }
  192. void String::Append(const char* str, unsigned length)
  193. {
  194. if (!str)
  195. return;
  196. unsigned oldLength = length_;
  197. Resize(oldLength + length);
  198. CopyChars(&buffer_[oldLength], str, length);
  199. }
  200. void String::Insert(unsigned pos, const String& str)
  201. {
  202. if (pos > length_)
  203. pos = length_;
  204. if (pos == length_)
  205. (*this) += str;
  206. else
  207. Replace(pos, 0, str);
  208. }
  209. void String::Insert(unsigned pos, char c)
  210. {
  211. if (pos > length_)
  212. pos = length_;
  213. if (pos == length_)
  214. (*this) += c;
  215. else
  216. {
  217. unsigned oldLength = length_;
  218. Resize(length_ + 1);
  219. MoveRange(pos + 1, pos, oldLength - pos);
  220. buffer_[pos] = c;
  221. }
  222. }
  223. String::Iterator String::Insert(const String::Iterator& dest, const String& str)
  224. {
  225. unsigned pos = dest - Begin();
  226. if (pos > length_)
  227. pos = length_;
  228. Insert(pos, str);
  229. return Begin() + pos;
  230. }
  231. String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
  232. {
  233. unsigned pos = dest - Begin();
  234. if (pos > length_)
  235. pos = length_;
  236. unsigned length = end - start;
  237. Replace(pos, 0, &(*start), length);
  238. return Begin() + pos;
  239. }
  240. String::Iterator String::Insert(const String::Iterator& dest, char c)
  241. {
  242. unsigned pos = dest - Begin();
  243. if (pos > length_)
  244. pos = length_;
  245. Insert(pos, c);
  246. return Begin() + pos;
  247. }
  248. void String::Erase(unsigned pos, unsigned length)
  249. {
  250. Replace(pos, length, String());
  251. }
  252. String::Iterator String::Erase(const String::Iterator& it)
  253. {
  254. unsigned pos = it - Begin();
  255. if (pos >= length_)
  256. return End();
  257. Erase(pos);
  258. return Begin() + pos;
  259. }
  260. String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
  261. {
  262. unsigned pos = start - Begin();
  263. if (pos >= length_)
  264. return End();
  265. unsigned length = end - start;
  266. Erase(pos, length);
  267. return Begin() + pos;
  268. }
  269. void String::Resize(unsigned newLength)
  270. {
  271. if (!capacity_)
  272. {
  273. // Calculate initial capacity
  274. capacity_ = newLength + 1;
  275. if (capacity_ < MIN_CAPACITY)
  276. capacity_ = MIN_CAPACITY;
  277. buffer_ = new char[capacity_];
  278. }
  279. else
  280. {
  281. if (newLength && capacity_ < newLength + 1)
  282. {
  283. // Increase the capacity with half each time it is exceeded
  284. while (capacity_ < newLength + 1)
  285. capacity_ += (capacity_ + 1) >> 1;
  286. char* newBuffer = new char[capacity_];
  287. // Move the existing data to the new buffer, then delete the old buffer
  288. if (length_)
  289. CopyChars(newBuffer, buffer_, length_);
  290. delete[] buffer_;
  291. buffer_ = newBuffer;
  292. }
  293. }
  294. buffer_[newLength] = 0;
  295. length_ = newLength;
  296. }
  297. void String::Reserve(unsigned newCapacity)
  298. {
  299. if (newCapacity < length_ + 1)
  300. newCapacity = length_ + 1;
  301. if (newCapacity == capacity_)
  302. return;
  303. char* newBuffer = new char[newCapacity];
  304. // Move the existing data to the new buffer, then delete the old buffer
  305. CopyChars(newBuffer, buffer_, length_ + 1);
  306. if (capacity_)
  307. delete[] buffer_;
  308. capacity_ = newCapacity;
  309. buffer_ = newBuffer;
  310. }
  311. void String::Compact()
  312. {
  313. if (capacity_)
  314. Reserve(length_ + 1);
  315. }
  316. void String::Clear()
  317. {
  318. Resize(0);
  319. }
  320. void String::Swap(String& str)
  321. {
  322. ::Swap(length_, str.length_);
  323. ::Swap(capacity_, str.capacity_);
  324. ::Swap(buffer_, str.buffer_);
  325. }
  326. String String::Substring(unsigned pos) const
  327. {
  328. if (pos < length_)
  329. {
  330. String ret;
  331. ret.Resize(length_ - pos);
  332. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  333. return ret;
  334. }
  335. else
  336. return String();
  337. }
  338. String String::Substring(unsigned pos, unsigned length) const
  339. {
  340. if (pos < length_)
  341. {
  342. String ret;
  343. if (pos + length > length_)
  344. length = length_ - pos;
  345. ret.Resize(length);
  346. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  347. return ret;
  348. }
  349. else
  350. return String();
  351. }
  352. String String::Trimmed() const
  353. {
  354. unsigned trimStart = 0;
  355. unsigned trimEnd = length_;
  356. while (trimStart < trimEnd)
  357. {
  358. char c = buffer_[trimStart];
  359. if (c != ' ' && c != 9)
  360. break;
  361. ++trimStart;
  362. }
  363. while (trimEnd > trimStart)
  364. {
  365. char c = buffer_[trimEnd - 1];
  366. if (c != ' ' && c != 9)
  367. break;
  368. --trimEnd;
  369. }
  370. return Substring(trimStart, trimEnd - trimStart);
  371. }
  372. String String::ToLower() const
  373. {
  374. String ret(*this);
  375. for (unsigned i = 0; i < ret.length_; ++i)
  376. ret[i] = tolower(buffer_[i]);
  377. return ret;
  378. }
  379. String String::ToUpper() const
  380. {
  381. String ret(*this);
  382. for (unsigned i = 0; i < ret.length_; ++i)
  383. ret[i] = toupper(buffer_[i]);
  384. return ret;
  385. }
  386. Vector<String> String::Split(char separator) const
  387. {
  388. return Split(CString(), separator);
  389. }
  390. unsigned String::Find(char c, unsigned startPos) const
  391. {
  392. for (unsigned i = startPos; i < length_; ++i)
  393. {
  394. if (buffer_[i] == c)
  395. return i;
  396. }
  397. return NPOS;
  398. }
  399. unsigned String::Find(const String& str, unsigned startPos) const
  400. {
  401. if (!str.length_ || str.length_ > length_)
  402. return NPOS;
  403. char first = str.buffer_[0];
  404. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  405. {
  406. if (buffer_[i] == first)
  407. {
  408. unsigned skip = NPOS;
  409. bool found = true;
  410. for (unsigned j = 1; j < str.length_; ++j)
  411. {
  412. char c = buffer_[i + j];
  413. if (skip == NPOS && c == first)
  414. skip = i + j - 1;
  415. if (c != str.buffer_[j])
  416. {
  417. found = false;
  418. if (skip != NPOS)
  419. i = skip;
  420. break;
  421. }
  422. }
  423. if (found)
  424. return i;
  425. }
  426. }
  427. return NPOS;
  428. }
  429. unsigned String::FindLast(char c, unsigned startPos) const
  430. {
  431. if (startPos >= length_)
  432. startPos = length_ - 1;
  433. for (unsigned i = startPos; i < length_; --i)
  434. {
  435. if (buffer_[i] == c)
  436. return i;
  437. }
  438. return NPOS;
  439. }
  440. unsigned String::FindLast(const String& str, unsigned startPos) const
  441. {
  442. if (!str.length_ || str.length_ > length_)
  443. return NPOS;
  444. if (startPos > length_ - str.length_)
  445. startPos = length_ - str.length_;
  446. char first = str.buffer_[0];
  447. for (unsigned i = startPos; i < length_; --i)
  448. {
  449. if (buffer_[i] == first)
  450. {
  451. bool found = true;
  452. for (unsigned j = 1; j < str.length_; ++j)
  453. {
  454. char c = buffer_[i + j];
  455. if (c != str.buffer_[j])
  456. {
  457. found = false;
  458. break;
  459. }
  460. }
  461. if (found)
  462. return i;
  463. }
  464. }
  465. return NPOS;
  466. }
  467. bool String::StartsWith(const String& str) const
  468. {
  469. return Find(str) == 0;
  470. }
  471. bool String::EndsWith(const String& str) const
  472. {
  473. return Find(str) == Length() - str.Length();
  474. }
  475. int String::Compare(const String& str, bool caseSensitive) const
  476. {
  477. return Compare(str.CString(), caseSensitive);
  478. }
  479. int String::Compare(const char* str, bool caseSensitive) const
  480. {
  481. const char* lhs = CString();
  482. const char* rhs = str;
  483. if (caseSensitive)
  484. return strcmp(lhs, rhs);
  485. else
  486. {
  487. if (!lhs || !rhs)
  488. return lhs ? 1 : (rhs ? -1 : 0);
  489. for (;;)
  490. {
  491. char l = tolower(*lhs);
  492. char r = tolower(*rhs);
  493. if (!l || !r)
  494. return l ? 1 : (r ? -1 : 0);
  495. if (l < r)
  496. return -1;
  497. if (l > r)
  498. return 1;
  499. ++lhs;
  500. ++rhs;
  501. }
  502. }
  503. }
  504. Vector<String> String::Split(const char* str, char separator)
  505. {
  506. Vector<String> ret;
  507. unsigned pos = 0;
  508. unsigned length = CStringLength(str);
  509. while (pos < length)
  510. {
  511. if (str[pos] != separator)
  512. break;
  513. ++pos;
  514. }
  515. while (pos < length)
  516. {
  517. unsigned start = pos;
  518. while (start < length)
  519. {
  520. if (str[start] == separator)
  521. break;
  522. ++start;
  523. }
  524. if (start == length)
  525. {
  526. ret.Push(String(&str[pos]));
  527. break;
  528. }
  529. unsigned end = start;
  530. while (end < length)
  531. {
  532. if (str[end] != separator)
  533. break;
  534. ++end;
  535. }
  536. ret.Push(String(&str[pos], start - pos));
  537. pos = end;
  538. }
  539. return ret;
  540. }
  541. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  542. {
  543. int delta = (int)srcLength - (int)length;
  544. if (pos + length < length_)
  545. {
  546. if (delta < 0)
  547. {
  548. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  549. Resize(length_ + delta);
  550. }
  551. if (delta > 0)
  552. {
  553. Resize(length_ + delta);
  554. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  555. }
  556. }
  557. else
  558. Resize(length_ + delta);
  559. CopyChars(buffer_ + pos, srcStart, srcLength);
  560. }