String.cpp 13 KB

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