File: StringBase.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (219 lines) | stat: -rw-r--r-- 8,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#ifndef ROCKETCORESTRINGBASE_H
#define ROCKETCORESTRINGBASE_H

#include "Debug.h"
#include <stdlib.h>

namespace Rocket {
namespace Core {

/**
	Base String Implementation
	@author Lloyd Weehuizen
 */
template< typename T >
class StringBase
{
public:
	typedef size_t size_type;
	static const size_type npos = (size_type)-1;

	StringBase();
	StringBase(const StringBase& copy);
	StringBase(const T* string);
	StringBase(const T* string_start, const T* string_end);
	StringBase(size_type length, const T character);
	StringBase(size_type max_length, const T* fmt, ...);

	~StringBase();

	/// Is the string currently empty
	inline bool Empty() const;
	/// Clear the string to empty
	void Clear();

	/// The length of the string
	inline size_type Length() const;
	/// Get the hash value of this string
	inline unsigned int Hash() const;
	/// Access the string as a standard C string
	inline const T* CString() const;
	
	/// Reserve space for at least this much data
	inline void Reserve(size_type size);

	/// Find the given string within this string
	/// @param find The string to search for
	/// @param offset Starting location of the search
	size_type Find(const T* find, size_type offset = 0) const;
	/// Find the given string within this string
	/// @param find The string to search for
	/// @param offset Starting location of the search
	size_type Find(const StringBase<T>& find, size_type offset = 0) const;
	/// Reverse find the given string within this string
	/// @param find The string to search for
	/// @param offset Starting location of the search
	size_type RFind(const T* find, size_type offset = npos) const;
	/// Reverse find the given string within this string
	/// @param find The string to search for
	/// @param offset Starting location of the search
	size_type RFind(const StringBase<T>& find, size_type offset = npos) const;

	/// Replace all occurances of the given string with another
	/// @param find The string to search for
	/// @param replace The string to replace it with
	StringBase<T> Replace(const T* find, const T* replace) const;
	/// Replace all occurances of the given string with another
	/// @param find The string to search for
	/// @param replace The string to replace it with
	StringBase<T> Replace(const StringBase<T>& find, const StringBase<T>& replace) const;

	/// Return a substring of this string
	/// @param start The starting position
	/// @param length The number of characters to copy
	inline StringBase<T> Substring(size_type start, size_type length = StringBase<T>::npos) const;

	/// Append the given string to this string
	/// @param append The string to appen
	/// @param count The number of characters to append
	inline StringBase<T>& Append(const T* append, size_type count = StringBase<T>::npos);
	/// Assign the given string to this string
	/// @param assign The string to assign
	/// @param count The number of characters to assign
	inline StringBase<T>& Append(const StringBase<T>& append, size_type count = StringBase<T>::npos);
	/// Append a single character
	/// @param append The character to append
	inline StringBase<T>& Append(const T& append);

	/// Assign the given string to this string
	/// @param assign The string to assign
	/// @param length The number of characters to assign
	inline StringBase<T>& Assign(const T* assign, size_type count = StringBase<T>::npos);
	/// Assign the given string to this string
	/// @param assign The string to assign
	/// @param count The number of characters to assign
	inline StringBase<T>& Assign(const T* assign, const T* end);
	/// Assign the given string to this string
	/// @param assign The string to assign
	/// @param count The number of characters to assign
	inline StringBase<T>& Assign(const StringBase<T>& assign, size_type count = StringBase<T>::npos);

	/// Insert a string into this string
	/// @param index Index to insert the characters
	/// @param insert String to insert
	/// @param count Number of characters to insert
	inline void Insert(size_type index, const T* insert, size_type count = StringBase<T>::npos);
	/// Insert a string into this string
	/// @param index Index to insert the characters
	/// @param insert String to insert
	/// @param count Number of characters to insert
	inline void Insert(size_type index, const StringBase<T>& insert, size_type count = StringBase<T>::npos);
	/// Insert a character into this string
	/// @param index Index to insert the characters
	/// @param insert Character to insert	
	inline void Insert(size_type index, const T& insert);

	/// Erase characters from this string
	/// @param index Index to erase the characters
	/// @param length Number of characters to erase
	inline void Erase(size_type index, size_type length = StringBase<T>::npos);

	/// sprsize_typef style string formatting.
	/// NOTE: This is not implemented in the base layer and requires template
	/// specialisation of the specific string type
	/// @param max_length Maximum length of the result
	/// @param format The sprsize_typef style formatting
	int FormatString(size_type max_length, const T* format, ...);

	/// Resize the string to the given size, inserts space if the string is getting bigger
	/// @param size New size
	void Resize(size_type size);

	/// Create a lowercase version of the string
	/// @returns The lower case representation of the string
	StringBase<T> ToLower() const;
	/// Create a lowercase version of the string
	/// @returns The lower case representation of the string
	StringBase<T> ToUpper() const;

	inline bool operator==(const T* compare) const;
	inline bool operator==(const StringBase<T>& compare) const;

	inline bool operator!=(const T* compare) const;
	inline bool operator!=(const StringBase<T>& compare) const;

	inline bool operator<(const T* compare) const;
	inline bool operator<(const StringBase<T>& compare) const;

	inline StringBase<T>& operator=(const T* assign);
	inline StringBase<T>& operator=(const StringBase<T>& assign);

	inline StringBase<T> operator+(const T* append) const;
	inline StringBase<T> operator+(const StringBase<T>& append) const;

	inline StringBase<T>& operator+=(const T* append);
	inline StringBase<T>& operator+=(const StringBase<T>& append);
	inline StringBase<T>& operator+=(const T& append);

	inline const T& operator[](size_type index) const;
	inline T& operator[](size_type index);

protected:	

	T* value;
	size_type buffer_size;
	size_type length;
	mutable unsigned int hash;
	static const size_type LOCAL_BUFFER_SIZE = 8;
	char local_buffer[LOCAL_BUFFER_SIZE];

	size_type GetLength(const T* string) const;

	// Copies the source string to target string
	inline void Copy(T* target, const T* src, size_type length, bool terminate = false);

	// Internal implementations of the public interfaces,
	// all these functions take the length of the const T*'s they're
	// dealing with which *MUST* be accurate.
	// Its up to the external interfaces to provide valid values for these functions
	inline size_type _Find(const T* find, size_type find_length, size_type offset = 0) const;
	inline size_type _RFind(const T* find, size_type find_length, size_type offset = 0) const;
	inline StringBase<T> _Replace(const T* find, size_type find_length, const T* replace, size_type replace_length) const;
	inline StringBase<T>& _Append(const T* append, size_type append_length, size_type count = StringBase<T>::npos);
	inline StringBase<T>& _Assign(const T* assign, size_type assign_length, size_type count = StringBase<T>::npos);
	inline void _Insert(size_type index, const T* insert, size_type insert_length, size_type count = StringBase<T>::npos);
};
	
#include "StringBase.inl"

}
}

#endif