String.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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::Replace(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::Replace(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. Replace(pos, replaceThis.length_, replaceWith);
  140. nextPos = pos + replaceWith.length_;
  141. }
  142. }
  143. void String::Replace(unsigned pos, unsigned length, const String& str)
  144. {
  145. // If substring is illegal, do nothing
  146. if (pos + length > length_)
  147. return;
  148. Replace(pos, length, str.buffer_, str.length_);
  149. }
  150. String::Iterator String::Replace(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. Replace(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. Replace(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. Replace(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. Replace(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::Substring(unsigned pos) const
  286. {
  287. if (pos < length_)
  288. {
  289. String ret;
  290. ret.Resize(length_ - pos);
  291. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  292. return ret;
  293. }
  294. else
  295. return String();
  296. }
  297. String String::Substring(unsigned pos, unsigned length) const
  298. {
  299. if (pos < length_)
  300. {
  301. String ret;
  302. if (pos + length > length_)
  303. length = length_ - pos;
  304. ret.Resize(length);
  305. CopyChars(ret.buffer_, buffer_ + pos, ret.length_);
  306. return ret;
  307. }
  308. else
  309. return String();
  310. }
  311. String String::Trimmed() const
  312. {
  313. unsigned trimStart = 0;
  314. unsigned trimEnd = length_;
  315. while (trimStart < trimEnd)
  316. {
  317. char c = buffer_[trimStart];
  318. if (c != ' ' && c != 9)
  319. break;
  320. ++trimStart;
  321. }
  322. while (trimEnd > trimStart)
  323. {
  324. char c = buffer_[trimEnd - 1];
  325. if (c != ' ' && c != 9)
  326. break;
  327. --trimEnd;
  328. }
  329. return Substring(trimStart, trimEnd - trimStart);
  330. }
  331. String String::ToLower() const
  332. {
  333. String ret(*this);
  334. for (unsigned i = 0; i < ret.length_; ++i)
  335. ret[i] = tolower(buffer_[i]);
  336. return ret;
  337. }
  338. String String::ToUpper() const
  339. {
  340. String ret(*this);
  341. for (unsigned i = 0; i < ret.length_; ++i)
  342. ret[i] = toupper(buffer_[i]);
  343. return ret;
  344. }
  345. Vector<String> String::Split(char separator) const
  346. {
  347. Vector<String> ret;
  348. unsigned pos = 0;
  349. while (pos < length_)
  350. {
  351. unsigned start = pos;
  352. while (start < length_)
  353. {
  354. if (buffer_[start] == separator)
  355. break;
  356. start++;
  357. }
  358. if (start == length_)
  359. {
  360. ret.Push(Substring(pos));
  361. break;
  362. }
  363. unsigned end = start;
  364. while (end < length_)
  365. {
  366. if (buffer_[end] != separator)
  367. break;
  368. end++;
  369. }
  370. ret.Push(Substring(pos, start - pos));
  371. pos = end;
  372. }
  373. return ret;
  374. }
  375. unsigned String::Find(char c, unsigned startPos) const
  376. {
  377. for (unsigned i = startPos; i < length_; ++i)
  378. {
  379. if (buffer_[i] == c)
  380. return i;
  381. }
  382. return NPOS;
  383. }
  384. unsigned String::Find(const String& str, unsigned startPos) const
  385. {
  386. if (!str.length_ || str.length_ > length_)
  387. return NPOS;
  388. char first = str.buffer_[0];
  389. for (unsigned i = startPos; i <= length_ - str.length_; ++i)
  390. {
  391. if (buffer_[i] == first)
  392. {
  393. unsigned skip = NPOS;
  394. bool found = true;
  395. for (unsigned j = 1; j < str.length_; ++j)
  396. {
  397. char c = buffer_[i + j];
  398. if (skip == NPOS && c == first)
  399. skip = i + j - 1;
  400. if (c != str.buffer_[j])
  401. {
  402. found = false;
  403. if (skip != NPOS)
  404. i = skip;
  405. break;
  406. }
  407. }
  408. if (found)
  409. return i;
  410. }
  411. }
  412. return NPOS;
  413. }
  414. unsigned String::FindLast(char c, unsigned startPos) const
  415. {
  416. if (startPos >= length_)
  417. startPos = length_ - 1;
  418. for (unsigned i = startPos; i < length_; --i)
  419. {
  420. if (buffer_[i] == c)
  421. return i;
  422. }
  423. return NPOS;
  424. }
  425. unsigned String::FindLast(const String& str, unsigned startPos) const
  426. {
  427. if (!str.length_ || str.length_ > length_)
  428. return NPOS;
  429. if (startPos > length_ - str.length_)
  430. startPos = length_ - str.length_;
  431. char first = str.buffer_[0];
  432. for (unsigned i = startPos; i < length_; --i)
  433. {
  434. if (buffer_[i] == first)
  435. {
  436. bool found = true;
  437. for (unsigned j = 1; j < str.length_; ++j)
  438. {
  439. char c = buffer_[i + j];
  440. if (c != str.buffer_[j])
  441. {
  442. found = false;
  443. break;
  444. }
  445. }
  446. if (found)
  447. return i;
  448. }
  449. }
  450. return NPOS;
  451. }
  452. void String::Replace(unsigned pos, unsigned length, const char* srcStart, unsigned srcLength)
  453. {
  454. int delta = (int)srcLength - (int)length;
  455. if (pos + length < length_)
  456. {
  457. if (delta < 0)
  458. {
  459. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  460. Resize(length_ + delta);
  461. }
  462. if (delta > 0)
  463. {
  464. Resize(length_ + delta);
  465. MoveRange(pos + srcLength, pos + length, length_ - pos - length);
  466. }
  467. }
  468. else
  469. Resize(length_ + delta);
  470. CopyChars(buffer_ + pos, srcStart, srcLength);
  471. }