1 |
/* |
2 |
* HT Editor |
3 |
* str.h |
4 |
* |
5 |
* Copyright (C) 2002 Stefan Weyergraf |
6 |
* Copyright (C) 2002, 2003 Sebastian Biallas (sb@biallas.net) |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 as |
10 |
* published by the Free Software Foundation. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
21 |
|
22 |
#ifndef __STR_H__ |
23 |
#define __STR_H__ |
24 |
|
25 |
#include "data.h" |
26 |
|
27 |
enum StringCase { |
28 |
stringCaseLower, |
29 |
stringCaseUpper, |
30 |
stringCaseCaps |
31 |
}; |
32 |
|
33 |
/** |
34 |
* Class for easy string handling. |
35 |
*/ |
36 |
class String: public Object { |
37 |
protected: |
38 |
int mLength; |
39 |
byte *mContent; |
40 |
public: |
41 |
String(); |
42 |
String(const char *s); |
43 |
String(const String *s); |
44 |
String(const String &s); |
45 |
String(const byte *s, int aLength); |
46 |
String(char c, int count = 1); |
47 |
virtual ~String(); |
48 |
|
49 |
void assign(const String *s); |
50 |
void assign(const String &s); |
51 |
void assign(const char *s); |
52 |
void assign(const byte *s, int aLength); |
53 |
void assign(char c, int count = 1); |
54 |
void assignFormat(const char *s, ...); |
55 |
|
56 |
void append(const String &s); |
57 |
void append(const char *s); |
58 |
void appendChar(char c); |
59 |
inline char & at(int aIndex) const; |
60 |
inline bool chop(); |
61 |
void clear(); |
62 |
virtual String * clone() const; |
63 |
virtual int compareChar(char c1, char c2) const; |
64 |
virtual int compareTo(const Object *o) const; |
65 |
int compare(const String &s) const; |
66 |
int compare(const String &s, int aMax) const; |
67 |
inline byte * content() const; |
68 |
inline char * contentChar() const; |
69 |
void crop(int aNewLength); |
70 |
void del(int pos, int aLength); |
71 |
void escape(const char *aSpecialChars, bool bit7 = true); |
72 |
virtual int findFirstChar(char c, int start = -1) const; |
73 |
virtual int findFirstString(const String &s, int start = -1) const; |
74 |
virtual int findLastChar(char c, int start = -1) const; |
75 |
virtual int findLastString(const String &s, int start = -1) const; |
76 |
inline char firstChar() const; |
77 |
void insert(const String &s, int pos); |
78 |
#ifdef HAVE_HT_OBJECTS |
79 |
virtual bool instanceOf(ObjectID id) const; |
80 |
inline bool isEmpty() const; |
81 |
virtual ObjectID getObjectID() const; |
82 |
#endif |
83 |
inline char lastChar() const; |
84 |
inline int length() const; |
85 |
bool leftSplit(char chr, String &initial, String &rem) const; |
86 |
void prepend(const String &s); |
87 |
// bool regexMatch(const String &aRegEx, Container *resultStrings = NULL, int maxRegExMatches = 32) const; |
88 |
// bool regexReplace(const String &aRegEx, Container *resultStrings = NULL) const; |
89 |
int replace(const String &what, const String &with, int start = 0, int maxReplacements = -1); |
90 |
bool rightSplit(char chr, String &initial, String &rem) const; |
91 |
int subString(int aStart, int aLength, String &result) const; |
92 |
void transformCase(StringCase c); |
93 |
void translate(const String &inAlpha, const String &outAlpha); |
94 |
virtual int toArray(byte *buf, int buflen) const; |
95 |
bool toInt(int &i, int defaultbase=10) const; |
96 |
bool toInt32(uint32 &u32, int defaultbase=10) const; |
97 |
bool toInt64(uint64 &u64, int defaultbase=10) const; |
98 |
virtual int toString(char *buf, int buflen) const; |
99 |
virtual char * toString() const; |
100 |
void unescape(); |
101 |
|
102 |
inline char & operator [](int aIndex) const; |
103 |
|
104 |
inline String & operator =(const String &s); |
105 |
inline String & operator =(const char *s); |
106 |
inline String & operator +=(const String &s); |
107 |
inline String & operator +=(const char *s); |
108 |
String & operator +=(char c); |
109 |
|
110 |
inline bool operator < (const String &s) const; |
111 |
inline bool operator > (const String &s) const; |
112 |
inline bool operator <=(const String &s) const; |
113 |
inline bool operator >=(const String &s) const; |
114 |
inline bool operator ==(const String &s) const; |
115 |
inline bool operator !=(const String &s) const; |
116 |
|
117 |
inline bool operator < (const char *s) const; |
118 |
inline bool operator > (const char *s) const; |
119 |
inline bool operator <=(const char *s) const; |
120 |
inline bool operator >=(const char *s) const; |
121 |
inline bool operator ==(const char *s) const; |
122 |
inline bool operator !=(const char *s) const; |
123 |
|
124 |
protected: |
125 |
int compare(const char *s) const; |
126 |
void realloc(int aNewSize); |
127 |
}; |
128 |
|
129 |
String operator +(const String &s1, const String &s2); |
130 |
String operator +(const char *s1, const String &s2); |
131 |
|
132 |
/** |
133 |
* case-insensitive string. |
134 |
*/ |
135 |
class IString: public String { |
136 |
public: |
137 |
IString(); |
138 |
|
139 |
virtual IString * clone() const; |
140 |
virtual int compareChar(char c1, char c2) const; |
141 |
#ifdef HAVE_HT_OBJECTS |
142 |
virtual bool instanceOf(ObjectID id) const; |
143 |
virtual ObjectID getObjectID() const; |
144 |
#endif |
145 |
}; |
146 |
|
147 |
/* |
148 |
* inline functions |
149 |
*/ |
150 |
|
151 |
//class MsgException; |
152 |
|
153 |
/** |
154 |
* @returns char at position |aIndex| |
155 |
* @throws exception if aIndex out of bounds |
156 |
*/ |
157 |
inline char &String::at(int aIndex) const |
158 |
{ |
159 |
// if ((uint)aIndex >= (uint)mLength) throw new MsgException("index out of bounds"); |
160 |
return (char &)mContent[aIndex]; |
161 |
} |
162 |
|
163 |
/** |
164 |
* Removes the last character of the string if string length is non-zero. |
165 |
*/ |
166 |
inline bool String::chop() |
167 |
{ |
168 |
if (mLength) { |
169 |
crop(mLength-1); |
170 |
return true; |
171 |
} else { |
172 |
return false; |
173 |
} |
174 |
} |
175 |
|
176 |
/** |
177 |
* @returns a string content ptr |
178 |
*/ |
179 |
inline byte *String::content() const |
180 |
{ |
181 |
return mContent; |
182 |
} |
183 |
|
184 |
/** |
185 |
* @returns a string content ptr (as char*) |
186 |
*/ |
187 |
inline char *String::contentChar() const |
188 |
{ |
189 |
return (char*)mContent; |
190 |
} |
191 |
|
192 |
/** |
193 |
* @returns first character of string |
194 |
* @throws exception if string is empty |
195 |
*/ |
196 |
inline char String::firstChar() const |
197 |
{ |
198 |
return at(0); |
199 |
} |
200 |
|
201 |
/** |
202 |
* @returns true if string is empty. |
203 |
*/ |
204 |
inline bool String::isEmpty() const |
205 |
{ |
206 |
return mLength == 0; |
207 |
} |
208 |
|
209 |
/** |
210 |
* @returns last character of string |
211 |
* @throws exception if string is empty |
212 |
*/ |
213 |
inline char String::lastChar() const |
214 |
{ |
215 |
return at(mLength-1); |
216 |
} |
217 |
|
218 |
/** |
219 |
* @returns length of string |
220 |
*/ |
221 |
inline int String::length() const |
222 |
{ |
223 |
return mLength; |
224 |
} |
225 |
|
226 |
inline char &String::operator [](int aIndex) const |
227 |
{ |
228 |
return at(aIndex); |
229 |
} |
230 |
|
231 |
inline String &String::operator =(const String &s) |
232 |
{ |
233 |
assign(s); |
234 |
return *this; |
235 |
} |
236 |
|
237 |
inline String &String::operator =(const char *s) |
238 |
{ |
239 |
assign(s); |
240 |
return *this; |
241 |
} |
242 |
|
243 |
inline String &String::operator +=(const String &s) |
244 |
{ |
245 |
append(s); |
246 |
return *this; |
247 |
} |
248 |
|
249 |
inline String &String::operator +=(const char *s) |
250 |
{ |
251 |
append(s); |
252 |
return *this; |
253 |
} |
254 |
|
255 |
inline bool String::operator < (const String &s) const |
256 |
{ |
257 |
return compare(s) < 0; |
258 |
} |
259 |
|
260 |
inline bool String::operator <= (const String &s) const |
261 |
{ |
262 |
return compare(s) <= 0; |
263 |
} |
264 |
|
265 |
inline bool String::operator > (const String &s) const |
266 |
{ |
267 |
return compare(s) > 0; |
268 |
} |
269 |
|
270 |
inline bool String::operator >= (const String &s) const |
271 |
{ |
272 |
return compare(s) >= 0; |
273 |
} |
274 |
|
275 |
inline bool String::operator == (const String &s) const |
276 |
{ |
277 |
return compare(s) == 0; |
278 |
} |
279 |
|
280 |
inline bool String::operator != (const String &s) const |
281 |
{ |
282 |
return compare(s) != 0; |
283 |
} |
284 |
|
285 |
inline bool String::operator < (const char *s) const |
286 |
{ |
287 |
return compare(s) < 0; |
288 |
} |
289 |
|
290 |
inline bool String::operator <= (const char *s) const |
291 |
{ |
292 |
return compare(s) <= 0; |
293 |
} |
294 |
|
295 |
inline bool String::operator > (const char *s) const |
296 |
{ |
297 |
return compare(s) > 0; |
298 |
} |
299 |
|
300 |
inline bool String::operator >= (const char *s) const |
301 |
{ |
302 |
return compare(s) >= 0; |
303 |
} |
304 |
|
305 |
inline bool String::operator == (const char *s) const |
306 |
{ |
307 |
return compare(s) == 0; |
308 |
} |
309 |
|
310 |
inline bool String::operator != (const char *s) const |
311 |
{ |
312 |
return compare(s) != 0; |
313 |
} |
314 |
|
315 |
#endif |