String.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. char String::endZero = 0;
  26. void String::ReplaceInPlace(char replaceThis, char replaceWith)
  27. {
  28. for (unsigned i = 0; i < length_; ++i)
  29. {
  30. if (buffer_[i] == replaceThis)
  31. buffer_[i] = replaceWith;
  32. }
  33. }
  34. void String::ReplaceInPlace(const String& replaceThis, const String& replaceWith)
  35. {
  36. unsigned nextPos = 0;
  37. while (nextPos < length_)
  38. {
  39. unsigned pos = Find(replaceThis, nextPos);
  40. if (pos == NPOS)
  41. break;
  42. ReplaceInPlace(pos, replaceThis.length_, replaceWith);
  43. nextPos = pos + replaceWith.length_;
  44. }
  45. }
  46. void String::ReplaceInPlace(unsigned pos, unsigned length, const String& str)
  47. {
  48. // If substring is illegal, do nothing
  49. if (pos + length > length_)
  50. return;
  51. ReplaceInPlace(pos, length, str.buffer_, str.length_);
  52. }
  53. String::Iterator String::ReplaceInPlace(const String::Iterator& start, const String::Iterator& end, const String& replaceWith)
  54. {
  55. unsigned pos = start - Begin();
  56. if (pos >= length_)
  57. return End();
  58. unsigned length = end - start;
  59. ReplaceInPlace(pos, length, replaceWith);
  60. return Begin() + pos;
  61. }
  62. void String::Insert(unsigned pos, const String& str)
  63. {
  64. if (pos > length_)
  65. pos = length_;
  66. if (pos == length_)
  67. (*this) += str;
  68. else
  69. ReplaceInPlace(pos, 0, str);
  70. }
  71. void String::Insert(unsigned pos, char c)
  72. {
  73. if (pos > length_)
  74. pos = length_;
  75. if (pos == length_)
  76. (*this) += c;
  77. else
  78. {
  79. unsigned oldLength = length_;
  80. Resize(length_ + 1);
  81. MoveRange(pos + 1, pos, oldLength - pos);
  82. buffer_[pos] = c;
  83. }
  84. }
  85. String::Iterator String::Insert(const String::Iterator& dest, const String& str)
  86. {
  87. unsigned pos = dest - Begin();
  88. if (pos > length_)
  89. pos = length_;
  90. Insert(pos, str);
  91. return Begin() + pos;
  92. }
  93. String::Iterator String::Insert(const String::Iterator& dest, const String::Iterator& start, const String::Iterator& end)
  94. {
  95. unsigned pos = dest - Begin();
  96. if (pos > length_)
  97. pos = length_;
  98. unsigned length = end - start;
  99. ReplaceInPlace(pos, 0, &(*start), length);
  100. return Begin() + pos;
  101. }
  102. String::Iterator String::Insert(const String::Iterator& dest, char c)
  103. {
  104. unsigned pos = dest - Begin();
  105. if (pos > length_)
  106. pos = length_;
  107. Insert(pos, c);
  108. return Begin() + pos;
  109. }
  110. void String::Erase(unsigned pos, unsigned length)
  111. {
  112. ReplaceInPlace(pos, length, String());
  113. }
  114. String::Iterator String::Erase(const String::Iterator& it)
  115. {
  116. unsigned pos = it - Begin();
  117. if (pos >= length_)
  118. return End();
  119. Erase(pos);
  120. return Begin() + pos;
  121. }
  122. String::Iterator String::Erase(const String::Iterator& start, const String::Iterator& end)
  123. {
  124. unsigned pos = start - Begin();
  125. if (pos >= length_)
  126. return End();
  127. unsigned length = end - start;
  128. Erase(pos, length);
  129. return Begin() + pos;
  130. }
  131. void String::Resize(unsigned newLength)
  132. {
  133. if (newLength == length_)
  134. return;
  135. if (!capacity_)
  136. {
  137. // Calculate initial capacity
  138. capacity_ = newLength + 1;
  139. if (capacity_ < MIN_CAPACITY)
  140. capacity_ = MIN_CAPACITY;
  141. buffer_ = new char[capacity_];
  142. }
  143. else
  144. {
  145. if (capacity_ < newLength + 1)
  146. {
  147. // Increase the capacity with half each time it is exceeded
  148. while (capacity_ < newLength + 1)
  149. {
  150. unsigned increment = capacity_ >> 1;
  151. if (!increment)
  152. increment = 1;
  153. capacity_ += increment;
  154. }
  155. char* newBuffer = new char[capacity_];
  156. // Move the existing data to the new buffer, then delete the old buffer
  157. if (length_)
  158. CopyChars(newBuffer, buffer_, length_);
  159. delete[] buffer_;
  160. buffer_ = newBuffer;
  161. }
  162. }
  163. buffer_[newLength] = 0;
  164. length_ = newLength;
  165. }
  166. void String::Reserve(unsigned newCapacity)
  167. {
  168. if (newCapacity < length_ + 1)
  169. newCapacity = length_ + 1;
  170. if (newCapacity == capacity_)
  171. return;
  172. char* newBuffer = new char[newCapacity];
  173. // Move the existing data to the new buffer, then delete the old buffer
  174. CopyChars(newBuffer, buffer_, length_ + 1);
  175. if (capacity_)
  176. delete[] buffer_;
  177. capacity_ = newCapacity;
  178. buffer_ = newBuffer;
  179. }
  180. void String::Compact()
  181. {
  182. if (!capacity_)
  183. return;
  184. Reserve(length_ + 1);
  185. }
  186. void String::Clear()
  187. {
  188. Resize(0);
  189. }
  190. void String::Swap(String& str)
  191. {
  192. ::Swap(length_, str.length_);
  193. ::Swap(capacity_, str.capacity_);
  194. ::Swap(buffer_, str.buffer_);
  195. }
  196. String String::Replace(char replaceThis, char replaceWith) const
  197. {
  198. String ret(*this);
  199. ret.ReplaceInPlace(replaceThis, replaceWith);
  200. return ret;
  201. }
  202. String String::Replace(const String& replaceThis, const String& replaceWith) const
  203. {
  204. String ret(*this);
  205. ret.ReplaceInPlace(replaceThis, replaceWith);
  206. return ret;
  207. }
  208. String String::Replace(unsigned pos, unsigned length, const String& str) const
  209. {
  210. String ret(*this);
  211. ret.ReplaceInPlace(pos, length, str);
  212. return ret;
  213. }
  214. String String::Substring(unsigned pos) const
  215. {
  216. if (pos >= length_)
  217. return String();
  218. else
  219. {
  220. String ret;
  221. ret.Resize(length_ - pos);
  222. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  223. return ret;
  224. }
  225. }
  226. String String::Substring(unsigned pos, unsigned length) const
  227. {
  228. if (pos >= length_)
  229. return String();
  230. else
  231. {
  232. String ret;
  233. if (pos + length > length_)
  234. length = length_ - pos;
  235. ret.Resize(length);
  236. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  237. return ret;
  238. }
  239. }
  240. String String::Trim() const
  241. {
  242. unsigned trimStart = 0;
  243. unsigned trimEnd = length_;
  244. while (trimStart < trimEnd)
  245. {
  246. char c = buffer_[trimStart];
  247. if ((c != ' ') && (c != 9))
  248. break;
  249. ++trimStart;
  250. }
  251. while (trimEnd > trimStart)
  252. {
  253. char c = buffer_[trimEnd - 1];
  254. if ((c != ' ') && (c != 9))
  255. break;
  256. --trimEnd;
  257. }
  258. return Substring(trimStart, trimEnd - trimStart);
  259. }
  260. String String::ToLower() const
  261. {
  262. String ret(*this);
  263. for (unsigned i = 0; i < ret.length_; ++i)
  264. ret[i] = tolower(buffer_[i]);
  265. return ret;
  266. }
  267. String String::ToUpper() const
  268. {
  269. String ret(*this);
  270. for (unsigned i = 0; i < ret.length_; ++i)
  271. ret[i] = toupper(buffer_[i]);
  272. return ret;
  273. }
  274. unsigned String::Find(char c, unsigned startPos) const
  275. {
  276. for (unsigned i = startPos; i < length_; ++i)
  277. {
  278. if (buffer_[i] == c)
  279. return i;
  280. }
  281. return NPOS;
  282. }
  283. unsigned String::Find(const String& str, unsigned startPos) const
  284. {
  285. if ((!str.length_) || (str.length_ > length_))
  286. return NPOS;
  287. char first = str.buffer_[0];
  288. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  289. {
  290. if (buffer_[i] == first)
  291. {
  292. unsigned skip = NPOS;
  293. bool found = true;
  294. for (unsigned j = 1; j < str.length_; ++j)
  295. {
  296. char c = buffer_[i + j];
  297. if ((skip == NPOS) && (c == first))
  298. skip = i + j - 1;
  299. if (c != str.buffer_[j])
  300. {
  301. found = false;
  302. if (skip != NPOS)
  303. i = skip;
  304. break;
  305. }
  306. }
  307. if (found)
  308. return i;
  309. }
  310. }
  311. return NPOS;
  312. }
  313. unsigned String::FindLast(char c) const
  314. {
  315. for (unsigned i = length_ - 1; i < length_; --i)
  316. {
  317. if (buffer_[i] == c)
  318. return i;
  319. }
  320. return NPOS;
  321. }
  322. unsigned String::FindLast(const String& str) const
  323. {
  324. if ((!str.length_) || (str.length_ > length_))
  325. return NPOS;
  326. char first = str.buffer_[0];
  327. for (unsigned i = length_ - str.length_; i < length_; --i)
  328. {
  329. if (buffer_[i] == first)
  330. {
  331. bool found = true;
  332. for (unsigned j = 1; j < str.length_; ++j)
  333. {
  334. char c = buffer_[i + j];
  335. if (c != str.buffer_[j])
  336. {
  337. found = false;
  338. break;
  339. }
  340. }
  341. if (found)
  342. return i;
  343. }
  344. }
  345. return NPOS;
  346. }
  347. void String::ReplaceInPlace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  348. {
  349. int delta = (int)srcLength - (int)length;
  350. if (pos + length < length_)
  351. {
  352. if (delta < 0)
  353. {
  354. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  355. Resize(length_ + delta);
  356. }
  357. if (delta > 0)
  358. {
  359. Resize(length_ + delta);
  360. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  361. }
  362. }
  363. else
  364. Resize(length_ + delta);
  365. CopyChars(buffer_ + pos, srcStart, srcLength);
  366. }