StringBase.h 8.7 KB

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