StringBase.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifndef ROCKETCORESTRINGBASE_H
  28. #define ROCKETCORESTRINGBASE_H
  29. #include "Debug.h"
  30. #include <stdlib.h>
  31. namespace Rocket {
  32. namespace Core {
  33. /**
  34. Base String Implementation
  35. @author Lloyd Weehuizen
  36. */
  37. template< typename T >
  38. class StringBase
  39. {
  40. public:
  41. typedef size_t size_type;
  42. static const size_type npos = (size_type)-1;
  43. StringBase();
  44. StringBase(const StringBase& copy);
  45. StringBase(const T* string);
  46. StringBase(const T* string_start, const T* string_end);
  47. StringBase(size_type length, const T character);
  48. StringBase(size_type max_length, const T* fmt, ...);
  49. ~StringBase();
  50. /// Is the string currently empty
  51. inline bool Empty() const;
  52. /// Clear the string to empty
  53. void Clear();
  54. /// The length of the string
  55. inline size_type Length() const;
  56. /// Get the hash value of this string
  57. inline unsigned int Hash() const;
  58. /// Access the string as a standard C string
  59. inline const T* CString() const;
  60. /// Reserve space for at least this much data
  61. inline void Reserve(size_type size);
  62. /// Find the given string within this string
  63. /// @param find The string to search for
  64. /// @param offset Starting location of the search
  65. size_type Find(const T* find, size_type offset = 0) const;
  66. /// Find the given string within this string
  67. /// @param find The string to search for
  68. /// @param offset Starting location of the search
  69. size_type Find(const StringBase<T>& find, size_type offset = 0) const;
  70. /// Reverse find the given string within this string
  71. /// @param find The string to search for
  72. /// @param offset Starting location of the search
  73. size_type RFind(const T* find, size_type offset = npos) const;
  74. /// Reverse find the given string within this string
  75. /// @param find The string to search for
  76. /// @param offset Starting location of the search
  77. size_type RFind(const StringBase<T>& find, size_type offset = npos) const;
  78. /// Replace all occurances of the given string with another
  79. /// @param find The string to search for
  80. /// @param replace The string to replace it with
  81. StringBase<T> Replace(const T* find, const T* replace) const;
  82. /// Replace all occurances of the given string with another
  83. /// @param find The string to search for
  84. /// @param replace The string to replace it with
  85. StringBase<T> Replace(const StringBase<T>& find, const StringBase<T>& replace) const;
  86. /// Return a substring of this string
  87. /// @param start The starting position
  88. /// @param length The number of characters to copy
  89. inline StringBase<T> Substring(size_type start, size_type length = StringBase<T>::npos) const;
  90. /// Append the given string to this string
  91. /// @param append The string to appen
  92. /// @param count The number of characters to append
  93. inline StringBase<T>& Append(const T* append, size_type count = StringBase<T>::npos);
  94. /// Assign the given string to this string
  95. /// @param assign The string to assign
  96. /// @param count The number of characters to assign
  97. inline StringBase<T>& Append(const StringBase<T>& append, size_type count = StringBase<T>::npos);
  98. /// Append a single character
  99. /// @param append The character to append
  100. inline StringBase<T>& Append(const T& append);
  101. /// Assign the given string to this string
  102. /// @param assign The string to assign
  103. /// @param length The number of characters to assign
  104. inline StringBase<T>& Assign(const T* assign, size_type count = StringBase<T>::npos);
  105. /// Assign the given string to this string
  106. /// @param assign The string to assign
  107. /// @param count The number of characters to assign
  108. inline StringBase<T>& Assign(const T* assign, const T* end);
  109. /// Assign the given string to this string
  110. /// @param assign The string to assign
  111. /// @param count The number of characters to assign
  112. inline StringBase<T>& Assign(const StringBase<T>& assign, size_type count = StringBase<T>::npos);
  113. /// Insert a string into this string
  114. /// @param index Index to insert the characters
  115. /// @param insert String to insert
  116. /// @param count Number of characters to insert
  117. inline void Insert(size_type index, const T* insert, size_type count = StringBase<T>::npos);
  118. /// Insert a string into this string
  119. /// @param index Index to insert the characters
  120. /// @param insert String to insert
  121. /// @param count Number of characters to insert
  122. inline void Insert(size_type index, const StringBase<T>& insert, size_type count = StringBase<T>::npos);
  123. /// Insert a character into this string
  124. /// @param index Index to insert the characters
  125. /// @param insert Character to insert
  126. inline void Insert(size_type index, const T& insert);
  127. /// Erase characters from this string
  128. /// @param index Index to erase the characters
  129. /// @param length Number of characters to erase
  130. inline void Erase(size_type index, size_type length = StringBase<T>::npos);
  131. /// sprsize_typef style string formatting.
  132. /// NOTE: This is not implemented in the base layer and requires template
  133. /// specialisation of the specific string type
  134. /// @param max_length Maximum length of the result
  135. /// @param format The sprsize_typef style formatting
  136. int FormatString(size_type max_length, const T* format, ...);
  137. /// Resize the string to the given size, inserts space if the string is getting bigger
  138. /// @param size New size
  139. void Resize(size_type size);
  140. /// Create a lowercase version of the string
  141. /// @returns The lower case representation of the string
  142. StringBase<T> ToLower() const;
  143. /// Create a lowercase version of the string
  144. /// @returns The lower case representation of the string
  145. StringBase<T> ToUpper() const;
  146. inline bool operator==(const T* compare) const;
  147. inline bool operator==(const StringBase<T>& compare) const;
  148. inline bool operator!=(const T* compare) const;
  149. inline bool operator!=(const StringBase<T>& compare) const;
  150. inline bool operator<(const T* compare) const;
  151. inline bool operator<(const StringBase<T>& compare) const;
  152. inline StringBase<T>& operator=(const T* assign);
  153. inline StringBase<T>& operator=(const StringBase<T>& assign);
  154. inline StringBase<T> operator+(const T* append) const;
  155. inline StringBase<T> operator+(const StringBase<T>& append) const;
  156. inline StringBase<T>& operator+=(const T* append);
  157. inline StringBase<T>& operator+=(const StringBase<T>& append);
  158. inline StringBase<T>& operator+=(const T& append);
  159. inline const T& operator[](size_type index) const;
  160. inline T& operator[](size_type index);
  161. protected:
  162. T* value;
  163. size_type buffer_size;
  164. size_type length;
  165. mutable unsigned int hash;
  166. static const size_type LOCAL_BUFFER_SIZE = 8;
  167. char local_buffer[LOCAL_BUFFER_SIZE];
  168. size_type GetLength(const T* string) const;
  169. // Copies the source string to target string
  170. inline void Copy(T* target, const T* src, size_type length, bool terminate = false);
  171. // Internal implementations of the public interfaces,
  172. // all these functions take the length of the const T*'s they're
  173. // dealing with which *MUST* be accurate.
  174. // Its up to the external interfaces to provide valid values for these functions
  175. inline size_type _Find(const T* find, size_type find_length, size_type offset = 0) const;
  176. inline size_type _RFind(const T* find, size_type find_length, size_type offset = 0) const;
  177. inline StringBase<T> _Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const;
  178. inline StringBase<T>& _Append(const T* append, size_type append_length, size_type count = StringBase<T>::npos);
  179. inline StringBase<T>& _Assign(const T* assign, size_type assign_length, size_type count = StringBase<T>::npos);
  180. inline void _Insert(size_type index, const T* insert, size_type insert_length, size_type count = StringBase<T>::npos);
  181. };
  182. #include "StringBase.inl"
  183. }
  184. }
  185. #endif