Changeset 7831
- Timestamp:
- Jul 6, 2006, 12:26:13 PM (20 years ago)
- Location:
- trunk/psLib
- Files:
-
- 3 edited
-
src/sys/psString.c (modified) (4 diffs)
-
test/sys/tst_psString.c (modified) (4 diffs)
-
test/sys/verified/tst_psString.stderr (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psString.c
r7766 r7831 13 13 * @author David Robbins, MHPCC 14 14 * 15 * @version $Revision: 1.3 3$ $Name: not supported by cvs2svn $16 * @date $Date: 2006-0 6-30 02:20:06$15 * @version $Revision: 1.34 $ $Name: not supported by cvs2svn $ 16 * @date $Date: 2006-07-06 22:26:13 $ 17 17 * 18 18 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 218 218 // given the input string, search for all copies of the key, and replace with the replacement value 219 219 // the input string may be freed if not needed 220 char *psStringSubstitute(char *input, const char *replace, const char *key) 221 { 222 if (key == NULL || strlen(key) == 0) { 223 return input; 224 } 225 220 char *psStringSubstitute(char *input, 221 const char *replace, 222 const char *key) 223 { 224 if (input == NULL ) { 225 psError(PS_ERR_BAD_PARAMETER_NULL, true, 226 "Invalid string in psStringSubstitute. Input cannot be NULL or empty.\n"); 227 return NULL; 228 } else if (strlen(input) == 0) { 229 psError(PS_ERR_BAD_PARAMETER_NULL, true, 230 "Invalid string in psStringSubstitute. Input cannot be NULL or empty.\n"); 231 return NULL; 232 } 233 if (replace == NULL) { 234 return NULL; 235 } 236 if (key == NULL ) { 237 return NULL; 238 } else if (strlen(key) == 0) { 239 return NULL; 240 } 241 242 psString in = psStringCopy(input); 226 243 while (true) { 227 char *p = strstr (input, key); 244 // char *p = strstr (input, key); 245 char *p = strstr (in, key); 228 246 if (!p) { 229 return input; 247 // return input; 248 // strcpy(input, in); 249 // psFree(in); 250 return in; 230 251 } 231 252 … … 234 255 235 256 // this is safe since we will subtract strlen(key) elements from input 236 char *output = psAlloc(strlen(input) + strlen(replace) + 1); 237 int Nc = p - input; 257 // char *output = psAlloc(strlen(input) + strlen(replace) + 1); 258 // int Nc = p - input; 259 int Nc = p - in; 238 260 239 261 // copy the first segement into 'output' 240 strncpy(output, input, Nc); 262 psString output = psStringNCopy(in, strlen(in)+strlen(replace)+1); 263 264 // strncpy(output, input, Nc); 241 265 242 266 // copy the key replacement to the start of the key … … 247 271 strcpy(&output[Nc], p + strlen(key)); 248 272 249 psFree(input); 250 input = output; 251 } 252 return input; 273 // psFree(input); 274 // input = output; 275 // psFree(in); 276 // strcpy(in, "\0"); 277 //in = psStringCopy("\0"); 278 psFree(in); 279 // input = output; 280 // strcpy(in, output); 281 in = psStringCopy(output); 282 psFree(output); 283 } 284 // strcpy(input, in); 285 // psFree(in); 286 return in; 253 287 } 254 288 -
trunk/psLib/test/sys/tst_psString.c
r7418 r7831 20 20 * @author Eric Van Alst, MHPCC 21 21 * 22 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $23 * @date $Date: 2006-0 6-08 01:06:36$22 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 23 * @date $Date: 2006-07-06 22:26:13 $ 24 24 * 25 25 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 53 53 static psS32 testStrSplit00(void); 54 54 static psS32 testNULLStrings(void); 55 static psS32 testStrSub(void); 55 56 56 57 testDescription tests[] = { … … 74 75 {testStrSplit00,15, "Test String Splitting", 0, false}, 75 76 {testNULLStrings,666, "Test NULL String Error Handling", 0, false}, 77 {testStrSub,16, "Test String Substitute", 0, false}, 76 78 {NULL} 77 79 }; … … 710 712 } 711 713 714 static psS32 testStrSub(void) 715 { 716 psString input = NULL; 717 char str[35]; 718 strncpy(str, "This is, a, test case, to check.", 35); 719 720 //Return str for NULL key 721 input = psStringSubstitute(str, ",", NULL); 722 if (input != NULL) { 723 psError(PS_ERR_BAD_PARAMETER_NULL, true, 724 "psStringSubstitute failed to return unchanged str for NULL key.\n"); 725 psFree(input); 726 return 1; 727 } 728 //Return NULL for empty key 729 input = psStringSubstitute(str, ",", ""); 730 if (input != NULL) { 731 psError(PS_ERR_BAD_PARAMETER_NULL, true, 732 "psStringSubstitute failed to return unchanged str for empty key.\n"); 733 psFree(input); 734 return 2; 735 } 736 //Return NULL for NULL replace 737 input = psStringSubstitute(str, NULL, ","); 738 if (input != NULL) { 739 psError(PS_ERR_BAD_PARAMETER_NULL, true, 740 "psStringSubstitute failed to return unchanged str for NULL replace.\n"); 741 psFree(input); 742 return 3; 743 } 744 //Return NULL for NULL input 745 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message"); 746 input = psStringSubstitute(NULL, "", ","); 747 if (input != NULL) { 748 psError(PS_ERR_BAD_PARAMETER_NULL, true, 749 "psStringSubstitute failed to return NULL for NULL input.\n"); 750 psFree(input); 751 return 4; 752 } 753 //Return empty string for empty input string 754 psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message"); 755 input = psStringSubstitute("", "", ","); 756 if (input != NULL) { 757 psError(PS_ERR_BAD_PARAMETER_NULL, true, 758 "psStringSubstitute failed to return empty string for empty input string.\n"); 759 psFree(input); 760 return 5; 761 } 762 //Check valid test case, changing commas to exclamation points 763 input = psStringSubstitute(str, "!", ","); 764 if (strcmp(input, "This is! a! test case! to check.") != 0 ) { 765 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 766 "psStringSubstitute failed to return the correct output string.\n"); 767 psFree(input); 768 return 7; 769 } 770 psFree(input); 771 input = NULL; 772 //Check valid test case, remove exclamation points 773 input = psStringSubstitute(str, "", ","); 774 if (strcmp(input, "This is a test case to check.") != 0 ) { 775 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 776 "psStringSubstitute failed to return the correct output string.\n"); 777 psFree(input); 778 return 8; 779 } 780 psFree(input); 781 input = NULL; 782 //Check case where replacement is long. Should still work now. 783 input = psStringSubstitute(str, "; This string is too long to fit in str(35 chars)", "."); 784 if (strcmp(input,"This is, a, test case, to check; This string is too long to fit in str(35 chars)") != 0 ) { 785 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 786 "psStringSubstitute failed to return the correct output string.\n"); 787 psFree(input); 788 return 9; 789 } 790 psFree(input); 791 792 return 0; 793 } -
trunk/psLib/test/sys/verified/tst_psString.stderr
r7418 r7831 175 175 ---> TESTPOINT PASSED (psString{Test NULL String Error Handling} | tst_psString.c) 176 176 177 /***************************** TESTPOINT ******************************************\ 178 * TestFile: tst_psString.c * 179 * TestPoint: psString{Test String Substitute} * 180 * TestType: Positive * 181 \**********************************************************************************/ 182 183 <DATE><TIME>|<HOST>|I|testStrSub 184 Following should generate error message 185 <DATE><TIME>|<HOST>|E|psStringSubstitute (FILE:LINENO) 186 Invalid string in psStringSubstitute. Input cannot be NULL or empty. 187 <DATE><TIME>|<HOST>|I|testStrSub 188 Following should generate error message 189 <DATE><TIME>|<HOST>|E|psStringSubstitute (FILE:LINENO) 190 Invalid string in psStringSubstitute. Input cannot be NULL or empty. 191 192 ---> TESTPOINT PASSED (psString{Test String Substitute} | tst_psString.c) 193
Note:
See TracChangeset
for help on using the changeset viewer.
