String.cpp 14 KB

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