Changeset 118
- Timestamp:
- Jan 9, 2006 5:20:59 PM (19 years ago)
- Location:
- pjproject/trunk/pjlib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/string_i.h
r77 r118 130 130 diff = str1->slen - str2->slen; 131 131 if (diff) { 132 return (int)diff;132 return diff > 0 ? 1 : -1; 133 133 } else if (str1->ptr && str1->slen) { 134 134 return pj_native_strncmp(str1->ptr, str2->ptr, str1->slen); … … 141 141 pj_size_t len) 142 142 { 143 return (str1->ptr && str2->ptr) ? 144 pj_native_strncmp(str1->ptr, str2->ptr, len) : 145 (str1->ptr == str2->ptr ? 0 : 1); 143 if (str1->ptr && str2->ptr) 144 return pj_native_strncmp(str1->ptr, str2->ptr, len); 145 else if (str2->ptr) 146 return str2->slen==0 ? 0 : -1; 147 else if (str1->ptr) 148 return str1->slen==0 ? 0 : 1; 149 else 150 return 0; 146 151 } 147 152 … … 149 154 pj_size_t len) 150 155 { 151 return (str1->ptr && str2) ? pj_native_strncmp(str1->ptr, str2, len) : 152 (str1->ptr==str2 ? 0 : 1); 156 if (len == 0) 157 return 0; 158 else if (str1->ptr && str2) 159 return pj_native_strncmp(str1->ptr, str2, len); 160 else if (str1->ptr) 161 return str1->slen==0 ? 0 : 1; 162 else if (str2) 163 return *str2=='\0' ? 0 : -1; 164 else 165 return 0; 153 166 } 154 167 155 168 PJ_IDEF(int) pj_strcmp2( const pj_str_t *str1, const char *str2 ) 156 169 { 157 return pj_strncmp2( str1, str2, str1->slen); 170 if (str1->slen == 0) { 171 return (!str2 || *str2=='\0') ? 0 : -1; 172 } else 173 return pj_strncmp2( str1, str2, str1->slen); 158 174 } 159 175 … … 208 224 209 225 if (len != str2->slen) { 210 return -1;226 return (len < str2->slen) ? -1 : 1; 211 227 } else if (len == 0) { 212 228 return 0; … … 241 257 PJ_IDEF(int) pj_stricmp2( const pj_str_t *str1, const char *str2) 242 258 { 243 return (str1->ptr && str2) ? 244 pj_native_strnicmp(str1->ptr, str2, str1->slen) : 245 (str1->ptr==str2 ? 0 : 1); 259 if (str1->ptr && str2) 260 return pj_native_strnicmp(str1->ptr, str2, str1->slen); 261 else if (str2) 262 return (*str2=='\0') ? 0 : -1; 263 else if (str1->ptr) 264 return (str1->slen==0) ? 0 : 1; 265 else 266 return 0; 246 267 } 247 268 … … 249 270 pj_size_t len) 250 271 { 251 return (str1->ptr && str2->ptr) ? 252 pj_native_strnicmp(str1->ptr, str2->ptr, len) : 253 (str1->ptr == str2->ptr ? 0 : 1); 272 if (str1->ptr && str2->ptr) 273 return pj_native_strnicmp(str1->ptr, str2->ptr, len); 274 else if (str2->ptr) 275 return str2->slen==0 ? 0 : -1; 276 else if (str1->ptr) 277 return str1->slen==0 ? 0 : 1; 278 else 279 return 0; 254 280 } 255 281 … … 257 283 pj_size_t len) 258 284 { 259 return (str1->ptr && str2) ? 260 pj_native_strnicmp(str1->ptr, str2, len) : 261 (str1->ptr == str2 ? 0 : 1); 285 if (len == 0) 286 return 0; 287 else if (str1->ptr && str2) 288 return pj_native_strnicmp(str1->ptr, str2, len); 289 else if (str1->ptr) 290 return str1->slen==0 ? 0 : 1; 291 else if (str2) 292 return *str2=='\0' ? 0 : -1; 293 else 294 return 0; 295 262 296 } 263 297 -
pjproject/trunk/pjlib/src/pjlib-test/main.c
r65 r118 58 58 { 59 59 int rc; 60 int interractive = 0; 60 61 61 62 boost(); … … 65 66 char *arg = argv[--argc]; 66 67 67 if (*arg=='-' && *(arg+1)=='p') { 68 if (*arg=='-' && *(arg+1)=='i') { 69 interractive = 1; 70 71 } else if (*arg=='-' && *(arg+1)=='p') { 68 72 pj_str_t port = pj_str(argv[--argc]); 69 73 … … 89 93 rc = test_main(); 90 94 95 if (interractive) { 96 char s[10]; 97 puts(""); 98 puts("Press <ENTER> to exit"); 99 fgets(s, sizeof(s), stdin); 100 } 101 91 102 return rc; 92 103 } -
pjproject/trunk/pjlib/src/pjlib-test/string.c
r77 r118 48 48 * - pj_strtoul() 49 49 * - pj_create_random_string() 50 * 50 * - ... and mode.. 51 51 * 52 52 * This file is <b>pjlib-test/string.c</b> … … 67 67 static int stricmp_test(void) 68 68 { 69 /* This specificly tests and benchmark pj_stricmp(), pj_stricmp_alnum(). 70 * In addition, it also tests pj_stricmp2(), pj_strnicmp(), and 71 * pj_strnicmp2(). 72 */ 69 73 #define STRTEST(res,S1,S2,code) \ 70 74 do { \ 71 s1.ptr=S1; s1.slen=len; \72 s2.ptr=S2; s2.slen=len; \75 s1.ptr=S1; s1.slen=S1?len:0; \ 76 s2.ptr=S2; s2.slen=S2?len:0; \ 73 77 pj_get_timestamp(&t1); \ 74 78 if (pj_stricmp(&s1,&s2)!=res) return code; \ … … 81 85 pj_sub_timestamp(&t2, &t1); \ 82 86 pj_add_timestamp(&e2, &t2); \ 87 if (pj_stricmp2(&s1,S2)!=res) return code*10; \ 88 if (pj_strnicmp(&s1,&s2,len)!=res) return code*100; \ 89 if (pj_strnicmp2(&s1,S2,len)!=res) return code*1000; \ 83 90 } while (0) 84 91 … … 96 103 len=0; 97 104 STRTEST( 0, "","",-500); 105 STRTEST( 0, NULL,"",-502); 106 STRTEST( 0, "",NULL,-504); 107 STRTEST( 0, NULL,NULL,-506); 108 STRTEST( 0, "hello","world",-508); 98 109 99 110 /* equal, length=1 … … 105 116 STRTEST( 0, "a",buf+1,-512); 106 117 STRTEST( -1, "0", "P", -514); 118 STRTEST(-1, NULL, "a", -516); 119 STRTEST(1, "a", NULL, -518); 107 120 108 121 /* equal, length=2 … … 208 221 } 209 222 223 /* This tests pj_strcmp(), pj_strcmp2(), pj_strncmp(), pj_strncmp2() */ 224 static int strcmp_test(void) 225 { 226 #define STR_TEST(res,S1,S2,code) \ 227 do { \ 228 s1.ptr=S1; s1.slen=S1?len:0; \ 229 s2.ptr=S2; s2.slen=S2?len:0; \ 230 if (pj_strcmp(&s1,&s2)!=res) return code; \ 231 if (pj_strcmp2(&s1,S2)!=res) return code-1; \ 232 if (pj_strncmp(&s1,&s2,len)!=res) return code-2; \ 233 if (pj_strncmp2(&s1,S2,len)!=res) return code-3; \ 234 } while (0) 235 236 pj_str_t s1, s2; 237 int len; 238 239 /* Test with length == 0 */ 240 len=0; 241 STR_TEST(0, "", "", -400); 242 STR_TEST(0, NULL, "", -405); 243 STR_TEST(0, "", NULL, -410); 244 STR_TEST(0, NULL, NULL, -415); 245 STR_TEST(0, "hello", "", -420); 246 STR_TEST(0, "hello", NULL, -425); 247 248 /* Test with length != 0 */ 249 len = 2; 250 STR_TEST(0, "12", "12", -430); 251 STR_TEST(1, "12", "1", -435); 252 STR_TEST(-1, "1", "12", -440); 253 STR_TEST(-1, NULL, "12", -445); 254 STR_TEST(1, "12", NULL, -450); 255 256 return 0; 257 258 #undef STR_TEST 259 } 260 210 261 int string_test(void) 211 262 { … … 308 359 pj_pool_release(pool); 309 360 310 return stricmp_test(); 361 /* Case sensitive comparison test. */ 362 i = strcmp_test(); 363 if (i != 0) 364 return i; 365 366 /* Caseless comparison test. */ 367 i = stricmp_test(); 368 if (i != 0) 369 return i; 370 371 return 0; 311 372 } 312 373
Note: See TracChangeset
for help on using the changeset viewer.