StringBase.inl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 uppercase 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. return Assign(assign);
  399. }
  400. template< typename T >
  401. StringBase< T > StringBase< T >::operator+(const T* add) const
  402. {
  403. StringBase< T > combined(*this);
  404. combined.Append(add);
  405. return combined;
  406. }
  407. template< typename T >
  408. StringBase< T > StringBase< T >::operator+(const StringBase< T >& add) const
  409. {
  410. StringBase< T > combined(*this);
  411. combined.Append(add);
  412. return combined;
  413. }
  414. template< typename T >
  415. StringBase< T >& StringBase< T >::operator+=(const T* add)
  416. {
  417. return Append(add);
  418. }
  419. template< typename T >
  420. StringBase< T >& StringBase< T >::operator+=(const StringBase< T >& add)
  421. {
  422. return _Append(add.CString(), add.length);
  423. }
  424. template< typename T >
  425. StringBase< T >& StringBase< T >::operator+=(const T& add)
  426. {
  427. return Append(add);
  428. }
  429. template< typename T >
  430. const T& StringBase< T >::operator[](size_type index) const
  431. {
  432. ROCKET_ASSERT(index < length);
  433. return value[index];
  434. }
  435. template< typename T >
  436. T& StringBase< T >::operator[](size_type index)
  437. {
  438. ROCKET_ASSERT(index < length);
  439. return value[index];
  440. }
  441. template< typename T >
  442. typename StringBase< T >::size_type StringBase< T >::GetLength(const T* string) const
  443. {
  444. const T* ptr = string;
  445. while (*ptr)
  446. {
  447. ptr++;
  448. }
  449. return ptr - string;
  450. }
  451. template< typename T >
  452. void StringBase< T >::Copy(T* target, const T* src, size_type length, bool terminate)
  453. {
  454. // Copy values
  455. for (size_type i = 0; i < length; i++)
  456. {
  457. *target++ = *src++;
  458. }
  459. if (terminate)
  460. {
  461. *target++ = 0;
  462. }
  463. }
  464. template< typename T >
  465. typename StringBase< T >::size_type StringBase< T >::_Find(const T* find, size_type find_length, size_type offset) const
  466. {
  467. size_type needle_index = 0;
  468. size_type haystack_index = offset;
  469. // If the find length is greater than the string we have, it can't be here
  470. if (find_length > length)
  471. return npos;
  472. // While there's still data in the haystack loop
  473. while (value[haystack_index])
  474. {
  475. // If the current haystack posize_typeer plus needle offset matches,
  476. // advance the needle index
  477. if (value[haystack_index + needle_index] == find[needle_index])
  478. {
  479. needle_index++;
  480. // If we reach the end of the search term, return the current haystack index
  481. if (needle_index == find_length)
  482. return haystack_index;
  483. }
  484. else
  485. {
  486. // Advance haystack index by one and reset needle index.
  487. haystack_index++;
  488. needle_index = 0;
  489. }
  490. }
  491. return npos;
  492. }
  493. template< typename T >
  494. typename StringBase< T >::size_type StringBase< T >::_RFind(const T* find, size_type find_length, size_type offset) const
  495. {
  496. ROCKET_ASSERT(find_length > 0);
  497. size_type needle_index = 0;
  498. size_type haystack_index = (offset < length ? offset : length) - find_length;
  499. // If the find length is greater than the string we have, it can't be here
  500. if (find_length > length)
  501. return npos;
  502. // While theres still data in the haystack loop
  503. for (;;)
  504. {
  505. // If the current haystack index plus needle offset matches,
  506. // advance the needle index
  507. if (value[haystack_index + needle_index] == find[needle_index])
  508. {
  509. needle_index++;
  510. // If we reach the end of the search term, return the current haystack index
  511. if (find[needle_index] == 0)
  512. return haystack_index;
  513. }
  514. else
  515. {
  516. if (haystack_index == 0)
  517. return npos;
  518. // Advance haystack index backwards
  519. haystack_index--;
  520. needle_index = 0;
  521. }
  522. }
  523. }
  524. template< typename T >
  525. StringBase< T > StringBase< T >::_Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const
  526. {
  527. StringBase< T > result;
  528. size_type offset = 0;
  529. // Loop until we reach the end of the string
  530. while (offset < Length())
  531. {
  532. // Look for the next search term
  533. size_type pos = _Find(find, find_length, offset);
  534. // Term not found, add remainder and return
  535. if (pos == npos)
  536. return result + (Substring(offset).CString());
  537. // Add the unchanged text and replacement after it
  538. result += Substring(offset, pos - offset);
  539. result._Append(replace, replace_length);
  540. // Advance the find position
  541. offset = pos + find_length;
  542. }
  543. hash = 0;
  544. return result;
  545. }
  546. template< typename T >
  547. StringBase< T >& StringBase< T >::_Append(const T* append, size_type append_length, size_type count)
  548. {
  549. size_type add_length = count < append_length ? count : append_length;
  550. if (add_length == 0)
  551. return *this;
  552. Reserve(length + add_length);
  553. Copy(&value[length], append, add_length, true);
  554. length += add_length;
  555. hash = 0;
  556. return *this;
  557. }
  558. template< typename T >
  559. StringBase< T >& StringBase< T >::_Assign(const T* assign, size_type assign_length, size_type count)
  560. {
  561. size_type new_length = count < assign_length ? count : assign_length;
  562. if (new_length == 0)
  563. {
  564. Clear();
  565. }
  566. else
  567. {
  568. Reserve(new_length);
  569. Copy(value, assign, new_length, true);
  570. }
  571. length = new_length;
  572. hash = 0;
  573. return *this;
  574. }
  575. template< typename T >
  576. void StringBase< T >::_Insert(size_type index, const T* insert, size_type insert_length, size_type count)
  577. {
  578. if (index >= length)
  579. {
  580. Append(insert, count);
  581. return;
  582. }
  583. size_type add_length = count < insert_length ? count : insert_length;
  584. Reserve(length + add_length);
  585. for (size_type i = length + 1; i > index; i--)
  586. value[i + add_length - 1] = value[i - 1];
  587. Copy(&value[index], insert, add_length);
  588. length += add_length;
  589. hash = 0;
  590. }