StringBase.inl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. namespace Rocket {
  28. namespace Core {
  29. template< typename T >
  30. StringBase< T >::StringBase() : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  31. {
  32. value[0] = 0;
  33. }
  34. template< typename T >
  35. StringBase< T >::StringBase(const StringBase< T >& copy) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  36. {
  37. value[0] = 0;
  38. *this = copy;
  39. }
  40. template< typename T >
  41. StringBase< T >::StringBase(const T* string) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  42. {
  43. value[0] = 0;
  44. *this = string;
  45. }
  46. template< typename T >
  47. StringBase< T >::StringBase(const T* string_start, const T* string_end) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  48. {
  49. value[0] = 0;
  50. length = (string_end - string_start);
  51. if (length > 0)
  52. {
  53. Reserve(length);
  54. Copy(value, string_start, length, true);
  55. }
  56. }
  57. template< typename T >
  58. StringBase< T >::StringBase(size_type count, const T character) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  59. {
  60. value[0] = 0;
  61. length = count;
  62. if (length > 0)
  63. {
  64. Reserve(length);
  65. for (size_type i = 0; i < length; i++)
  66. value[i] = character;
  67. value[length] = '\0';
  68. }
  69. }
  70. template< typename T >
  71. StringBase< T >::StringBase(size_type ROCKET_UNUSED_PARAMETER(max_length), const T * ROCKET_UNUSED_PARAMETER(fmt), ...) : value((T*)local_buffer), buffer_size(LOCAL_BUFFER_SIZE), length(0), hash(0)
  72. {
  73. ROCKET_UNUSED(max_length);
  74. ROCKET_UNUSED(fmt);
  75. value[0] = 0;
  76. // Can't implement this at the base level, requires template specialisation
  77. ROCKET_ERRORMSG("Not implemented.");
  78. }
  79. template< typename T >
  80. StringBase< T >::~StringBase()
  81. {
  82. if (value != (T*)local_buffer)
  83. free(value);
  84. }
  85. template< typename T >
  86. bool StringBase< T >::Empty() const
  87. {
  88. return length == 0;
  89. }
  90. template< typename T >
  91. void StringBase< T >::Clear()
  92. {
  93. if (value != (T*)local_buffer)
  94. free(value);
  95. length = 0;
  96. hash = 0;
  97. value = (T*)local_buffer;
  98. value[0] = 0;
  99. buffer_size = LOCAL_BUFFER_SIZE;
  100. }
  101. template< typename T >
  102. typename StringBase< T >::size_type StringBase< T >::Length() const
  103. {
  104. return length;
  105. }
  106. template< typename T >
  107. unsigned int StringBase< T >::Hash() const
  108. {
  109. if (hash == 0 && length > 0)
  110. {
  111. // FNV-1 hash algorithm
  112. unsigned char* bp = (unsigned char*)value; // start of buffer
  113. unsigned char* be = (unsigned char*)value + (length * sizeof(T));
  114. // FNV-1a hash each octet in the buffer
  115. while (bp < be)
  116. {
  117. // xor the bottom with the current octet
  118. hash ^= *bp++;
  119. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  120. #if !defined(__GNUC__)
  121. const unsigned int FNV_32_PRIME = ((unsigned int)16777619);
  122. hash *= FNV_32_PRIME;
  123. #else
  124. hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
  125. #endif
  126. }
  127. }
  128. return hash;
  129. }
  130. template< typename T >
  131. const T * StringBase< T >::CString() const
  132. {
  133. return value;
  134. }
  135. template< typename T >
  136. void StringBase< T >::Reserve(size_type size)
  137. {
  138. size_type new_size = (size + 1) * sizeof(T);
  139. if (buffer_size >= new_size)
  140. return;
  141. // Pad out to a block of 16 bytes
  142. const int BLOCK_SIZE = 16;
  143. new_size = (new_size + BLOCK_SIZE - 1) & (~(BLOCK_SIZE - 1));
  144. if (value == (T*)local_buffer)
  145. {
  146. T* new_value = (T*)realloc(NULL, new_size);
  147. ROCKET_ASSERTMSG(new_value, "Could not reserve memory for String, realloc failed.");
  148. if (new_value != NULL)
  149. {
  150. buffer_size = new_size;
  151. Copy(new_value, (T*)local_buffer, LOCAL_BUFFER_SIZE / sizeof(T));
  152. value = new_value;
  153. }
  154. }
  155. else
  156. {
  157. T* new_value = (T*)realloc(value, new_size);
  158. ROCKET_ASSERTMSG(new_value, "Could not reserve memory for String, realloc failed.");
  159. if (new_value != NULL)
  160. {
  161. buffer_size = new_size;
  162. value = new_value;
  163. }
  164. }
  165. }
  166. template< typename T >
  167. typename StringBase< T >::size_type StringBase< T >::Find(const T * find, size_type offset) const
  168. {
  169. return _Find(find, GetLength(find), offset);
  170. }
  171. template< typename T >
  172. typename StringBase< T >::size_type StringBase< T >::Find(const StringBase< T > & find, size_type offset) const
  173. {
  174. return _Find(find.CString(), find.Length(), offset);
  175. }
  176. template< typename T >
  177. typename StringBase< T >::size_type StringBase< T >::RFind(const T * find, size_type offset) const
  178. {
  179. return _RFind(find, GetLength(find), offset);
  180. }
  181. template< typename T >
  182. typename StringBase< T >::size_type StringBase< T >::RFind(const StringBase< T > & find, size_type offset) const
  183. {
  184. return _RFind(find.CString(), find.Length(), offset);
  185. }
  186. template< typename T >
  187. StringBase< T > StringBase< T >::Replace(const T * find, const T * replace) const
  188. {
  189. return _Replace(find, GetLength(find), replace, GetLength(replace));
  190. }
  191. template< typename T >
  192. StringBase< T > StringBase< T >::Replace(const StringBase< T > & find, const StringBase< T > & replace) const
  193. {
  194. return _Replace(find.CString(), find.Length(), replace.CString(), replace.Length());
  195. }
  196. template< typename T >
  197. StringBase< T > StringBase< T >::Substring(size_type start, size_type count) const
  198. {
  199. // Ensure we're not going of bounds
  200. if (count > length - start)
  201. count = length - start;
  202. if (start > length)
  203. count = 0;
  204. return StringBase< T >(&value[start], &value[start + count]);
  205. }
  206. template< typename T >
  207. StringBase< T > & StringBase< T >::Append(const T * append, size_type count)
  208. {
  209. return _Append(append, GetLength(append), count);
  210. }
  211. template< typename T >
  212. StringBase< T >& StringBase< T >::Append(const StringBase< T > & append, size_type count)
  213. {
  214. return _Append(append.CString(), append.Length(), count);
  215. }
  216. template< typename T >
  217. StringBase< T >& StringBase< T >::Append(const T & append)
  218. {
  219. T buffer[2] = { append, 0 };
  220. return (*this += buffer);
  221. }
  222. template< typename T >
  223. StringBase< T >& StringBase< T >::Assign(const T * assign, size_type count)
  224. {
  225. size_type assign_length = GetLength(assign);
  226. return _Assign(assign, count > assign_length ? assign_length : count);
  227. }
  228. template< typename T >
  229. StringBase< T >& StringBase< T >::Assign(const T * assign, const T * end)
  230. {
  231. return _Assign(assign, end - assign);
  232. }
  233. template< typename T >
  234. StringBase< T >& StringBase< T >::Assign(const StringBase< T > & assign, size_type count)
  235. {
  236. return _Assign(assign.CString(), assign.length, count);
  237. }
  238. // Insert a string into this string
  239. template< typename T >
  240. void StringBase< T >::Insert(size_type index, const T * insert, size_type count)
  241. {
  242. return _Insert(index, insert, GetLength(insert), count);
  243. }
  244. // Insert a string into this string
  245. template< typename T >
  246. void StringBase< T >::Insert(size_type index, const StringBase< T > & insert, size_type count)
  247. {
  248. return _Insert(index, insert.value, insert.length, count);
  249. }
  250. // Insert a character into this string
  251. template< typename T >
  252. void StringBase< T >::Insert(size_type index, const T & insert)
  253. {
  254. return _Insert(index, &insert, 1, 1);
  255. }
  256. /// Erase characters from this string
  257. template< typename T >
  258. void StringBase< T >::Erase(size_type index, size_type count)
  259. {
  260. if (index >= length)
  261. return;
  262. if (count == npos)
  263. {
  264. Resize(index);
  265. }
  266. else
  267. {
  268. size_type erase_amount = count < length - index ? count : length - index;
  269. Copy(&value[index], &value[index + erase_amount], length - index - erase_amount, true);
  270. length -= erase_amount;
  271. if (length == 0)
  272. Clear();
  273. }
  274. }
  275. template< typename T >
  276. int StringBase< T >::FormatString(size_type ROCKET_UNUSED_PARAMETER(max_length), const T * ROCKET_UNUSED_PARAMETER(fmt), ...)
  277. {
  278. ROCKET_UNUSED(max_length);
  279. ROCKET_UNUSED(fmt);
  280. ROCKET_ERRORMSG("Not implemented.");
  281. return -1;
  282. }
  283. template< typename T >
  284. void StringBase< T >::Resize(size_type new_length)
  285. {
  286. Reserve(new_length);
  287. length = new_length;
  288. value[length] = '\0';
  289. if (length == 0)
  290. Clear();
  291. }
  292. // Create a lowercase version of the string
  293. template< typename T >
  294. StringBase< T > StringBase< T >::ToLower() const
  295. {
  296. // Loop through the string, looking for an uppercase character
  297. size_t copy_index = npos;
  298. for (size_t i = 0; i < length; i++)
  299. {
  300. if (value[i] >= 'A' && value[i] <= 'Z')
  301. {
  302. copy_index = i;
  303. break;
  304. }
  305. }
  306. // If theres no lowercase letters, simply copy us direct
  307. if (copy_index == npos)
  308. return StringBase< T >(*this);
  309. StringBase< T > lowercase(CString(), CString() + copy_index);
  310. // Otherwise trawl through the rest of the letters
  311. for (size_t i = copy_index; i < length; i++)
  312. {
  313. if (value[i] >= 'A' && value[i] <= 'Z')
  314. lowercase.Append((T)(value[i] + ('a' - 'A')));
  315. else
  316. lowercase.Append(value[i]);
  317. }
  318. return lowercase;
  319. }
  320. // Create a lowercase version of the string
  321. template< typename T >
  322. StringBase< T > StringBase< T >::ToUpper() const
  323. {
  324. // Loop through the string, looking for an lowercase character
  325. size_t copy_index = npos;
  326. for (size_t i = 0; i < length; i++)
  327. {
  328. if (value[i] >= 'a' && value[i] <= 'z')
  329. {
  330. copy_index = i;
  331. break;
  332. }
  333. }
  334. // If theres no lowercase letters, simply copy us direct
  335. if (copy_index == npos)
  336. return StringBase< T >(*this);
  337. StringBase< T > uppercase(CString(), CString() + copy_index);
  338. // Otherwise trawl through the rest of the letters
  339. for (size_t i = copy_index; i < length; i++)
  340. {
  341. if (value[i] >= 'a' && value[i] <= 'z')
  342. uppercase.Append((T)(value[i] - ('a' - 'A')));
  343. else
  344. uppercase.Append(value[i]);
  345. }
  346. return uppercase;
  347. }
  348. template< typename T >
  349. bool StringBase< T >::operator==(const T * compare) const
  350. {
  351. size_type index = 0;
  352. while (index < length && compare[index] == value[index])
  353. index++;
  354. return index == length && compare[index] == '\0';
  355. }
  356. template< typename T >
  357. bool StringBase< T >::operator==(const StringBase< T > & compare) const
  358. {
  359. if (length != compare.length)
  360. return false;
  361. if (Hash() != compare.Hash())
  362. return false;
  363. return (*this) == compare.value;
  364. }
  365. template< typename T >
  366. bool StringBase< T >::operator!=(const T * compare) const
  367. {
  368. return !(*this == compare);
  369. }
  370. template< typename T >
  371. bool StringBase< T >::operator!=(const StringBase< T > & compare) const
  372. {
  373. return !(*this == compare);
  374. }
  375. template< typename T >
  376. bool StringBase< T >::operator<(const T * compare) const
  377. {
  378. size_type index = 0;
  379. while (index < length && compare[index] == value[index])
  380. index++;
  381. // Check if we reached the end of the string
  382. if (index < length)
  383. {
  384. // If we didn't check if we reached the end of
  385. // the string we're comparing against, if so
  386. // then we're not less than
  387. if (compare[index] == 0)
  388. return false;
  389. // Check the character at index
  390. return value[index] < compare[index];
  391. }
  392. else
  393. {
  394. // We reached the end of our string,
  395. // if the string we're comparing with still
  396. // has data, then we're smaller
  397. if (compare[index] != 0)
  398. return true;
  399. }
  400. return false;
  401. }
  402. template< typename T >
  403. bool StringBase< T >::operator<(const StringBase< T > & compare) const
  404. {
  405. return *this < compare.CString();
  406. }
  407. template< typename T >
  408. StringBase< T >& StringBase< T >::operator=(const T * assign)
  409. {
  410. return Assign(assign);
  411. }
  412. template< typename T >
  413. StringBase< T >& StringBase< T >::operator=(const StringBase< T > & assign)
  414. {
  415. StringBase< T >& out = Assign(assign);
  416. out.hash = assign.hash;
  417. return out;
  418. }
  419. template< typename T >
  420. StringBase< T > StringBase< T >::operator+(const T * add) const
  421. {
  422. StringBase< T > combined(*this);
  423. combined.Append(add);
  424. return combined;
  425. }
  426. template< typename T >
  427. StringBase< T > StringBase< T >::operator+(const StringBase< T > & add) const
  428. {
  429. StringBase< T > combined(*this);
  430. combined.Append(add);
  431. return combined;
  432. }
  433. template< typename T >
  434. StringBase< T >& StringBase< T >::operator+=(const T * add)
  435. {
  436. return Append(add);
  437. }
  438. template< typename T >
  439. StringBase< T >& StringBase< T >::operator+=(const StringBase< T > & add)
  440. {
  441. return _Append(add.CString(), add.length);
  442. }
  443. template< typename T >
  444. StringBase< T >& StringBase< T >::operator+=(const T & add)
  445. {
  446. return Append(add);
  447. }
  448. template< typename T >
  449. const T& StringBase< T >::operator[](size_type index) const
  450. {
  451. ROCKET_ASSERT(index < length);
  452. return value[index];
  453. }
  454. template< typename T >
  455. T& StringBase< T >::operator[](size_type index)
  456. {
  457. ROCKET_ASSERT(index < length);
  458. return value[index];
  459. }
  460. template< typename T >
  461. typename StringBase< T >::size_type StringBase< T >::GetLength(const T * string) const
  462. {
  463. const T* ptr = string;
  464. while (*ptr)
  465. {
  466. ptr++;
  467. }
  468. return ptr - string;
  469. }
  470. template< typename T >
  471. void StringBase< T >::Copy(T * target, const T * src, size_type length, bool terminate)
  472. {
  473. // Copy values
  474. for (size_type i = 0; i < length; i++)
  475. {
  476. *target++ = *src++;
  477. }
  478. if (terminate)
  479. {
  480. *target++ = 0;
  481. }
  482. }
  483. template< typename T >
  484. typename StringBase< T >::size_type StringBase< T >::_Find(const T * find, size_type find_length, size_type offset) const
  485. {
  486. size_type needle_index = 0;
  487. size_type haystack_index = offset;
  488. // If the find length is greater than the string we have, it can't be here
  489. if (find_length > length)
  490. return npos;
  491. // While there's still data in the haystack loop
  492. while (value[haystack_index])
  493. {
  494. // If the current haystack posize_typeer plus needle offset matches,
  495. // advance the needle index
  496. if (value[haystack_index + needle_index] == find[needle_index])
  497. {
  498. needle_index++;
  499. // If we reach the end of the search term, return the current haystack index
  500. if (needle_index == find_length)
  501. return haystack_index;
  502. }
  503. else
  504. {
  505. // Advance haystack index by one and reset needle index.
  506. haystack_index++;
  507. needle_index = 0;
  508. }
  509. }
  510. return npos;
  511. }
  512. template< typename T >
  513. typename StringBase< T >::size_type StringBase< T >::_RFind(const T * find, size_type find_length, size_type offset) const
  514. {
  515. ROCKET_ASSERT(find_length > 0);
  516. size_type needle_index = 0;
  517. size_type haystack_index = (offset < length ? offset : length) - find_length;
  518. // If the find length is greater than the string we have, it can't be here
  519. if (find_length > length)
  520. return npos;
  521. // While theres still data in the haystack loop
  522. for (;;)
  523. {
  524. // If the current haystack index plus needle offset matches,
  525. // advance the needle index
  526. if (value[haystack_index + needle_index] == find[needle_index])
  527. {
  528. needle_index++;
  529. // If we reach the end of the search term, return the current haystack index
  530. if (find[needle_index] == 0)
  531. return haystack_index;
  532. }
  533. else
  534. {
  535. if (haystack_index == 0)
  536. return npos;
  537. // Advance haystack index backwards
  538. haystack_index--;
  539. needle_index = 0;
  540. }
  541. }
  542. }
  543. template< typename T >
  544. StringBase< T > StringBase< T >::_Replace(const T * find, size_type find_length, const T * replace, size_type replace_length) const
  545. {
  546. StringBase< T > result;
  547. size_type offset = 0;
  548. // Loop until we reach the end of the string
  549. while (offset < Length())
  550. {
  551. // Look for the next search term
  552. size_type pos = _Find(find, find_length, offset);
  553. // Term not found, add remainder and return
  554. if (pos == npos)
  555. return result + (Substring(offset).CString());
  556. // Add the unchanged text and replacement after it
  557. result += Substring(offset, pos - offset);
  558. result._Append(replace, replace_length);
  559. // Advance the find position
  560. offset = pos + find_length;
  561. }
  562. hash = 0;
  563. return result;
  564. }
  565. template< typename T >
  566. StringBase< T >& StringBase< T >::_Append(const T * append, size_type append_length, size_type count)
  567. {
  568. size_type add_length = count < append_length ? count : append_length;
  569. if (add_length == 0)
  570. return *this;
  571. Reserve(length + add_length);
  572. Copy(&value[length], append, add_length, true);
  573. length += add_length;
  574. hash = 0;
  575. return *this;
  576. }
  577. template< typename T >
  578. StringBase< T > & StringBase< T >::_Assign(const T * assign, size_type assign_length, size_type count)
  579. {
  580. size_type new_length = count < assign_length ? count : assign_length;
  581. if (new_length == 0)
  582. {
  583. Clear();
  584. }
  585. else
  586. {
  587. Reserve(new_length);
  588. Copy(value, assign, new_length, true);
  589. }
  590. length = new_length;
  591. hash = 0;
  592. return *this;
  593. }
  594. template< typename T >
  595. void StringBase< T >::_Insert(size_type index, const T * insert, size_type insert_length, size_type count)
  596. {
  597. if (index >= length)
  598. {
  599. Append(insert, count);
  600. return;
  601. }
  602. size_type add_length = count < insert_length ? count : insert_length;
  603. Reserve(length + add_length);
  604. for (size_type i = length + 1; i > index; i--)
  605. value[i + add_length - 1] = value[i - 1];
  606. Copy(&value[index], insert, add_length);
  607. length += add_length;
  608. hash = 0;
  609. }
  610. }
  611. }