StringBase.inl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. buffer_size = new_size;
  142. if (value == (T*)local_buffer)
  143. {
  144. T* new_value = (T*)realloc(NULL, buffer_size);
  145. Copy(new_value, (T*)local_buffer, LOCAL_BUFFER_SIZE / sizeof(T));
  146. value = new_value;
  147. }
  148. else
  149. {
  150. value = (T*)realloc(value, buffer_size);
  151. }
  152. }
  153. template< typename T >
  154. typename StringBase< T >::size_type StringBase< T >::Find(const T* find, size_type offset) const
  155. {
  156. return _Find(find, GetLength(find), offset);
  157. }
  158. template< typename T >
  159. typename StringBase< T >::size_type StringBase< T >::Find(const StringBase< T >& find, size_type offset) const
  160. {
  161. return _Find(find.CString(), find.Length(), offset);
  162. }
  163. template< typename T >
  164. typename StringBase< T >::size_type StringBase< T >::RFind(const T* find, size_type offset) const
  165. {
  166. return _RFind(find, GetLength(find), offset);
  167. }
  168. template< typename T >
  169. typename StringBase< T >::size_type StringBase< T >::RFind(const StringBase< T >& find, size_type offset) const
  170. {
  171. return _RFind(find.CString(), find.Length(), offset);
  172. }
  173. template< typename T >
  174. StringBase< T > StringBase< T >::Replace(const T* find, const T* replace) const
  175. {
  176. return _Replace(find, GetLength(find), replace, GetLength(replace));
  177. }
  178. template< typename T >
  179. StringBase< T > StringBase< T >::Replace(const StringBase< T >& find, const StringBase< T >& replace) const
  180. {
  181. return _Replace(find.CString(), find.Length(), replace.CString(), replace.Length());
  182. }
  183. template< typename T >
  184. StringBase< T > StringBase< T >::Substring(size_type start, size_type count) const
  185. {
  186. // Ensure we're not going of bounds
  187. if (count > length - start)
  188. count = length - start;
  189. if (start > length)
  190. count = 0;
  191. return StringBase< T >(&value[start], &value[start + count]);
  192. }
  193. template< typename T >
  194. StringBase< T >& StringBase< T >::Append(const T* append, size_type count)
  195. {
  196. return _Append(append, GetLength(append), count);
  197. }
  198. template< typename T >
  199. StringBase< T >& StringBase< T >::Append(const StringBase< T >& append, size_type count)
  200. {
  201. return _Append(append.CString(), append.Length(), count);
  202. }
  203. template< typename T >
  204. StringBase< T >& StringBase< T >::Append(const T& append)
  205. {
  206. T buffer[2] = { append, 0 };
  207. return (*this += buffer);
  208. }
  209. template< typename T >
  210. StringBase< T >& StringBase< T >::Assign(const T* assign, size_type count)
  211. {
  212. size_type assign_length = GetLength(assign);
  213. return _Assign(assign, count > assign_length ? assign_length : count);
  214. }
  215. template< typename T >
  216. StringBase< T >& StringBase< T >::Assign(const T* assign, const T* end)
  217. {
  218. return _Assign(assign, end - assign);
  219. }
  220. template< typename T >
  221. StringBase< T >& StringBase< T >::Assign(const StringBase< T >& assign, size_type count)
  222. {
  223. return _Assign(assign.CString(), assign.length, count);
  224. }
  225. // Insert a string into this string
  226. template< typename T >
  227. void StringBase< T >::Insert(size_type index, const T* insert, size_type count)
  228. {
  229. return _Insert(index, insert, GetLength(insert), count);
  230. }
  231. // Insert a string into this string
  232. template< typename T >
  233. void StringBase< T >::Insert(size_type index, const StringBase< T >& insert, size_type count)
  234. {
  235. return _Insert(index, insert.value, insert.length, count);
  236. }
  237. // Insert a character into this string
  238. template< typename T >
  239. void StringBase< T >::Insert(size_type index, const T& insert)
  240. {
  241. return _Insert(index, &insert, 1, 1);
  242. }
  243. /// Erase characters from this string
  244. template< typename T >
  245. void StringBase< T >::Erase(size_type index, size_type count)
  246. {
  247. if (index >= length)
  248. return;
  249. if (count == npos)
  250. {
  251. Resize(index);
  252. }
  253. else
  254. {
  255. size_type erase_amount = count < length - index ? count : length - index;
  256. Copy(&value[index], &value[index + erase_amount], length - index - erase_amount, true);
  257. length -= erase_amount;
  258. if (length == 0)
  259. Clear();
  260. }
  261. }
  262. template< typename T >
  263. int StringBase< T >::FormatString(size_type ROCKET_UNUSED_PARAMETER(max_length), const T* ROCKET_UNUSED_PARAMETER(fmt), ...)
  264. {
  265. ROCKET_UNUSED(max_length);
  266. ROCKET_UNUSED(fmt);
  267. ROCKET_ERRORMSG("Not implemented.");
  268. return -1;
  269. }
  270. template< typename T >
  271. void StringBase< T >::Resize(size_type new_length)
  272. {
  273. Reserve(new_length);
  274. length = new_length;
  275. value[length] = '\0';
  276. if (length == 0)
  277. Clear();
  278. }
  279. // Create a lowercase version of the string
  280. template< typename T >
  281. StringBase< T > StringBase< T >::ToLower() const
  282. {
  283. // Loop through the string, looking for an uppercase character
  284. size_t copy_index = npos;
  285. for (size_t i = 0; i < length; i++)
  286. {
  287. if (value[i] >= 'A' && value[i] <= 'Z')
  288. {
  289. copy_index = i;
  290. break;
  291. }
  292. }
  293. // If theres no lowercase letters, simply copy us direct
  294. if (copy_index == npos)
  295. return StringBase< T >(*this);
  296. StringBase< T > lowercase(CString(), CString() + copy_index);
  297. // Otherwise trawl through the rest of the letters
  298. for (size_t i = copy_index; i < length; i++)
  299. {
  300. if (value[i] >= 'A' && value[i] <= 'Z')
  301. lowercase.Append((T)(value[i] + ('a' - 'A')));
  302. else
  303. lowercase.Append(value[i]);
  304. }
  305. return lowercase;
  306. }
  307. // Create a lowercase version of the string
  308. template< typename T >
  309. StringBase< T > StringBase< T >::ToUpper() const
  310. {
  311. // Loop through the string, looking for an lowercase character
  312. size_t copy_index = npos;
  313. for (size_t i = 0; i < length; i++)
  314. {
  315. if (value[i] >= 'a' && value[i] <= 'z')
  316. {
  317. copy_index = i;
  318. break;
  319. }
  320. }
  321. // If theres no lowercase letters, simply copy us direct
  322. if (copy_index == npos)
  323. return StringBase< T >(*this);
  324. StringBase< T > uppercase(CString(), CString() + copy_index);
  325. // Otherwise trawl through the rest of the letters
  326. for (size_t i = copy_index; i < length; i++)
  327. {
  328. if (value[i] >= 'a' && value[i] <= 'z')
  329. uppercase.Append((T)(value[i] - ('a' - 'A')));
  330. else
  331. uppercase.Append(value[i]);
  332. }
  333. return uppercase;
  334. }
  335. template< typename T >
  336. bool StringBase< T >::operator==(const T* compare) const
  337. {
  338. size_type index = 0;
  339. while (index < length && compare[index] == value[index])
  340. index++;
  341. return index == length && compare[index] == '\0';
  342. }
  343. template< typename T >
  344. bool StringBase< T >::operator==(const StringBase< T >& compare) const
  345. {
  346. if (length != compare.length)
  347. return false;
  348. if (Hash() != compare.Hash())
  349. return false;
  350. return (*this) == compare.value;
  351. }
  352. template< typename T >
  353. bool StringBase< T >::operator!=(const T* compare) const
  354. {
  355. return !(*this == compare);
  356. }
  357. template< typename T >
  358. bool StringBase< T >::operator!=(const StringBase< T >& compare) const
  359. {
  360. return !(*this == compare);
  361. }
  362. template< typename T >
  363. bool StringBase< T >::operator<(const T* compare) const
  364. {
  365. size_type index = 0;
  366. while (index < length && compare[index] == value[index])
  367. index++;
  368. // Check if we reached the end of the string
  369. if (index < length)
  370. {
  371. // If we didn't check if we reached the end of
  372. // the string we're comparing against, if so
  373. // then we're not less than
  374. if (compare[index] == 0)
  375. return false;
  376. // Check the character at index
  377. return value[index] < compare[index];
  378. }
  379. else
  380. {
  381. // We reached the end of our string,
  382. // if the string we're comparing with still
  383. // has data, then we're smaller
  384. if (compare[index] != 0)
  385. return true;
  386. }
  387. return false;
  388. }
  389. template< typename T >
  390. bool StringBase< T >::operator<(const StringBase< T >& compare) const
  391. {
  392. return *this < compare.CString();
  393. }
  394. template< typename T >
  395. StringBase< T >& StringBase< T >::operator=(const T* assign)
  396. {
  397. return Assign(assign);
  398. }
  399. template< typename T >
  400. StringBase< T >& StringBase< T >::operator=(const StringBase< T >& assign)
  401. {
  402. StringBase< T >&out = Assign(assign);
  403. out.hash = assign.hash;
  404. return out;
  405. }
  406. template< typename T >
  407. StringBase< T > StringBase< T >::operator+(const T* add) const
  408. {
  409. StringBase< T > combined(*this);
  410. combined.Append(add);
  411. return combined;
  412. }
  413. template< typename T >
  414. StringBase< T > StringBase< T >::operator+(const StringBase< T >& add) const
  415. {
  416. StringBase< T > combined(*this);
  417. combined.Append(add);
  418. return combined;
  419. }
  420. template< typename T >
  421. StringBase< T >& StringBase< T >::operator+=(const T* add)
  422. {
  423. return Append(add);
  424. }
  425. template< typename T >
  426. StringBase< T >& StringBase< T >::operator+=(const StringBase< T >& add)
  427. {
  428. return _Append(add.CString(), add.length);
  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. const T& StringBase< T >::operator[](size_type index) const
  437. {
  438. ROCKET_ASSERT(index < length);
  439. return value[index];
  440. }
  441. template< typename T >
  442. T& StringBase< T >::operator[](size_type index)
  443. {
  444. ROCKET_ASSERT(index < length);
  445. return value[index];
  446. }
  447. template< typename T >
  448. typename StringBase< T >::size_type StringBase< T >::GetLength(const T* string) const
  449. {
  450. const T* ptr = string;
  451. while (*ptr)
  452. {
  453. ptr++;
  454. }
  455. return ptr - string;
  456. }
  457. template< typename T >
  458. void StringBase< T >::Copy(T* target, const T* src, size_type length, bool terminate)
  459. {
  460. // Copy values
  461. for (size_type i = 0; i < length; i++)
  462. {
  463. *target++ = *src++;
  464. }
  465. if (terminate)
  466. {
  467. *target++ = 0;
  468. }
  469. }
  470. template< typename T >
  471. typename StringBase< T >::size_type StringBase< T >::_Find(const T* find, size_type find_length, size_type offset) const
  472. {
  473. size_type needle_index = 0;
  474. size_type haystack_index = offset;
  475. // If the find length is greater than the string we have, it can't be here
  476. if (find_length > length)
  477. return npos;
  478. // While there's still data in the haystack loop
  479. while (value[haystack_index])
  480. {
  481. // If the current haystack posize_typeer plus needle offset matches,
  482. // advance the needle index
  483. if (value[haystack_index + needle_index] == find[needle_index])
  484. {
  485. needle_index++;
  486. // If we reach the end of the search term, return the current haystack index
  487. if (needle_index == find_length)
  488. return haystack_index;
  489. }
  490. else
  491. {
  492. // Advance haystack index by one and reset needle index.
  493. haystack_index++;
  494. needle_index = 0;
  495. }
  496. }
  497. return npos;
  498. }
  499. template< typename T >
  500. typename StringBase< T >::size_type StringBase< T >::_RFind(const T* find, size_type find_length, size_type offset) const
  501. {
  502. ROCKET_ASSERT(find_length > 0);
  503. size_type needle_index = 0;
  504. size_type haystack_index = (offset < length ? offset : length) - find_length;
  505. // If the find length is greater than the string we have, it can't be here
  506. if (find_length > length)
  507. return npos;
  508. // While theres still data in the haystack loop
  509. for (;;)
  510. {
  511. // If the current haystack index plus needle offset matches,
  512. // advance the needle index
  513. if (value[haystack_index + needle_index] == find[needle_index])
  514. {
  515. needle_index++;
  516. // If we reach the end of the search term, return the current haystack index
  517. if (find[needle_index] == 0)
  518. return haystack_index;
  519. }
  520. else
  521. {
  522. if (haystack_index == 0)
  523. return npos;
  524. // Advance haystack index backwards
  525. haystack_index--;
  526. needle_index = 0;
  527. }
  528. }
  529. }
  530. template< typename T >
  531. StringBase< T > StringBase< T >::_Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const
  532. {
  533. StringBase< T > result;
  534. size_type offset = 0;
  535. // Loop until we reach the end of the string
  536. while (offset < Length())
  537. {
  538. // Look for the next search term
  539. size_type pos = _Find(find, find_length, offset);
  540. // Term not found, add remainder and return
  541. if (pos == npos)
  542. return result + (Substring(offset).CString());
  543. // Add the unchanged text and replacement after it
  544. result += Substring(offset, pos - offset);
  545. result._Append(replace, replace_length);
  546. // Advance the find position
  547. offset = pos + find_length;
  548. }
  549. hash = 0;
  550. return result;
  551. }
  552. template< typename T >
  553. StringBase< T >& StringBase< T >::_Append(const T* append, size_type append_length, size_type count)
  554. {
  555. size_type add_length = count < append_length ? count : append_length;
  556. if (add_length == 0)
  557. return *this;
  558. Reserve(length + add_length);
  559. Copy(&value[length], append, add_length, true);
  560. length += add_length;
  561. hash = 0;
  562. return *this;
  563. }
  564. template< typename T >
  565. StringBase< T >& StringBase< T >::_Assign(const T* assign, size_type assign_length, size_type count)
  566. {
  567. size_type new_length = count < assign_length ? count : assign_length;
  568. if (new_length == 0)
  569. {
  570. Clear();
  571. }
  572. else
  573. {
  574. Reserve(new_length);
  575. Copy(value, assign, new_length, true);
  576. }
  577. length = new_length;
  578. hash = 0;
  579. return *this;
  580. }
  581. template< typename T >
  582. void StringBase< T >::_Insert(size_type index, const T* insert, size_type insert_length, size_type count)
  583. {
  584. if (index >= length)
  585. {
  586. Append(insert, count);
  587. return;
  588. }
  589. size_type add_length = count < insert_length ? count : insert_length;
  590. Reserve(length + add_length);
  591. for (size_type i = length + 1; i > index; i--)
  592. value[i + add_length - 1] = value[i - 1];
  593. Copy(&value[index], insert, add_length);
  594. length += add_length;
  595. hash = 0;
  596. }