Changeset 10446
- Timestamp:
- Dec 4, 2006, 12:15:04 PM (19 years ago)
- Location:
- trunk/psLib
- Files:
-
- 3 edited
-
src/sys/psString.c (modified) (5 diffs)
-
src/sys/psString.h (modified) (2 diffs)
-
test/sys/tap_psStringSubstitute.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psString.c
r10286 r10446 13 13 * @author David Robbins, MHPCC 14 14 * 15 * @version $Revision: 1.4 6$ $Name: not supported by cvs2svn $16 * @date $Date: 2006-1 1-29 21:33:09$15 * @version $Revision: 1.47 $ $Name: not supported by cvs2svn $ 16 * @date $Date: 2006-12-04 22:15:04 $ 17 17 * 18 18 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 275 275 // given the input string, search for all copies of the key, and replace with the replacement value 276 276 // the input string may be freed if not needed 277 psString psStringSubstitute(psString input, 278 const char *replace, 279 const char *key) 280 { 277 ssize_t psStringSubstitute(psString *input, 278 const char *replace, 279 const char *key) 280 { 281 PS_ASSERT_PTR_NON_NULL(input, 0); 282 281 283 // Empty input gives empty output 282 if (! input || strlen(input) == 0) {283 return input;284 if (!*input || strlen(*input) == 0) { 285 return 0; 284 286 } 285 287 // No key gives the same as the input 286 288 if (!key || strlen(key) == 0) { 287 return input;288 } 289 if (!psMemCheckString( input)) {289 return strlen(*input); 290 } 291 if (!psMemCheckString(*input)) { 290 292 psError(PS_ERR_BAD_PARAMETER_TYPE, true, "This function requires a psString as input.\n"); 291 return NULL;293 return 0; 292 294 } 293 295 … … 302 304 } 303 305 304 char *search = input;// Search this string306 char *search = *input; // Search this string 305 307 while (true) { 306 308 char *found = strstr(search, key); // Found occurence of the key 307 309 if (!found) { 308 return input;310 return strlen(*input); 309 311 } 310 312 … … 313 315 314 316 // this is safe since we will subtract strlen(key) elements from input 315 psString output = psStringAlloc(strlen( input) + replaceLength + 1); // Output string316 int numChar = found - input; // Number of characters to copy317 psString output = psStringAlloc(strlen(*input) + replaceLength + 1); // Output string 318 int numChar = found - *input; // Number of characters to copy 317 319 318 320 // copy the first segement into 'output' 319 strncpy(output, input, numChar);321 strncpy(output, *input, numChar); 320 322 321 323 // copy the key replacement to the start of the key … … 328 330 strcpy(output + numChar, found + strlen(key)); 329 331 330 psFree( input);331 input = output;332 psFree(*input); 333 *input = output; 332 334 search = output + numChar; 333 335 } 334 336 335 337 psAbort(__func__, "Should never get here.\n"); 336 return NULL;338 return 0; 337 339 } 338 340 -
trunk/psLib/src/sys/psString.h
r10286 r10446 14 14 * @author David Robbins, MHPCC 15 15 * 16 * @version $Revision: 1.3 2$ $Name: not supported by cvs2svn $17 * @date $Date: 2006-1 1-29 21:33:09$16 * @version $Revision: 1.33 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2006-12-04 22:15:04 $ 18 18 * 19 19 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 191 191 * the replacement value wherever found. The input string may be freed if not needed. 192 192 * 193 * @return char*: the modified input string.194 */ 195 psStringpsStringSubstitute (196 psString input, ///<input string to be modified193 * @return ssize_t: the length of the new string (excluding '\0') 194 */ 195 ssize_t psStringSubstitute ( 196 psString *input, ///< ptr to input string to be modified 197 197 const char *replace, ///< replacement value 198 198 const char *key ///< string to be replaced in input -
trunk/psLib/test/sys/tap_psStringSubstitute.c
r8841 r10446 18 18 { 19 19 psString input = psStringCopy(ORIGINAL); 20 input = psStringSubstitute(input, ",", NULL);20 psStringSubstitute(&input, ",", NULL); 21 21 ok(input && strcmp(input, ORIGINAL) == 0, "output = %s", input); 22 22 psFree(input); … … 28 28 { 29 29 psString input = psStringCopy(ORIGINAL); 30 input = psStringSubstitute(input, "XXX", "");30 psStringSubstitute(&input, "XXX", ""); 31 31 ok(input && strcmp(input, ORIGINAL) == 0, "output = %s", input); 32 32 psFree(input); … … 38 38 { 39 39 psString input = psStringCopy(ORIGINAL); 40 psString output = psStringSubstitute(input, NULL, ",");41 ok( output && strcmp(output, CORRECTED) == 0, "output = %s", output);42 psFree( output);40 psStringSubstitute(&input, NULL, ","); 41 ok(input && strcmp(input, CORRECTED) == 0, "output = %s", input); 42 psFree(input); 43 43 mem(); 44 44 } … … 47 47 { 48 48 psString input = psStringCopy(ORIGINAL); 49 psString output = psStringSubstitute(input, "", ",");50 ok( output && strcmp(output, CORRECTED) == 0, "output = %s", output);51 psFree( output);49 psStringSubstitute(&input, "", ","); 50 ok(input && strcmp(input, CORRECTED) == 0, "output = %s", input); 51 psFree(input); 52 52 mem(); 53 53 } … … 55 55 // Return NULL for NULL input 56 56 { 57 psString output= psStringSubstitute(NULL, "XXX", ",");58 ok( !output, "output = %s", output);57 int status = psStringSubstitute(NULL, "XXX", ","); 58 ok(status == 0, "status = %d", status); 59 59 mem(); 60 60 } … … 63 63 { 64 64 psString input = psStringCopy(""); 65 psString output = psStringSubstitute(input, "XXX", ",");66 ok( output && strcmp(output, input) == 0, "output = %s", output);67 psFree( output);65 psStringSubstitute(&input, "XXX", ","); 66 ok(input && strcmp(input, "") == 0, "output = %s", input); 67 psFree(input); 68 68 mem(); 69 69 } … … 72 72 { 73 73 psString input = psStringCopy(ORIGINAL); 74 psString output = psStringSubstitute(input, "!", ",");75 ok( output && strcmp(output, "This is! a! test case! to check.") == 0, "output = %s", output);76 psFree( output);74 psStringSubstitute(&input, "!", ","); 75 ok(input && strcmp(input, "This is! a! test case! to check.") == 0, "output = %s", input); 76 psFree(input); 77 77 mem(); 78 78 } … … 81 81 { 82 82 psString input = psStringCopy(ORIGINAL); 83 psString output = psStringSubstitute(input, "; This string is too long to fit in input(35 chars)", ".");84 ok( output && strcmp(output, "This is, a, test case, to check; "85 "This string is too long to fit in input(35 chars)") == 0,86 "output = %s", output);87 psFree( output);83 psStringSubstitute(&input, "; This string is too long to fit in input(35 chars)", "."); 84 ok(input && strcmp(input, "This is, a, test case, to check; " 85 "This string is too long to fit in input(35 chars)") == 0, 86 "output = %s", input); 87 psFree(input); 88 88 mem(); 89 89 }
Note:
See TracChangeset
for help on using the changeset viewer.
