File: stringUtils.h

package info (click to toggle)
between 6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 99; perl: 67
file content (321 lines) | stat: -rw-r--r-- 9,051 bytes parent folder | download | duplicates (10)
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
317
318
319
320
321
/*
 * Modification History
 *
 * 2002-April-6   Jason Rohrer
 * Created.
 *
 * 2002-April-8   Jason Rohrer
 * Fixed multiple inclusion bug
 *
 * 2002-May-7   Jason Rohrer
 * Added functions for case-insensitive substring finding and case
 * conversion.
 *
 * 2002-May-9   Jason Rohrer
 * Fixed a bug when string not found.
 *
 * 2002-May-26   Jason Rohrer
 * Added a function for string comparison ignoring cases.
 *
 * 2003-March-28   Jason Rohrer
 * Added split function.
 *
 * 2003-May-1   Jason Rohrer
 * Added replacement functions.
 *
 * 2003-May-4   Jason Rohrer
 * Added list replacement function.
 *
 * 2003-May-10   Jason Rohrer
 * Moved implementations into a .cpp file.  This will break several
 * projects.
 * Added a tokenization function.
 *
 * 2003-June-14   Jason Rohrer
 * Added a join function.
 *
 * 2003-June-22   Jason Rohrer
 * Added an autoSprintf function.
 *
 * 2003-August-12  Jason Rohrer
 * Added a concatonate function.
 *
 * 2003-September-7  Jason Rohrer
 * Improved split comment.
 *
 * 2006-June-2  Jason Rohrer
 * Added a stringStartsWith function.
 *
 * 2010-May-14    Jason Rohrer
 * String parameters as const to fix warnings.
 */



#include "minorGems/common.h"
#include "minorGems/util/SimpleVector.h"



#ifndef STRING_UTILS_INCLUDED
#define STRING_UTILS_INCLUDED



// ANSI does not support strdup, strcasestr, or strcasecmp
#include <string.h>
#include <ctype.h>
#include <stdio.h>




/**
 * Duplicates a string into a newly allocated string.
 *
 * @param inString the \0-terminated string to duplicate.
 *   Must be destroyed by caller if non-const.
 *
 * @return a \0-terminated duplicate of inString.
 *   Must be destroyed by caller.
 */
inline char *stringDuplicate( const char *inString ) {
    
    char *returnBuffer = new char[ strlen( inString ) + 1 ];

    strcpy( returnBuffer, inString );

    return returnBuffer;    
    }



/**
 * Converts a string to lower case.
 *
 * @param inString the \0-terminated string to convert.
 *   Must be destroyed by caller if non-const.
 *
 * @return a newly allocated \0-terminated string
 *   that is a lowercase version of inString.
 *   Must be destroyed by caller.
 */
char *stringToLowerCase( const char *inString  );



/**
 * Searches for the first occurrence of one string in another.
 *
 * @param inHaystack the \0-terminated string to search in.
 *   Must be destroyed by caller if non-const.
 * @param inNeedle the \0-terminated string to search for.
 *   Must be destroyed by caller if non-const.
 *
 * @return a string pointer into inHaystack where the
 *   first occurrence of inNeedle starts, or NULL if inNeedle is not found.
 */
char *stringLocateIgnoreCase( const char *inHaystack,
                              const char *inNeedle );



/**
 * Compares two strings, ignoring case.
 *
 * @param inStringA the first \0-terminated string.
 *   Must be destroyed by caller if non-const.
 * @param inStringB the second \0-terminated string.
 *   Must be destroyed by caller if non-const.
 *
 * @return an integer less than, equal to, or greater than zero if
 *   inStringA is found,  respectively,  to  be  less  than, to match, or be
 *   greater than inStringB.
 */
int stringCompareIgnoreCase( const char *inStringA,
                             const char *inStringB );



/**
 * Checks if a string starts with a given prefix string.
 *
 * @param inString a \0-terminated string.
 *   Must be destroyed by caller if non-const.
 * @param inPrefix the prefix to look for as a \0-terminated string.
 *   Must be destroyed by caller if non-const.
 *
 * @return true if inString begins with inPrefix, or false otherwise.
 */
char stringStartsWith( const char *inString, const char *inPrefix );



/**
 * Splits a string into parts around a separator string.
 *
 * Note that splitting strings that start and/or end with the separator
 * results in "empty" strings being returned at the start and/or end
 * of the parts array.
 *
 * For example, splitting "a#short#test" around the "#" separator will
 * result in the array { "a", "short", "test" }, but splitting
 * "#another#short#test#" will result in the array
 * { "", "another", "short", "test", "" }.
 *
 * This differs somewhat from the perl version of split, but it gives the
 * caller more information about the string being split.
 *
 * @param inString the string to split.
 *   Must be destroyed by caller if non-const.
 * @param inSeparator the separator string.
 *   Must be destroyed by caller if non-const.
 * @param outNumParts pointer to where the the number of parts (the length of 
 *   the returned array) should be returned.
 *
 * @return an array of split parts.
 *   Must be destroyed by caller.
 */
char **split( const char *inString, const char *inSeparator, 
              int *outNumParts );



/**
 * Joins a collection of strings using a separator string.
 *
 * @param inStrings the strings to join.
 *   Array and strings must be destroyed by caller.
 * @param inNumParts the number of strings to join.
 * @param inSeparator the separator string.
 *   Must be destroyed by caller if non-const.
 *
 * @return the joined string.
 *   Must be destroyed by caller.
 */
char *join( char **inStrings, int inNumParts, const char *inGlue );



/**
 * Concatonates two strings.
 *
 * @param inStringA the first string in the concatonation.
 *   Must be destroyed by caller if non-const.
 * @param inStringB the second string in the concatonation.
 *   Must be destroyed by caller if non-const.
 *
 * @return the concatonation.
 *   Must be destroyed by caller.
 */
char *concatonate( const char *inStringA, const char *inStringB );



/**
 * Replaces the first occurrence of a target string with
 * a substitute string.
 *
 * All parameters and return value must be destroyed by caller.
 *
 * @param inHaystack the string to search for inTarget in. 
 * @param inTarget the string to search for.
 * @param inSubstitute the string to replace the first occurrence
 *   of the target with.
 * @param outFound a pre-allocated character which will be filled
 *   with true if the target is found, and filled with false
 *   otherwise.
 *
 * @return a newly allocated string with the substitution performed.
 */
char *replaceOnce( const char *inHaystack, const char *inTarget,
                   const char *inSubstitute,
                   char *outFound );



/**
 * Replaces the all occurrences of a target string with
 * a substitute string.
 *
 * Note that this function is not self-insertion-safe:
 * If inSubstitute contains inTarget, this function will
 * enter an infinite loop.
 *         
 * All parameters and return value must be destroyed by caller.
 *
 * @param inHaystack the string to search for inTarget in. 
 * @param inTarget the string to search for.
 * @param inSubstitute the string to replace the all occurrences
 *   of the target with.
 * @param outFound a pre-allocated character which will be filled
 *   with true if the target is found at least once,
 *   and filled with false otherwise.
 *
 * @return a newly allocated string with the substitutions performed.
 */
char *replaceAll( const char *inHaystack, const char *inTarget,
                  const char *inSubstitute,
                  char *outFound );



/**
 * Replaces the all occurrences of each target string on a list with
 * a corresponding substitute string.
 *
 * Note that this function is not self-insertion-safe:
 * If inSubstituteVector contains elements from inTargetVector,
 * this function will enter an infinite loop.
 *         
 * All parameters and return value must be destroyed by caller.
 *
 * @param inHaystack the string to search for targets in. 
 * @param inTargetVector the list of strings to search for.
 *   Vector and contained strings must be destroyed by caller.
 * @param inSubstituteVector the corresponding list of strings to
 *   replace the all occurrences of the targets with.
 *   Vector and contained strings must be destroyed by caller.
 *
 * @return a newly allocated string with the substitutions performed.
 */
char *replaceTargetListWithSubstituteList(
    const char *inHaystack,
    SimpleVector<char *> *inTargetVector,
    SimpleVector<char *> *inSubstituteVector );




/**
 * Split a string into tokens using whitespace as separators.
 *
 * @param inString the string to tokenize.
 *   Must be destroyed by caller.
 *
 * @return a vector of extracted strings.
 *   Vector and strings must be destroyed by caller.
 */
SimpleVector<char *> *tokenizeString( const char *inString );



/**
 * Prints formatted data elements into a newly allocated string buffer.
 *
 * Similar to sprintf, except that buffer sizing is automatic (and therefore
 * safer than manual sizing).
 *
 * @param inFormatString the format string to print from.
 * @param variable argument list data values to fill in the format string
 *   with (uses same conventions as printf).
 *
 * @return a newly allocated buffer containing the \0-terminated printed
 *   string.
 *   Must be destroyed by caller.
 */
char *autoSprintf( const char* inFormatString, ... );



#endif