String.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. Vector<String> ret;
  389. unsigned pos = 0;
  390. while (pos < length_)
  391. {
  392. unsigned start = pos;
  393. while (start < length_)
  394. {
  395. if (buffer_[start] == separator)
  396. break;
  397. start++;
  398. }
  399. if (start == length_)
  400. {
  401. ret.Push(Substring(pos));
  402. break;
  403. }
  404. unsigned end = start;
  405. while (end < length_)
  406. {
  407. if (buffer_[end] != separator)
  408. break;
  409. end++;
  410. }
  411. ret.Push(Substring(pos, start - pos));
  412. pos = end;
  413. }
  414. return ret;
  415. }
  416. unsigned String::Find(char c, unsigned startPos) const
  417. {
  418. for (unsigned i = startPos; i < length_; ++i)
  419. {
  420. if (buffer_[i] == c)
  421. return i;
  422. }
  423. return NPOS;
  424. }
  425. unsigned String::Find(const String& str, unsigned startPos) const
  426. {
  427. if (!str.length_ || str.length_ > length_)
  428. return NPOS;
  429. char first = str.buffer_[0];
  430. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  431. {
  432. if (buffer_[i] == first)
  433. {
  434. unsigned skip = NPOS;
  435. bool found = true;
  436. for (unsigned j = 1; j < str.length_; ++j)
  437. {
  438. char c = buffer_[i + j];
  439. if (skip == NPOS && c == first)
  440. skip = i + j - 1;
  441. if (c != str.buffer_[j])
  442. {
  443. found = false;
  444. if (skip != NPOS)
  445. i = skip;
  446. break;
  447. }
  448. }
  449. if (found)
  450. return i;
  451. }
  452. }
  453. return NPOS;
  454. }
  455. unsigned String::FindLast(char c, unsigned startPos) const
  456. {
  457. if (startPos >= length_)
  458. startPos = length_ - 1;
  459. for (unsigned i = startPos; i < length_; --i)
  460. {
  461. if (buffer_[i] == c)
  462. return i;
  463. }
  464. return NPOS;
  465. }
  466. unsigned String::FindLast(const String& str, unsigned startPos) const
  467. {
  468. if (!str.length_ || str.length_ > length_)
  469. return NPOS;
  470. if (startPos > length_ - str.length_)
  471. startPos = length_ - str.length_;
  472. char first = str.buffer_[0];
  473. for (unsigned i = startPos; i < length_; --i)
  474. {
  475. if (buffer_[i] == first)
  476. {
  477. bool found = true;
  478. for (unsigned j = 1; j < str.length_; ++j)
  479. {
  480. char c = buffer_[i + j];
  481. if (c != str.buffer_[j])
  482. {
  483. found = false;
  484. break;
  485. }
  486. }
  487. if (found)
  488. return i;
  489. }
  490. }
  491. return NPOS;
  492. }
  493. bool String::StartsWith(const String& str) const
  494. {
  495. return Find(str) == 0;
  496. }
  497. bool String::EndsWith(const String& str) const
  498. {
  499. return Find(str) == Length() - str.Length();
  500. }
  501. int String::Compare(const String& str, bool caseSensitive) const
  502. {
  503. return Compare(str.CString(), caseSensitive);
  504. }
  505. int String::Compare(const char* str, bool caseSensitive) const
  506. {
  507. const char* lhs = CString();
  508. const char* rhs = str;
  509. if (caseSensitive)
  510. return strcmp(lhs, rhs);
  511. else
  512. {
  513. if (!lhs || !rhs)
  514. return lhs ? 1 : (rhs ? -1 : 0);
  515. for (;;)
  516. {
  517. char l = tolower(*lhs);
  518. char r = tolower(*rhs);
  519. if (!l || !r)
  520. return l ? 1 : (r ? -1 : 0);
  521. if (l < r)
  522. return -1;
  523. if (l > r)
  524. return 1;
  525. ++lhs;
  526. ++rhs;
  527. }
  528. }
  529. }
  530. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  531. {
  532. int delta = (int)srcLength - (int)length;
  533. if (pos + length < length_)
  534. {
  535. if (delta < 0)
  536. {
  537. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  538. Resize(length_ + delta);
  539. }
  540. if (delta > 0)
  541. {
  542. Resize(length_ + delta);
  543. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  544. }
  545. }
  546. else
  547. Resize(length_ + delta);
  548. CopyChars(buffer_ + pos, srcStart, srcLength);
  549. }