StringBase.h 8.8 KB

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