StringBase.inl 17 KB

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