StringBase.inl 17 KB

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