File: str.h

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (316 lines) | stat: -rw-r--r-- 7,671 bytes parent folder | download | duplicates (4)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 *	HT Editor
 *	str.h
 *
 *	Copyright (C) 2002 Stefan Weyergraf (stefan@weyergraf.de)
 *	Copyright (C) 2002, 2003 Sebastian Biallas (sb@biallas.net)
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __STR_H__
#define __STR_H__

#include "data.h"

enum StringCase {
	stringCaseLower,
	stringCaseUpper,
	stringCaseCaps
};

/**
 *	Class for easy string handling.
 */
class String: public Object {
protected:
	int mLength;
	byte *mContent;
public:
				String(BuildCtorArg &a): Object(a) {};
				String();
				String(const char *s);
				String(const String *s);
				String(const String &s);
				String(const byte *s, int aLength);
				String(char c, int count = 1);
	virtual			~String();

		void		assign(const String *s);
		void		assign(const String &s);
		void		assign(const char *s);
		void		assign(const byte *s, int aLength);
		void		assign(char c, int count = 1);
		void		assignFormat(const char *s, ...);

		void		append(const String &s);
		void		append(const char *s);
		void		append(const byte *s, int aLength);
		void		appendChar(char c);
		void		appendFormat(const char *s, ...);
	inline	char &		at(int aIndex) const;
	inline	bool		chop();
		void		clear();
	virtual	String *	clone() const;
	virtual	int		compareChar(char c1, char c2) const;
	virtual	int		compareTo(const Object *o) const;
		int		compare(const String &s) const;
		int		compare(const String &s, int aMax) const;
	inline	byte *		content() const;
	inline	char *		contentChar() const;
		uint		countChar(char c) const;
		void		crop(int aNewLength);
		void		del(int pos, int aLength);
		void		escape(const char *aSpecialChars, bool bit7 = true);
	virtual	int		findCharFwd(char c, int start = -1, int ith_match = 0) const;
	virtual	int		findStringFwd(const String &s, int start = -1, int ith_match = 0) const;
	virtual	int		findCharBwd(char c, int start = -1, int ith_match = 0) const;
	virtual	int		findStringBwd(const String &s, int start = -1, int ith_match = 0) const;
	inline	char		firstChar() const;
		void		insert(const String &s, int pos);
	inline	bool		isEmpty() const;
	virtual	void		load(ObjectStream &s);
	virtual	ObjectID	getObjectID() const;
		void		grab(String &s);
		byte *		grabContent();
		char *		grabContentChar();
	virtual	void		store(ObjectStream &s) const;
	inline	char		lastChar() const;
	inline	int		length() const;
		bool		leftSplit(char chr, String &initial, String &rem) const;
		void		prepend(const String &s);
		bool		regexMatch(const String &aRegEx, Container *resultStrings = NULL) const;
//		bool		regexReplace(const String &aRegEx, Container *resultStrings = NULL) const;
		int		replace(String &what, String &with);
		bool		rightSplit(char chr, String &initial, String &rem) const;
		int		subString(int aStart, int aLength, String &result) const;
		void		transformCase(StringCase c);
		void		translate(const String &inAlpha, const String &outAlpha);
	virtual	int		toArray(byte *buf, int buflen) const;
		bool		toInt32(uint32 &u32, int defaultbase) const;
		bool		toInt64(uint64 &u64, int defaultbase) const;
	virtual	int		toString(char *buf, int buflen) const;
	virtual	char *		toString() const;
		void		unescape();

	inline	char &	operator [](int aIndex) const;

	inline	String &	operator =(const String &s);
	inline	String &	operator =(const char *s);
	inline	String &	operator +=(const String &s);
	inline	String &	operator +=(const char *s);
		String &	operator +=(char c);
			
	inline	bool		operator < (const String &s) const;
	inline	bool		operator > (const String &s) const;
	inline	bool		operator <=(const String &s) const;
	inline	bool		operator >=(const String &s) const;
	inline	bool		operator ==(const String &s) const;
	inline	bool		operator !=(const String &s) const;

	inline	bool		operator < (const char *s) const;
	inline	bool		operator > (const char *s) const;
	inline	bool		operator <=(const char *s) const;
	inline	bool		operator >=(const char *s) const;
	inline	bool		operator ==(const char *s) const;
	inline	bool		operator !=(const char *s) const;
protected:
		int		compare(const char *s) const;
		void		realloc(int aNewSize);
};

String operator +(const String &s1, const String &s2);
String operator +(const char *s1, const String &s2);

/**
 *	case-insensitive string.
 */
class IString: public String {
public:
				IString(BuildCtorArg &a): String(a) {};
				IString();

	virtual	IString *	clone() const;
	virtual	int		compareChar(char c1, char c2) const;
	virtual	ObjectID	getObjectID() const;
};

/*
 *	inline functions
 */

//class MsgException;

/**
 *	@returns char at position |aIndex|
 *	@throws exception if aIndex out of bounds
 */
inline char &String::at(int aIndex) const
{
//	if ((uint)aIndex >= (uint)mLength) throw new MsgException("index out of bounds");
	return (char &)mContent[aIndex];
}

/**
 *	Removes the last character of the string if string length is non-zero.
 */
inline bool String::chop()
{
	if (mLength) {
		crop(mLength-1);
		return true;
	} else {
		return false;
	}
}

/**
 *	@returns a string content ptr
 */
inline byte *String::content() const
{
	return mContent;
}
 
/**
 *	@returns a string content ptr (as char*)
 */
inline char *String::contentChar() const
{
	return (char*)mContent;
}
 
/**
 *	@returns first character of string
 *	@throws exception if string is empty
 */
inline char String::firstChar() const
{
	return at(0);
}

/**
 *	@returns true if string is empty.
 */
inline bool String::isEmpty() const
{
	return mLength == 0;
}

/**
 *	@returns last character of string
 *	@throws exception if string is empty
 */
inline char String::lastChar() const
{
	return at(mLength-1);
}

/**
 *	@returns length of string
 */
inline int String::length() const
{
	return mLength;
}

inline char &String::operator [](int aIndex) const
{
	return at(aIndex);
}

inline String &String::operator =(const String &s)
{
	assign(s);
	return *this;
}

inline String &String::operator =(const char *s)
{
	assign(s);
	return *this;
}

inline String &String::operator +=(const String &s)
{
	append(s);
	return *this;
}

inline String &String::operator +=(const char *s)
{
	append(s);
	return *this;
}

inline bool String::operator < (const String &s) const
{
	return compare(s) < 0;
}

inline bool String::operator <= (const String &s) const
{
	return compare(s) <= 0;
}

inline bool String::operator > (const String &s) const
{
	return compare(s) > 0;
}

inline bool String::operator >= (const String &s) const
{
	return compare(s) >= 0;
}

inline bool String::operator == (const String &s) const
{
	return compare(s) == 0;
}

inline bool String::operator != (const String &s) const
{
	return compare(s) != 0;
}

inline bool String::operator < (const char *s) const
{
	return compare(s) < 0;
}

inline bool String::operator <= (const char *s) const
{
	return compare(s) <= 0;
}

inline bool String::operator > (const char *s) const
{
	return compare(s) > 0;
}

inline bool String::operator >= (const char *s) const
{
	return compare(s) >= 0;
}

inline bool String::operator == (const char *s) const
{
	return compare(s) == 0;
}

inline bool String::operator != (const char *s) const
{
	return compare(s) != 0;
}
#endif