CoreString.cpp 10 KB

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