CoreString.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. char String::endZero = 0;
  26. void String::Replace(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::Replace(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. Replace(pos, replaceThis.length_, replaceWith);
  43. nextPos = pos + replaceWith.length_;
  44. }
  45. }
  46. void String::Replace(unsigned pos, unsigned length, const String& str)
  47. {
  48. // If substring is illegal, do nothing
  49. if (pos + length > length_)
  50. return;
  51. Replace(pos, length, str.buffer_, str.length_);
  52. }
  53. String::Iterator String::Replace(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. Replace(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. Replace(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. Replace(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. Replace(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::Substring(unsigned pos) const
  197. {
  198. if (pos >= length_)
  199. return String();
  200. else
  201. {
  202. String ret;
  203. ret.Resize(length_ - pos);
  204. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  205. return ret;
  206. }
  207. }
  208. String String::Substring(unsigned pos, unsigned length) const
  209. {
  210. if (pos >= length_)
  211. return String();
  212. else
  213. {
  214. String ret;
  215. if (pos + length > length_)
  216. length = length_ - pos;
  217. ret.Resize(length);
  218. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  219. return ret;
  220. }
  221. }
  222. String String::Trim() const
  223. {
  224. unsigned trimStart = 0;
  225. unsigned trimEnd = length_;
  226. while (trimStart < trimEnd)
  227. {
  228. char c = buffer_[trimStart];
  229. if ((c != ' ') && (c != 9))
  230. break;
  231. ++trimStart;
  232. }
  233. while (trimEnd > trimStart)
  234. {
  235. char c = buffer_[trimEnd - 1];
  236. if ((c != ' ') && (c != 9))
  237. break;
  238. --trimEnd;
  239. }
  240. return Substring(trimStart, trimEnd - trimStart);
  241. }
  242. String String::ToLower() const
  243. {
  244. String ret(*this);
  245. for (unsigned i = 0; i < ret.length_; ++i)
  246. ret[i] = tolower(buffer_[i]);
  247. return ret;
  248. }
  249. String String::ToUpper() const
  250. {
  251. String ret(*this);
  252. for (unsigned i = 0; i < ret.length_; ++i)
  253. ret[i] = toupper(buffer_[i]);
  254. return ret;
  255. }
  256. unsigned String::Find(char c, unsigned startPos) const
  257. {
  258. for (unsigned i = startPos; i < length_; ++i)
  259. {
  260. if (buffer_[i] == c)
  261. return i;
  262. }
  263. return NPOS;
  264. }
  265. unsigned String::Find(const String& str, unsigned startPos) const
  266. {
  267. if ((!str.length_) || (str.length_ > length_))
  268. return NPOS;
  269. char first = str.buffer_[0];
  270. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  271. {
  272. if (buffer_[i] == first)
  273. {
  274. unsigned skip = NPOS;
  275. bool found = true;
  276. for (unsigned j = 1; j < str.length_; ++j)
  277. {
  278. char c = buffer_[i + j];
  279. if ((skip == NPOS) && (c == first))
  280. skip = i + j - 1;
  281. if (c != str.buffer_[j])
  282. {
  283. found = false;
  284. if (skip != NPOS)
  285. i = skip;
  286. break;
  287. }
  288. }
  289. if (found)
  290. return i;
  291. }
  292. }
  293. return NPOS;
  294. }
  295. unsigned String::FindLast(char c) const
  296. {
  297. for (unsigned i = length_ - 1; i < length_; --i)
  298. {
  299. if (buffer_[i] == c)
  300. return i;
  301. }
  302. return NPOS;
  303. }
  304. unsigned String::FindLast(const String& str) const
  305. {
  306. if ((!str.length_) || (str.length_ > length_))
  307. return NPOS;
  308. char first = str.buffer_[0];
  309. for (unsigned i = length_ - str.length_; i < length_; --i)
  310. {
  311. if (buffer_[i] == first)
  312. {
  313. bool found = true;
  314. for (unsigned j = 1; j < str.length_; ++j)
  315. {
  316. char c = buffer_[i + j];
  317. if (c != str.buffer_[j])
  318. {
  319. found = false;
  320. break;
  321. }
  322. }
  323. if (found)
  324. return i;
  325. }
  326. }
  327. return NPOS;
  328. }
  329. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  330. {
  331. int delta = (int)srcLength - (int)length;
  332. if (pos + length < length_)
  333. {
  334. if (delta < 0)
  335. {
  336. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  337. Resize(length_ + delta);
  338. }
  339. if (delta > 0)
  340. {
  341. Resize(length_ + delta);
  342. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  343. }
  344. }
  345. else
  346. Resize(length_ + delta);
  347. CopyChars(buffer_ + pos, srcStart, srcLength);
  348. }
  349. template<> void Swap<String>(String& first, String& second)
  350. {
  351. first.Swap(second);
  352. }