libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StringBase.h
Go to the documentation of this file.
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 
28 #ifndef ROCKETCORESTRINGBASE_H
29 #define ROCKETCORESTRINGBASE_H
30 
31 #include "Debug.h"
32 #include <stdlib.h>
33 
34 namespace Rocket {
35 namespace Core {
36 
41 template< typename T >
43 {
44 public:
45  typedef size_t size_type;
46  static const size_type npos = (size_type)-1;
47 
48  StringBase();
49  StringBase(const StringBase& copy);
50  StringBase(const T* string);
51  StringBase(const T* string_start, const T* string_end);
52  StringBase(size_type length, const T character);
53  StringBase(size_type max_length, const T* fmt, ...);
54 
55  ~StringBase();
56 
58  inline bool Empty() const;
60  void Clear();
61 
63  inline size_type Length() const;
65  inline unsigned int Hash() const;
67  inline const T* CString() const;
68 
70  inline void Reserve(size_type size);
71 
75  size_type Find(const T* find, size_type offset = 0) const;
79  size_type Find(const StringBase<T>& find, size_type offset = 0) const;
83  size_type RFind(const T* find, size_type offset = npos) const;
87  size_type RFind(const StringBase<T>& find, size_type offset = npos) const;
88 
92  StringBase<T> Replace(const T* find, const T* replace) const;
96  StringBase<T> Replace(const StringBase<T>& find, const StringBase<T>& replace) const;
97 
101  inline StringBase<T> Substring(size_type start, size_type length = StringBase<T>::npos) const;
106  inline StringBase<T>& Append(const T* append, size_type count = StringBase<T>::npos);
110  inline StringBase<T>& Append(const StringBase<T>& append, size_type count = StringBase<T>::npos);
113  inline StringBase<T>& Append(const T& append);
118  inline StringBase<T>& Assign(const T* assign, size_type count = StringBase<T>::npos);
122  inline StringBase<T>& Assign(const T* assign, const T* end);
126  inline StringBase<T>& Assign(const StringBase<T>& assign, size_type count = StringBase<T>::npos);
127 
132  inline void Insert(size_type index, const T* insert, size_type count = StringBase<T>::npos);
137  inline void Insert(size_type index, const StringBase<T>& insert, size_type count = StringBase<T>::npos);
141  inline void Insert(size_type index, const T& insert);
142 
146  inline void Erase(size_type index, size_type length = StringBase<T>::npos);
153  int FormatString(size_type max_length, const T* format, ...);
154 
157  void Resize(size_type size);
158 
161  StringBase<T> ToLower() const;
164  StringBase<T> ToUpper() const;
165 
166  inline bool operator==(const T* compare) const;
167  inline bool operator==(const StringBase<T>& compare) const;
168 
169  inline bool operator!=(const T* compare) const;
170  inline bool operator!=(const StringBase<T>& compare) const;
171 
172  inline bool operator<(const T* compare) const;
173  inline bool operator<(const StringBase<T>& compare) const;
174 
175  inline StringBase<T>& operator=(const T* assign);
176  inline StringBase<T>& operator=(const StringBase<T>& assign);
177 
178  inline StringBase<T> operator+(const T* append) const;
179  inline StringBase<T> operator+(const StringBase<T>& append) const;
180 
181  inline StringBase<T>& operator+=(const T* append);
182  inline StringBase<T>& operator+=(const StringBase<T>& append);
183  inline StringBase<T>& operator+=(const T& append);
184 
185  inline const T& operator[](size_type index) const;
186  inline T& operator[](size_type index);
187 
188 protected:
189 
190  T* value;
191  size_type buffer_size;
192  size_type length;
193  mutable unsigned int hash;
194  static const size_type LOCAL_BUFFER_SIZE = 8;
196 
197  size_type GetLength(const T* string) const;
198 
199  // Copies the source string to target string
200  inline void Copy(T* target, const T* src, size_type length, bool terminate = false);
201 
202  // Internal implementations of the public interfaces,
203  // all these functions take the length of the const T*'s they're
204  // dealing with which *MUST* be accurate.
205  // Its up to the external interfaces to provide valid values for these functions
206  inline size_type _Find(const T* find, size_type find_length, size_type offset = 0) const;
207  inline size_type _RFind(const T* find, size_type find_length, size_type offset = 0) const;
208  inline StringBase<T> _Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const;
209  inline StringBase<T>& _Append(const T* append, size_type append_length, size_type count = StringBase<T>::npos);
210  inline StringBase<T>& _Assign(const T* assign, size_type assign_length, size_type count = StringBase<T>::npos);
211  inline void _Insert(size_type index, const T* insert, size_type insert_length, size_type count = StringBase<T>::npos);
212 };
213 
214 #include "StringBase.inl"
215 
216 }
217 }
219 #endif
size_type buffer_size
Definition: StringBase.h:191
size_type Find(const T *find, size_type offset=0) const
Definition: StringBase.h:188
StringBase< T > & operator=(const T *assign)
Definition: StringBase.h:477
StringBase< T > & operator+=(const T *append)
Definition: StringBase.h:509
StringBase< T > & Append(const T *append, size_type count=StringBase< T >::npos)
Definition: StringBase.h:237
bool operator==(const T *compare) const
Definition: StringBase.h:405
size_type RFind(const T *find, size_type offset=npos) const
Definition: StringBase.h:200
void Erase(size_type index, size_type length=StringBase< T >::npos)
Erase characters from this string.
Definition: StringBase.h:297
StringBase(const T *string_start, const T *string_end)
Definition: StringBase.h:50
StringBase< T > Replace(const T *find, const T *replace) const
Definition: StringBase.h:212
StringBase< T > & _Assign(const T *assign, size_type assign_length, size_type count=StringBase< T >::npos)
Definition: StringBase.h:686
StringBase< T > _Replace(const T *find, size_type find_length, const T *replace, size_type replace_length) const
Definition: StringBase.h:640
void Reserve(size_type size)
Reserve space for at least this much data.
Definition: StringBase.h:153
~StringBase()
Definition: StringBase.h:89
const T * CString() const
Access the string as a standard C string.
Definition: StringBase.h:147
size_type Length() const
The length of the string.
Definition: StringBase.h:114
Definition: Clipboard.h:33
StringBase< T > & Assign(const T *assign, size_type count=StringBase< T >::npos)
Definition: StringBase.h:256
StringBase< T > ToLower() const
Definition: StringBase.h:342
StringBase< T > & _Append(const T *append, size_type append_length, size_type count=StringBase< T >::npos)
Definition: StringBase.h:669
void _Insert(size_type index, const T *insert, size_type insert_length, size_type count=StringBase< T >::npos)
Definition: StringBase.h:708
T * value
Definition: StringBase.h:190
static const size_type npos
Definition: StringBase.h:46
StringBase()
Definition: StringBase.h:30
size_t size_type
Definition: StringBase.h:45
bool Empty() const
Is the string currently empty.
Definition: StringBase.h:96
static const size_type LOCAL_BUFFER_SIZE
Definition: StringBase.h:194
void Insert(size_type index, const T *insert, size_type count=StringBase< T >::npos)
Definition: StringBase.h:276
bool operator<(const T *compare) const
Definition: StringBase.h:440
unsigned int hash
Definition: StringBase.h:193
StringBase< T > ToUpper() const
Definition: StringBase.h:374
StringBase< T > operator+(const T *append) const
Definition: StringBase.h:491
StringBase< T > Substring(size_type start, size_type length=StringBase< T >::npos) const
Definition: StringBase.h:224
unsigned int Hash() const
Get the hash value of this string.
Definition: StringBase.h:120
int FormatString(size_type max_length, const T *format,...)
size_type _Find(const T *find, size_type find_length, size_type offset=0) const
Definition: StringBase.h:569
Definition: StringBase.h:42
void Clear()
Clear the string to empty.
Definition: StringBase.h:102
void Copy(T *target, const T *src, size_type length, bool terminate=false)
Definition: StringBase.h:554
size_type _RFind(const T *find, size_type find_length, size_type offset=0) const
Definition: StringBase.h:603
size_type length
Definition: StringBase.h:192
size_type GetLength(const T *string) const
Definition: StringBase.h:541
char local_buffer[LOCAL_BUFFER_SIZE]
Definition: StringBase.h:195
void Resize(size_type size)
Definition: StringBase.h:330
bool operator!=(const T *compare) const
Definition: StringBase.h:428
const T & operator[](size_type index) const
Definition: StringBase.h:527