String.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. void String::Append(const String& str)
  169. {
  170. *this += str;
  171. }
  172. void String::Append(const char* str)
  173. {
  174. *this += str;
  175. }
  176. void String::Append(char c)
  177. {
  178. *this += c;
  179. }
  180. void String::Append(const char* str, unsigned length)
  181. {
  182. if (!str)
  183. return;
  184. unsigned oldLength = length_;
  185. Resize(oldLength + length);
  186. CopyChars(&buffer_[oldLength], str, length);
  187. }
  188. void String::Insert(unsigned pos, const String& str)
  189. {
  190. if (pos > length_)
  191. pos = length_;
  192. if (pos == length_)
  193. (*this) += str;
  194. else
  195. Replace(pos, 0, str);
  196. }
  197. void String::Insert(unsigned pos, char c)
  198. {
  199. if (pos > length_)
  200. pos = length_;
  201. if (pos == length_)
  202. (*this) += c;
  203. else
  204. {
  205. unsigned oldLength = length_;
  206. Resize(length_ + 1);
  207. MoveRange(pos + 1, pos, oldLength - pos);
  208. buffer_[pos] = c;
  209. }
  210. }
  211. String::Iterator String::Insert(const String::Iterator& dest, const String& str)
  212. {
  213. unsigned pos = dest - Begin();
  214. if (pos > length_)
  215. pos = length_;
  216. Insert(pos, str);
  217. return Begin() + pos;
  218. }
  219. String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
  220. {
  221. unsigned pos = dest - Begin();
  222. if (pos > length_)
  223. pos = length_;
  224. unsigned length = end - start;
  225. Replace(pos, 0, &(*start), length);
  226. return Begin() + pos;
  227. }
  228. String::Iterator String::Insert(const String::Iterator& dest, char c)
  229. {
  230. unsigned pos = dest - Begin();
  231. if (pos > length_)
  232. pos = length_;
  233. Insert(pos, c);
  234. return Begin() + pos;
  235. }
  236. void String::Erase(unsigned pos, unsigned length)
  237. {
  238. Replace(pos, length, String());
  239. }
  240. String::Iterator String::Erase(const String::Iterator& it)
  241. {
  242. unsigned pos = it - Begin();
  243. if (pos >= length_)
  244. return End();
  245. Erase(pos);
  246. return Begin() + pos;
  247. }
  248. String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
  249. {
  250. unsigned pos = start - Begin();
  251. if (pos >= length_)
  252. return End();
  253. unsigned length = end - start;
  254. Erase(pos, length);
  255. return Begin() + pos;
  256. }
  257. void String::Resize(unsigned newLength)
  258. {
  259. if (!capacity_)
  260. {
  261. // Calculate initial capacity
  262. capacity_ = newLength + 1;
  263. if (capacity_ < MIN_CAPACITY)
  264. capacity_ = MIN_CAPACITY;
  265. buffer_ = new char[capacity_];
  266. }
  267. else
  268. {
  269. if (newLength && capacity_ < newLength + 1)
  270. {
  271. // Increase the capacity with half each time it is exceeded
  272. while (capacity_ < newLength + 1)
  273. capacity_ += (capacity_ + 1) >> 1;
  274. char* newBuffer = new char[capacity_];
  275. // Move the existing data to the new buffer, then delete the old buffer
  276. if (length_)
  277. CopyChars(newBuffer, buffer_, length_);
  278. delete[] buffer_;
  279. buffer_ = newBuffer;
  280. }
  281. }
  282. buffer_[newLength] = 0;
  283. length_ = newLength;
  284. }
  285. void String::Reserve(unsigned newCapacity)
  286. {
  287. if (newCapacity < length_ + 1)
  288. newCapacity = length_ + 1;
  289. if (newCapacity == capacity_)
  290. return;
  291. char* newBuffer = new char[newCapacity];
  292. // Move the existing data to the new buffer, then delete the old buffer
  293. CopyChars(newBuffer, buffer_, length_ + 1);
  294. if (capacity_)
  295. delete[] buffer_;
  296. capacity_ = newCapacity;
  297. buffer_ = newBuffer;
  298. }
  299. void String::Compact()
  300. {
  301. if (capacity_)
  302. Reserve(length_ + 1);
  303. }
  304. void String::Clear()
  305. {
  306. Resize(0);
  307. }
  308. void String::Swap(String& str)
  309. {
  310. ::Swap(length_, str.length_);
  311. ::Swap(capacity_, str.capacity_);
  312. ::Swap(buffer_, str.buffer_);
  313. }
  314. String String::Substring(unsigned pos) const
  315. {
  316. if (pos < length_)
  317. {
  318. String ret;
  319. ret.Resize(length_ - pos);
  320. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  321. return ret;
  322. }
  323. else
  324. return String();
  325. }
  326. String String::Substring(unsigned pos, unsigned length) const
  327. {
  328. if (pos < length_)
  329. {
  330. String ret;
  331. if (pos + length > length_)
  332. length = length_ - pos;
  333. ret.Resize(length);
  334. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  335. return ret;
  336. }
  337. else
  338. return String();
  339. }
  340. String String::Trimmed() const
  341. {
  342. unsigned trimStart = 0;
  343. unsigned trimEnd = length_;
  344. while (trimStart < trimEnd)
  345. {
  346. char c = buffer_[trimStart];
  347. if (c != ' ' && c != 9)
  348. break;
  349. ++trimStart;
  350. }
  351. while (trimEnd > trimStart)
  352. {
  353. char c = buffer_[trimEnd - 1];
  354. if (c != ' ' && c != 9)
  355. break;
  356. --trimEnd;
  357. }
  358. return Substring(trimStart, trimEnd - trimStart);
  359. }
  360. String String::ToLower() const
  361. {
  362. String ret(*this);
  363. for (unsigned i = 0; i < ret.length_; ++i)
  364. ret[i] = tolower(buffer_[i]);
  365. return ret;
  366. }
  367. String String::ToUpper() const
  368. {
  369. String ret(*this);
  370. for (unsigned i = 0; i < ret.length_; ++i)
  371. ret[i] = toupper(buffer_[i]);
  372. return ret;
  373. }
  374. Vector<String> String::Split(char separator) const
  375. {
  376. Vector<String> ret;
  377. unsigned pos = 0;
  378. while (pos < length_)
  379. {
  380. unsigned start = pos;
  381. while (start < length_)
  382. {
  383. if (buffer_[start] == separator)
  384. break;
  385. start++;
  386. }
  387. if (start == length_)
  388. {
  389. ret.Push(Substring(pos));
  390. break;
  391. }
  392. unsigned end = start;
  393. while (end < length_)
  394. {
  395. if (buffer_[end] != separator)
  396. break;
  397. end++;
  398. }
  399. ret.Push(Substring(pos, start - pos));
  400. pos = end;
  401. }
  402. return ret;
  403. }
  404. unsigned String::Find(char c, unsigned startPos) const
  405. {
  406. for (unsigned i = startPos; i < length_; ++i)
  407. {
  408. if (buffer_[i] == c)
  409. return i;
  410. }
  411. return NPOS;
  412. }
  413. unsigned String::Find(const String& str, unsigned startPos) const
  414. {
  415. if (!str.length_ || str.length_ > length_)
  416. return NPOS;
  417. char first = str.buffer_[0];
  418. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  419. {
  420. if (buffer_[i] == first)
  421. {
  422. unsigned skip = NPOS;
  423. bool found = true;
  424. for (unsigned j = 1; j < str.length_; ++j)
  425. {
  426. char c = buffer_[i + j];
  427. if (skip == NPOS && c == first)
  428. skip = i + j - 1;
  429. if (c != str.buffer_[j])
  430. {
  431. found = false;
  432. if (skip != NPOS)
  433. i = skip;
  434. break;
  435. }
  436. }
  437. if (found)
  438. return i;
  439. }
  440. }
  441. return NPOS;
  442. }
  443. unsigned String::FindLast(char c, unsigned startPos) const
  444. {
  445. if (startPos >= length_)
  446. startPos = length_ - 1;
  447. for (unsigned i = startPos; i < length_; --i)
  448. {
  449. if (buffer_[i] == c)
  450. return i;
  451. }
  452. return NPOS;
  453. }
  454. unsigned String::FindLast(const String& str, unsigned startPos) const
  455. {
  456. if (!str.length_ || str.length_ > length_)
  457. return NPOS;
  458. if (startPos > length_ - str.length_)
  459. startPos = length_ - str.length_;
  460. char first = str.buffer_[0];
  461. for (unsigned i = startPos; i < length_; --i)
  462. {
  463. if (buffer_[i] == first)
  464. {
  465. bool found = true;
  466. for (unsigned j = 1; j < str.length_; ++j)
  467. {
  468. char c = buffer_[i + j];
  469. if (c != str.buffer_[j])
  470. {
  471. found = false;
  472. break;
  473. }
  474. }
  475. if (found)
  476. return i;
  477. }
  478. }
  479. return NPOS;
  480. }
  481. int String::Compare(const String& str, bool caseSensitive) const
  482. {
  483. return Compare(str.CString(), caseSensitive);
  484. }
  485. int String::Compare(const char* str, bool caseSensitive) const
  486. {
  487. const char* lhs = CString();
  488. const char* rhs = str;
  489. if (caseSensitive)
  490. return strcmp(lhs, rhs);
  491. else
  492. {
  493. if (!lhs || !rhs)
  494. return lhs ? 1 : (rhs ? -1 : 0);
  495. for (;;)
  496. {
  497. char l = tolower(*lhs);
  498. char r = tolower(*rhs);
  499. if (!l || !r)
  500. return l ? 1 : (r ? -1 : 0);
  501. if (l < r)
  502. return -1;
  503. if (l > r)
  504. return 1;
  505. ++lhs;
  506. ++rhs;
  507. }
  508. }
  509. }
  510. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  511. {
  512. int delta = (int)srcLength - (int)length;
  513. if (pos + length < length_)
  514. {
  515. if (delta < 0)
  516. {
  517. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  518. Resize(length_ + delta);
  519. }
  520. if (delta > 0)
  521. {
  522. Resize(length_ + delta);
  523. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  524. }
  525. }
  526. else
  527. Resize(length_ + delta);
  528. CopyChars(buffer_ + pos, srcStart, srcLength);
  529. }