comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptStrings.h @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - String Objects
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30 ****************************************************************/
31
32 #ifndef _NPT_STRINGS_H_
33 #define _NPT_STRINGS_H_
34
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptConfig.h"
39 #if defined(NPT_CONFIG_HAVE_NEW_H)
40 #include <new>
41 #endif
42 #include "NptTypes.h"
43 #include "NptConstants.h"
44 #include "NptList.h"
45 #include "NptDebug.h"
46
47 /*----------------------------------------------------------------------
48 | constants
49 +---------------------------------------------------------------------*/
50 const int NPT_STRING_SEARCH_FAILED = -1;
51
52 /*----------------------------------------------------------------------
53 | NPT_String
54 +---------------------------------------------------------------------*/
55 class NPT_String
56 {
57 public:
58 // factories
59 static NPT_String FromInteger(NPT_Int64 value);
60 static NPT_String FromIntegerU(NPT_UInt64 value);
61 static NPT_String Format(const char* format, ...);
62
63 // constructors
64 NPT_String(const NPT_String& str);
65 NPT_String(const char* str);
66 NPT_String(const char* str, NPT_Size length);
67 NPT_String(const char* str, NPT_Ordinal first, NPT_Size length);
68 NPT_String(char c, NPT_Cardinal repeat = 1);
69 NPT_String() : m_Chars(NULL) {}
70 ~NPT_String() { if (m_Chars) delete GetBuffer(); }
71
72 // string info and manipulations
73 bool IsEmpty() const { return m_Chars == NULL || GetBuffer()->GetLength() == 0; }
74 NPT_Size GetLength() const { return m_Chars ? GetBuffer()->GetLength() : 0; }
75 NPT_Size GetCapacity() const { return m_Chars ? GetBuffer()->GetAllocated() : 0; }
76 NPT_Result SetLength(NPT_Size length, bool pad = false);
77 void Assign(const char* chars, NPT_Size size);
78 void Append(const char* chars, NPT_Size size);
79 void Append(const char* s) { Append(s, StringLength(s)); }
80 int Compare(const char* s, bool ignore_case = false) const;
81 static int Compare(const char* s1, const char* s2, bool ignore_case = false);
82 int CompareN(const char* s, NPT_Size count, bool ignore_case = false) const;
83 static int CompareN(const char* s1, const char* s2, NPT_Size count, bool ignore_case = false);
84
85 // substrings
86 NPT_String SubString(NPT_Ordinal first, NPT_Size length) const;
87 NPT_String SubString(NPT_Ordinal first) const {
88 return SubString(first, GetLength());
89 }
90 NPT_String Left(NPT_Size length) const {
91 return SubString(0, length);
92 }
93 NPT_String Right(NPT_Size length) const {
94 return length >= GetLength() ?
95 *this :
96 SubString(GetLength()-length, length);
97 }
98 NPT_List<NPT_String> Split(const char* separator) const;
99
100 // buffer management
101 void Reserve(NPT_Size length);
102
103 // conversions
104 NPT_String ToLowercase() const;
105 NPT_String ToUppercase() const;
106 NPT_Result ToInteger(NPT_Int32& value, bool relaxed = true) const;
107 NPT_Result ToInteger(NPT_UInt32& value, bool relaxed = true) const;
108 NPT_Result ToInteger(NPT_Int64& value, bool relaxed = true) const;
109 NPT_Result ToInteger(NPT_UInt64& value, bool relaxed = true) const;
110 NPT_Result ToFloat(float& value, bool relaxed = true) const;
111
112 // processing
113 void MakeLowercase();
114 void MakeUppercase();
115 void Replace(char a, char b);
116 void Replace(char a, const char* str);
117
118 // search
119 int Find(char c, NPT_Ordinal start = 0, bool ignore_case = false) const;
120 int Find(const char* s, NPT_Ordinal start = 0, bool ignore_case = false) const;
121 int ReverseFind(char c, NPT_Ordinal start = 0, bool ignore_case = false) const;
122 int ReverseFind(const char* s, NPT_Ordinal start = 0, bool ignore_case = false) const;
123 bool StartsWith(const char* s, bool ignore_case = false) const;
124 bool EndsWith(const char* s, bool ignore_case = false) const;
125
126 // editing
127 void Insert(const char* s, NPT_Ordinal where = 0);
128 void Erase(NPT_Ordinal start, NPT_Cardinal count = 1);
129 void Replace(NPT_Ordinal start, NPT_Cardinal count, const char* s);
130 void TrimLeft();
131 void TrimLeft(char c);
132 void TrimLeft(const char* chars);
133 void TrimRight();
134 void TrimRight(char c);
135 void TrimRight(const char* chars);
136 void Trim();
137 void Trim(char c);
138 void Trim(const char* chars);
139
140 // type casting
141 operator char*() const { return m_Chars ? m_Chars: &EmptyString; }
142 operator const char* () const { return m_Chars ? m_Chars: &EmptyString; }
143 const char* GetChars() const { return m_Chars ? m_Chars: &EmptyString; }
144 char* UseChars() { return m_Chars ? m_Chars: &EmptyString; }
145
146 // operator overloading
147 NPT_String& operator=(const char* str);
148 NPT_String& operator=(const NPT_String& str);
149 NPT_String& operator=(char c);
150 const NPT_String& operator+=(const NPT_String& s) {
151 Append(s.GetChars(), s.GetLength());
152 return *this;
153 }
154 const NPT_String& operator+=(const char* s) {
155 Append(s);
156 return *this;
157 }
158 const NPT_String& operator+=(char c) {
159 Append(&c, 1);
160 return *this;
161 }
162 char operator[](int index) const {
163 NPT_ASSERT((unsigned int)index < GetLength());
164 return GetChars()[index];
165 }
166 char& operator[](int index) {
167 NPT_ASSERT((unsigned int)index < GetLength());
168 return UseChars()[index];
169 }
170
171 // friend operators
172 friend NPT_String operator+(const NPT_String& s1, const NPT_String& s2) {
173 return s1+s2.GetChars();
174 }
175 friend NPT_String operator+(const NPT_String& s1, const char* s2);
176 friend NPT_String operator+(const char* s1, const NPT_String& s2);
177 friend NPT_String operator+(const NPT_String& s, char c);
178 friend NPT_String operator+(char c, const NPT_String& s);
179
180 protected:
181 // inner classes
182 class Buffer {
183 public:
184 // class methods
185 static Buffer* Allocate(NPT_Size allocated, NPT_Size length) {
186 void* mem = ::operator new(sizeof(Buffer)+allocated+1);
187 return new(mem) Buffer(allocated, length);
188 }
189 static char* Create(NPT_Size allocated, NPT_Size length=0) {
190 Buffer* shared = Allocate(allocated, length);
191 return shared->GetChars();
192 }
193 static char* Create(const char* copy) {
194 NPT_Size length = StringLength(copy);
195 Buffer* shared = Allocate(length, length);
196 CopyString(shared->GetChars(), copy);
197 return shared->GetChars();
198 }
199 static char* Create(const char* copy, NPT_Size length) {
200 Buffer* shared = Allocate(length, length);
201 CopyBuffer(shared->GetChars(), copy, length);
202 shared->GetChars()[length] = '\0';
203 return shared->GetChars();
204 }
205 static char* Create(char c, NPT_Cardinal repeat) {
206 Buffer* shared = Allocate(repeat, repeat);
207 char* s = shared->GetChars();
208 while (repeat--) {
209 *s++ = c;
210 }
211 *s = '\0';
212 return shared->GetChars();
213 }
214
215 // methods
216 char* GetChars() {
217 // return a pointer to the first char
218 return reinterpret_cast<char*>(this+1);
219 }
220 NPT_Size GetLength() const { return m_Length; }
221 void SetLength(NPT_Size length) { m_Length = length; }
222 NPT_Size GetAllocated() const { return m_Allocated; }
223
224 private:
225 // methods
226 Buffer(NPT_Size allocated, NPT_Size length = 0) :
227 m_Length(length),
228 m_Allocated(allocated) {}
229
230 // members
231 NPT_Cardinal m_Length;
232 NPT_Cardinal m_Allocated;
233 // the actual string data follows
234
235 };
236
237 // members
238 char* m_Chars;
239
240 private:
241 // friends
242 friend class Buffer;
243
244 // static members
245 static char EmptyString;
246
247 // methods
248 Buffer* GetBuffer() const {
249 return reinterpret_cast<Buffer*>(m_Chars)-1;
250 }
251 void Reset() {
252 if (m_Chars != NULL) {
253 delete GetBuffer();
254 m_Chars = NULL;
255 }
256 }
257 char* PrepareToWrite(NPT_Size length);
258 void PrepareToAppend(NPT_Size length, NPT_Size allocate);
259
260 // static methods
261 static void CopyString(char* dst, const char* src) {
262 while ((*dst++ = *src++)){}
263 }
264
265 static void CopyBuffer(char* dst, const char* src, NPT_Size size) {
266 while (size--) *dst++ = *src++;
267 }
268
269 static NPT_Size StringLength(const char* str) {
270 NPT_Size length = 0;
271 while (*str++) length++;
272 return length;
273 }
274 };
275
276 /*----------------------------------------------------------------------
277 | external operators
278 +---------------------------------------------------------------------*/
279 inline bool operator==(const NPT_String& s1, const NPT_String& s2) {
280 return s1.Compare(s2) == 0;
281 }
282 inline bool operator==(const NPT_String& s1, const char* s2) {
283 return s1.Compare(s2) == 0;
284 }
285 inline bool operator==(const char* s1, const NPT_String& s2) {
286 return s2.Compare(s1) == 0;
287 }
288 inline bool operator!=(const NPT_String& s1, const NPT_String& s2) {
289 return s1.Compare(s2) != 0;
290 }
291 inline bool operator!=(const NPT_String& s1, const char* s2) {
292 return s1.Compare(s2) != 0;
293 }
294 inline bool operator!=(const char* s1, const NPT_String& s2) {
295 return s2.Compare(s1) != 0;
296 }
297 inline bool operator<(const NPT_String& s1, const NPT_String& s2) {
298 return s1.Compare(s2) < 0;
299 }
300 inline bool operator<(const NPT_String& s1, const char* s2) {
301 return s1.Compare(s2) < 0;
302 }
303 inline bool operator<(const char* s1, const NPT_String& s2) {
304 return s2.Compare(s1) > 0;
305 }
306 inline bool operator>(const NPT_String& s1, const NPT_String& s2) {
307 return s1.Compare(s2) > 0;
308 }
309 inline bool operator>(const NPT_String& s1, const char* s2) {
310 return s1.Compare(s2) > 0;
311 }
312 inline bool operator>(const char* s1, const NPT_String& s2) {
313 return s2.Compare(s1) < 0;
314 }
315 inline bool operator<=(const NPT_String& s1, const NPT_String& s2) {
316 return s1.Compare(s2) <= 0;
317 }
318 inline bool operator<=(const NPT_String& s1, const char* s2) {
319 return s1.Compare(s2) <= 0;
320 }
321 inline bool operator<=(const char* s1, const NPT_String& s2) {
322 return s2.Compare(s1) >= 0;
323 }
324 inline bool operator>=(const NPT_String& s1, const NPT_String& s2) {
325 return s1.Compare(s2) >= 0;
326 }
327 inline bool operator>=(const NPT_String& s1, const char* s2) {
328 return s1.Compare(s2) >= 0;
329 }
330 inline bool operator>=(const char* s1, const NPT_String& s2) {
331 return s2.Compare(s1) <= 0;
332 }
333
334 #endif // _NPT_STRINGS_H_