File: charstring.h

package info (click to toggle)
librudiments0 0.27-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,528 kB
  • ctags: 2,284
  • sloc: cpp: 14,657; sh: 7,547; ansic: 2,664; makefile: 945; xml: 15
file content (257 lines) | stat: -rw-r--r-- 9,660 bytes parent folder | download
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
// Copyright (c) 2003 David Muse
// See the COPYING file for more information.

#ifndef RUDIMENTS_CHARSTRING_H
#define RUDIMENTS_CHARSTRING_H

#include <rudiments/private/charstringincludes.h>

// The charstring class provides methods for manipulating strings.
//
// Unlike the functions in string.h, these methods are NULL safe.  If any of
// the pointer arguments are NULL, your application will not crash.

class charstring {
	public:

		static	size_t	length(const char *string);
			// Returns the length of "string".

		static	void	zero(char *str, size_t size);
			// Sets "size" bytes of "str" to NULL.

		static	char	*append(char *dest, const char *source);
			// Appends "source" to "dest".  Assumes that there
			// is enough room remaining in "dest" to accommodate
			// the new string.
			// Returns a pointer to "dest".
		static	char	*append(char *dest, const char *source,
							size_t size);
			// Appends "size" bytes of "source" to "dest".  Assumes
			// that there is enough room remaining in "dest" to
			// accommodate the new string.
			// Returns a pointer to "dest".


		static	char	*copy(char *dest, const char *source);
			// Replaces "dest" with "source".  Assumes that there
			// is enough room in "dest" to accommodate "source".
			// Returns a pointer to "dest".
		static	char	*copy(char *dest,
					const char *source, size_t size);
			// Replaces the first "size" bytes of "dest" with
			// "source".  Assumes that "dest" is at least "size"
			// bytes long.
			// Returns a pointer to "dest".
		static	char	*copy(char *dest, size_t location,
					const char *source);
			// Replaces "dest" with "source", starting "location"
			// bytes into "dest".  Assumes that there is enough
			// room in "dest" (after "location" bytes) to
			// accommodate "source".
			// Returns a pointer to "dest".
		static	char	*copy(char *dest, size_t location,
					const char *source, size_t size);
			// Replaces "size" bytes of "dest" with "source",
			// starting "location" bytes into "dest".  Assumes that
			// there are "size" bytes in "dest" (after "location"
			// bytes).
			// Returns a pointer to "dest".


		static	int	compare(const char *str1, const char *str2);
			// Returns -1,0 or 1 if "str1" is greater than, 
			// equal to or less than "str2".
		static	int	compare(const char *str1, const char *str2,
								size_t size);
			// Returns -1,0 or 1 if "size" bytes of "str1" are
			// greater than, equal to or less than "size" bytes of
			// "str2".
		static	int	compareIgnoringCase(const char *str1,
							const char *str2);
			// Returns -1,0 or 1 if "str1" is greater than, 
			// equal to or less than "str2", ignoring case.
		static	int	compareIgnoringCase(const char *str1,
							const char *str2,
							size_t size);
			// Returns -1,0 or 1 if "size" bytes of "str1" are
			// greater than, equal to or less than "size" bytes of
			// "str2", ignoring case.


		static	bool	contains(const char *haystack,
						const char *needle);
			// Returns true if "haystack" contains "needle" or
			// false otherwise.
		static	bool	contains(const char *haystack,
						char needle);
			// Returns true if "haystack" contains "needle" or
			// false otherwise.
		static	char	*findFirst(const char *haystack,
							const char *needle);
			// Returns a pointer to the first occurrance of "needle"
			// in "haystack" or NULL if not found.
		static	char	*findFirst(const char *haystack,
							char needle);
			// Returns a pointer to the first occurrance of "needle"
			// in "haystack" or NULL if not found.
		static	char	*findLast(const char *haystack,
							const char *needle);
			// Returns a pointer to the last occurrance of "needle"
			// in "haystack" or NULL if not found.
		static	char	*findLast(const char *haystack,
							char needle);
			// Returns a pointer to the last occurrance of "needle"
			// in "haystack" or NULL if not found.


		static	char	*duplicate(const char *str);
			// Creates a duplicate of "str" and returns a pointer
			// to it.  Note that this method allocates a buffer for
			// the duplicate string internally and returns it.  The
			// calling program must deallocate this buffer.
		static	char	*duplicate(const char *str, size_t length);
			// Creates a duplicate of the first "length" bytes of
			// "str" and returns a pointer to it.  Note that this
			// method allocates a buffer for the duplicate string
			// internally and returns it.  The calling program must
			// deallocate this buffer.


		static	void	upper(char *str); 
			// Converts "str" to uppercase.
		static	void	lower(char *str); 
			// Converts "str" to lowercase.

		static	void	rightTrim(char *str);
			// Rrims all spaces off of the right hand side of "str".
		static	void	rightTrim(char *str, char character);
			// Rrims all "character"'s off of the right hand side
			// of "str".

		static	void	leftTrim(char *str);
			// Trims all spaces off of the left hand side of "str".
		static	void	leftTrim(char *str, char character);
			// Trims all "character"'s off of the left hand side
			// of "str".

		static	void	bothTrim(char *str);
			// Trims all spaces off of both sides of "str".
		static	void	bothTrim(char *str, char character);
			// Trims all characters off of both sides of "str".

		static	void	strip(char *str, char character);
			// Strips all instances of "character" from "str".

		static	void	strip(char *str1, char *str2);
			// Strips all instances of "str2" from "str1".

		static	int	integerLength(long number);
			// Returns the number of characters needed to represent
			// "number" as a string.

		static	bool	isInteger(const char *val);
		static	bool	isInteger(const char *val, int size);
			// Returns true if the string "val" is an integer and
			// false if it is not an integer.

		static	bool	isNumber(const char *val);
		static	bool	isNumber(const char *val, int size);
			// Returns true the string "val" is a number and false
			// if it is not a number
		static	char	*parseNumber(long number);
			// Returns a string representing "number".  The string
			// is allocated inside the function and must be deleted
			// by the calling program.
		static	char	*parseNumber(double number);
		static	char	*parseNumber(double number,
						unsigned short scale);
		static	char	*parseNumber(double number,
						unsigned short precision,
						unsigned short scale);
			// Returns a string representing "number".  The string
			// is allocated inside the function and must be deleted
			// by the calling program.

		static	long	toLong(const char *string);
			// Converts "string" to a long integer.
		static	long	toLong(const char *string, char **endptr);
			// Converts "string" to a long integer.  If non-NULL,
			// endptr will be set to the first character in the
			// string after the number.
		static	long	toLong(const char *string, int base);
			// Converts "string" to a long integer of base "base".
		static	long	toLong(const char *string,
					char **endptr, int base);
			// Converts "string" to a long integer of base "base".
			// If non-NULL, endptr will be set to the first
			// character in the string after the number.

		static	long long	toLongLong(const char *string);
			// Converts "string" to a long integer.
		static	long long	toLongLong(const char *string,
							char **endptr);
			// Converts "string" to a long long integer.  If
			// non-NULL, endptr will be set to the first character
			// in the string after the number.
		static	long long	toLongLong(const char *string,
								int base);
			// Converts "string" to a long long integer of
			// base "base".
		static	long long	toLongLong(const char *string,
						char **endptr, int base);
			// Converts "string" to a long long integer of
			// base "base".  If non-NULL, endptr will be set to the
			// first character in the string after the number.

		static	float	toFloat(const char *string);
			// Converts "string" to a floating point number.
		static	float	toFloat(const char *string, char **endptr);
			// Converts "string" to a floating point number.  If
			// non-NULL, endptr will be set to the first character
			// in the string after the number.

		static	double	toDouble(const char *string);
			// Converts "string" to a double precision
			// floating point number.
		static	double	toDouble(const char *string, char **endptr);
			// Converts "string" to a double precision floating
			// point number.  If non-NULL, endptr will be set to
			// the first character in the string after the number.

		static	long double	toLongDouble(const char *string);
			// Converts "string" to a long double precision
			// floating point number.
		static	long double	toLongDouble(const char *string,
								char **endptr);
			// Converts "string" to a long double precision floating
			// point number.  If non-NULL, endptr will be set to
			// the first character in the string after the number.

		static	char	*httpEscape(const char *input);
			// http escapes "input" and returns it in a buffer
			// allocated inside the function.  This buffer must be
			// deleted by the calling program.

		static	void	leftJustify(char *str, int length);
			// Moves leading spaces to the end of "str" for
			// "length" characters.
			// 
			// Example:
			//	"   hello   " -> "hello      "
		static	void	rightJustify(char *str, int length);
			// Moves trailing spaces to the beginning of "str" for
			// "length" characters.
			// 
			// Example:
			//	"   hello   " -> "      hello"
		static	void	center(char *str, int length);
			// Centers the text of "str" for "length" characters.
			// 
			// Example:
			//	"hello      " -> "   hello   "

	#include <rudiments/private/charstring.h>
};

#endif