Changeset 1397
- Timestamp:
- Jun 28, 2007 12:50:10 AM (17 years ago)
- Location:
- pjproject/trunk/pjlib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/string.h
r1210 r1397 342 342 343 343 /** 344 * Perform lowercase comparison to the strings.344 * Perform case-insensitive comparison to the strings. 345 345 * 346 346 * @param str1 The string to compare. … … 399 399 400 400 /** 401 * Perform lowercase comparison to the strings.401 * Perform case-insensitive comparison to the strings. 402 402 * 403 403 * @param str1 The string to compare. … … 412 412 413 413 /** 414 * Perform lowercase comparison to the strings.414 * Perform case-insensitive comparison to the strings. 415 415 * 416 416 * @param str1 The string to compare. … … 427 427 428 428 /** 429 * Perform lowercase comparison to the strings.429 * Perform case-insensitive comparison to the strings. 430 430 * 431 431 * @param str1 The string to compare. -
pjproject/trunk/pjlib/include/pj/string_i.h
r974 r1397 136 136 PJ_IDEF(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2) 137 137 { 138 pj_ssize_t diff; 139 140 diff = str1->slen - str2->slen; 141 if (diff) { 142 return diff > 0 ? 1 : -1; 143 } else if (str1->ptr && str1->slen) { 144 return memcmp(str1->ptr, str2->ptr, str1->slen); 145 } else { 146 return 0; 138 if (str1->slen == 0) { 139 return str2->slen==0 ? 0 : -1; 140 } else if (str2->slen == 0) { 141 return 1; 142 } else { 143 int min = (str1->slen < str2->slen)? str1->slen : str2->slen; 144 int res = pj_memcmp(str1->ptr, str2->ptr, min); 145 if (res == 0) { 146 return (str1->slen < str2->slen) ? -1 : 147 (str1->slen == str2->slen ? 0 : 1); 148 } else { 149 return res; 150 } 147 151 } 148 152 } … … 151 155 pj_size_t len) 152 156 { 153 if (str1->ptr && str2->ptr) 154 return memcmp(str1->ptr, str2->ptr, len); 155 else if (str2->ptr) 156 return str2->slen==0 ? 0 : -1; 157 else if (str1->ptr) 158 return str1->slen==0 ? 0 : 1; 159 else 160 return 0; 157 pj_str_t copy1, copy2; 158 159 if (len < (unsigned)str1->slen) { 160 copy1.ptr = str1->ptr; 161 copy1.slen = len; 162 str1 = ©1; 163 } 164 165 if (len < (unsigned)str2->slen) { 166 copy2.ptr = str2->ptr; 167 copy2.slen = len; 168 str2 = ©2; 169 } 170 171 return pj_strcmp(str1, str2); 161 172 } 162 173 … … 164 175 pj_size_t len) 165 176 { 166 if (len == 0)167 return 0; 168 else if (str1->ptr && str2)169 return memcmp(str1->ptr, str2, len);170 else if (str1->ptr) 171 return str1->slen==0 ? 0 : 1; 172 else if (str2) 173 return *str2=='\0' ? 0 : -1; 174 else 175 return 0;177 pj_str_t copy2; 178 179 if (str2) { 180 copy2.ptr = (char*)str2; 181 copy2.slen = pj_ansi_strlen(str2); 182 } else { 183 copy2.slen = 0; 184 } 185 186 return pj_strncmp(str1, ©2, len); 176 187 } 177 188 178 189 PJ_IDEF(int) pj_strcmp2( const pj_str_t *str1, const char *str2 ) 179 190 { 191 pj_str_t copy2; 192 193 if (str2) { 194 copy2.ptr = (char*)str2; 195 copy2.slen = pj_ansi_strlen(str2); 196 } else { 197 copy2.slen = 0; 198 } 199 200 return pj_strcmp(str1, ©2); 201 } 202 203 PJ_IDEF(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2) 204 { 180 205 if (str1->slen == 0) { 181 return (!str2 || *str2=='\0') ? 0 : -1; 182 } else 183 return pj_strncmp2( str1, str2, str1->slen); 184 } 185 186 PJ_IDEF(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2) 187 { 188 register int len = str1->slen; 189 if (len != str2->slen) { 190 return (int)(len - str2->slen); 191 } else if (len == 0) { 192 return 0; 193 } else { 194 return pj_ansi_strnicmp(str1->ptr, str2->ptr, len); 206 return str2->slen==0 ? 0 : -1; 207 } else if (str2->slen == 0) { 208 return 1; 209 } else { 210 int min = (str1->slen < str2->slen)? str1->slen : str2->slen; 211 int res = pj_ansi_strnicmp(str1->ptr, str2->ptr, min); 212 if (res == 0) { 213 return (str1->slen < str2->slen) ? -1 : 214 (str1->slen == str2->slen ? 0 : 1); 215 } else { 216 return res; 217 } 195 218 } 196 219 } … … 269 292 PJ_IDEF(int) pj_stricmp2( const pj_str_t *str1, const char *str2) 270 293 { 271 if (str1->ptr && str2) 272 return pj_ansi_strnicmp(str1->ptr, str2, str1->slen); 273 else if (str2) 274 return (*str2=='\0') ? 0 : -1; 275 else if (str1->ptr) 276 return (str1->slen==0) ? 0 : 1; 277 else 278 return 0; 294 pj_str_t copy2; 295 296 if (str2) { 297 copy2.ptr = (char*)str2; 298 copy2.slen = pj_ansi_strlen(str2); 299 } else { 300 copy2.slen = 0; 301 } 302 303 return pj_stricmp(str1, ©2); 279 304 } 280 305 … … 282 307 pj_size_t len) 283 308 { 284 if (str1->ptr && str2->ptr) 285 return pj_ansi_strnicmp(str1->ptr, str2->ptr, len); 286 else if (str2->ptr) 287 return str2->slen==0 ? 0 : -1; 288 else if (str1->ptr) 289 return str1->slen==0 ? 0 : 1; 290 else 291 return 0; 309 pj_str_t copy1, copy2; 310 311 if (len < (unsigned)str1->slen) { 312 copy1.ptr = str1->ptr; 313 copy1.slen = len; 314 str1 = ©1; 315 } 316 317 if (len < (unsigned)str2->slen) { 318 copy2.ptr = str2->ptr; 319 copy2.slen = len; 320 str2 = ©2; 321 } 322 323 return pj_stricmp(str1, str2); 292 324 } 293 325 … … 295 327 pj_size_t len) 296 328 { 297 if (len == 0) 298 return 0; 299 else if (str1->ptr && str2) 300 return pj_ansi_strnicmp(str1->ptr, str2, len); 301 else if (str1->ptr) 302 return str1->slen==0 ? 0 : 1; 303 else if (str2) 304 return *str2=='\0' ? 0 : -1; 305 else 306 return 0; 307 329 pj_str_t copy2; 330 331 if (str2) { 332 copy2.ptr = (char*)str2; 333 copy2.slen = pj_ansi_strlen(str2); 334 } else { 335 copy2.slen = 0; 336 } 337 338 return pj_strnicmp(str1, ©2, len); 308 339 } 309 340 … … 318 349 PJ_IDEF(void) pj_strcat2(pj_str_t *dst, const char *str) 319 350 { 320 unsigned len = pj_ansi_strlen(str);351 unsigned len = str? pj_ansi_strlen(str) : 0; 321 352 if (len) { 322 353 pj_memcpy(dst->ptr + dst->slen, str, len); -
pjproject/trunk/pjlib/src/pjlib-test/string.c
r1266 r1397 72 72 PJ_INLINE(int) cmp(const char *expr, int i, int j) 73 73 { 74 i = !((i>0 && j>0) || (i<0 && j<0) || (i==0 && j==0));75 if ( i) {74 int r = !((i>0 && j>0) || (i<0 && j<0) || (i==0 && j==0)); 75 if (r) { 76 76 PJ_LOG(3,(THIS_FILE," error: %s: expecting %d, got %d", expr, j, i)); 77 77 } 78 return i;78 return r; 79 79 } 80 80 #else … … 95 95 * pj_strnicmp2(). 96 96 */ 97 #define STRTEST(res, S1,S2,code) \97 #define STRTEST(res,res2,S1,S2,code) \ 98 98 do { \ 99 99 s1.ptr=S1; s1.slen=(S1)?len:0; \ … … 109 109 pj_sub_timestamp(&t2, &t1); \ 110 110 pj_add_timestamp(&e2, &t2); \ 111 if (C(pj_stricmp2(&s1,S2),res )) return code*10; \111 if (C(pj_stricmp2(&s1,S2),res2)) return code*10; \ 112 112 if (C(pj_strnicmp(&s1,&s2,len),res)) return code*100; \ 113 113 if (C(pj_strnicmp2(&s1,S2,len),res)) return code*1000; \ … … 128 128 /* Compare empty strings. */ 129 129 len=0; 130 STRTEST( 0, "","",-500);131 STRTEST( 0, SNULL,"",-502);132 STRTEST( 0, "",SNULL,-504);133 STRTEST( 0, SNULL,SNULL,-506);134 STRTEST( 0, "hello","world",-508);130 STRTEST( 0, 0, "","",-500); 131 STRTEST( 0, 0, SNULL,"",-502); 132 STRTEST( 0, 0, "",SNULL,-504); 133 STRTEST( 0, 0, SNULL,SNULL,-506); 134 STRTEST( 0, -1, "hello","world",-508); 135 135 136 136 /* equal, length=1 … … 139 139 buf = "a""A"; 140 140 len=1; 141 STRTEST( 0, "a",buf+0,-510);142 STRTEST( 0, "a",buf+1,-512);143 STRTEST( -1, "O", "P", -514);144 STRTEST(-1, SNULL, "a", -516);145 STRTEST( 1, "a", SNULL, -518);141 STRTEST( 0, -1, "a",buf+0,-510); 142 STRTEST( 0, 0, "a",buf+1,-512); 143 STRTEST(-1, -1, "O", "P", -514); 144 STRTEST(-1, -1, SNULL, "a", -516); 145 STRTEST( 1, 1, "a", SNULL, -518); 146 146 147 147 /* equal, length=2 … … 150 150 buf = "aa""Aa""aA""AA"; 151 151 len=2; 152 STRTEST( 0, "aa",buf+0,-520);153 STRTEST( 0, "aa",buf+2,-522);154 STRTEST( 0, "aa",buf+4,-524);155 STRTEST( 0, "aa",buf+6,-524);152 STRTEST( 0, -1, "aa",buf+0,-520); 153 STRTEST( 0, -1, "aa",buf+2,-522); 154 STRTEST( 0, -1, "aa",buf+4,-524); 155 STRTEST( 0, 0, "aa",buf+6,-524); 156 156 157 157 /* equal, length=3 … … 160 160 buf = "aaa""Aaa""aAa""aaA""AAa""aAA""AaA""AAA"; 161 161 len=3; 162 STRTEST( 0, "aaa",buf+0,-530);163 STRTEST( 0, "aaa",buf+3,-532);164 STRTEST( 0, "aaa",buf+6,-534);165 STRTEST( 0, "aaa",buf+9,-536);166 STRTEST( 0, "aaa",buf+12,-538);167 STRTEST( 0, "aaa",buf+15,-540);168 STRTEST( 0, "aaa",buf+18,-542);169 STRTEST( 0, "aaa",buf+21,-534);162 STRTEST( 0, -1, "aaa",buf+0,-530); 163 STRTEST( 0, -1, "aaa",buf+3,-532); 164 STRTEST( 0, -1, "aaa",buf+6,-534); 165 STRTEST( 0, -1, "aaa",buf+9,-536); 166 STRTEST( 0, -1, "aaa",buf+12,-538); 167 STRTEST( 0, -1, "aaa",buf+15,-540); 168 STRTEST( 0, -1, "aaa",buf+18,-542); 169 STRTEST( 0, 0, "aaa",buf+21,-534); 170 170 171 171 /* equal, length=4 */ 172 172 len=4; 173 STRTEST( 0, "aaaa","aaaa",-540);174 STRTEST( 0, "aaaa","Aaaa",-542);175 STRTEST( 0, "aaaa","aAaa",-544);176 STRTEST( 0, "aaaa","aaAa",-546);177 STRTEST( 0, "aaaa","aaaA",-548);178 STRTEST( 0, "aaaa","AAaa",-550);179 STRTEST( 0, "aaaa","aAAa",-552);180 STRTEST( 0, "aaaa","aaAA",-554);181 STRTEST( 0, "aaaa","AaAa",-556);182 STRTEST( 0, "aaaa","aAaA",-558);183 STRTEST( 0, "aaaa","AaaA",-560);184 STRTEST( 0, "aaaa","AAAa",-562);185 STRTEST( 0, "aaaa","aAAA",-564);186 STRTEST( 0, "aaaa","AAaA",-566);187 STRTEST( 0, "aaaa","AaAA",-568);188 STRTEST( 0, "aaaa","AAAA",-570);173 STRTEST( 0, 0, "aaaa","aaaa",-540); 174 STRTEST( 0, 0, "aaaa","Aaaa",-542); 175 STRTEST( 0, 0, "aaaa","aAaa",-544); 176 STRTEST( 0, 0, "aaaa","aaAa",-546); 177 STRTEST( 0, 0, "aaaa","aaaA",-548); 178 STRTEST( 0, 0, "aaaa","AAaa",-550); 179 STRTEST( 0, 0, "aaaa","aAAa",-552); 180 STRTEST( 0, 0, "aaaa","aaAA",-554); 181 STRTEST( 0, 0, "aaaa","AaAa",-556); 182 STRTEST( 0, 0, "aaaa","aAaA",-558); 183 STRTEST( 0, 0, "aaaa","AaaA",-560); 184 STRTEST( 0, 0, "aaaa","AAAa",-562); 185 STRTEST( 0, 0, "aaaa","aAAA",-564); 186 STRTEST( 0, 0, "aaaa","AAaA",-566); 187 STRTEST( 0, 0, "aaaa","AaAA",-568); 188 STRTEST( 0, 0, "aaaa","AAAA",-570); 189 189 190 190 /* equal, length=5 */ 191 191 buf = "aaaAa""AaaaA""AaAaA""AAAAA"; 192 192 len=5; 193 STRTEST( 0, "aaaaa",buf+0,-580);194 STRTEST( 0, "aaaaa",buf+5,-582);195 STRTEST( 0, "aaaaa",buf+10,-584);196 STRTEST( 0, "aaaaa",buf+15,-586);193 STRTEST( 0, -1, "aaaaa",buf+0,-580); 194 STRTEST( 0, -1, "aaaaa",buf+5,-582); 195 STRTEST( 0, -1, "aaaaa",buf+10,-584); 196 STRTEST( 0, 0, "aaaaa",buf+15,-586); 197 197 198 198 /* not equal, length=1 */ 199 199 len=1; 200 STRTEST( -1, "a", "b", -600);200 STRTEST( -1, -1, "a", "b", -600); 201 201 202 202 /* not equal, length=2 */ 203 203 buf = "ab""ba"; 204 204 len=2; 205 STRTEST( -1, "aa", buf+0, -610);206 STRTEST( -1, "aa", buf+2, -612);205 STRTEST( -1, -1, "aa", buf+0, -610); 206 STRTEST( -1, -1, "aa", buf+2, -612); 207 207 208 208 /* not equal, length=3 */ 209 209 buf = "aab""aba""baa"; 210 210 len=3; 211 STRTEST( -1, "aaa", buf+0, -620);212 STRTEST( -1, "aaa", buf+3, -622);213 STRTEST( -1, "aaa", buf+6, -624);211 STRTEST( -1, -1, "aaa", buf+0, -620); 212 STRTEST( -1, -1, "aaa", buf+3, -622); 213 STRTEST( -1, -1, "aaa", buf+6, -624); 214 214 215 215 /* not equal, length=4 */ 216 216 buf = "aaab""aaba""abaa""baaa"; 217 217 len=4; 218 STRTEST( -1, "aaaa", buf+0, -630);219 STRTEST( -1, "aaaa", buf+4, -632);220 STRTEST( -1, "aaaa", buf+8, -634);221 STRTEST( -1, "aaaa", buf+12, -636);218 STRTEST( -1, -1, "aaaa", buf+0, -630); 219 STRTEST( -1, -1, "aaaa", buf+4, -632); 220 STRTEST( -1, -1, "aaaa", buf+8, -634); 221 STRTEST( -1, -1, "aaaa", buf+12, -636); 222 222 223 223 /* not equal, length=5 */ 224 224 buf="aaaab""aaaba""aabaa""abaaa""baaaa"; 225 225 len=5; 226 STRTEST( -1, "aaaaa", buf+0, -640);227 STRTEST( -1, "aaaaa", buf+5, -642);228 STRTEST( -1, "aaaaa", buf+10, -644);229 STRTEST( -1, "aaaaa", buf+15, -646);230 STRTEST( -1, "aaaaa", buf+20, -648);226 STRTEST( -1, -1, "aaaaa", buf+0, -640); 227 STRTEST( -1, -1, "aaaaa", buf+5, -642); 228 STRTEST( -1, -1, "aaaaa", buf+10, -644); 229 STRTEST( -1, -1, "aaaaa", buf+15, -646); 230 STRTEST( -1, -1, "aaaaa", buf+20, -648); 231 231 232 232 zero.u32.hi = zero.u32.lo = 0;
Note: See TracChangeset
for help on using the changeset viewer.