String.cpp 14 KB

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