Changeset 72 for pjproject/trunk
- Timestamp:
- Nov 21, 2005 5:01:50 PM (19 years ago)
- Location:
- pjproject/trunk/pjlib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/string.h
r66 r72 335 335 * @return 336 336 * - < 0 if str1 is less than str2 337 * - 0 if str1 is identical to str2 338 * - > 0 if str1 is greater than str2 339 */ 340 PJ_IDECL(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2); 337 * - 0 if str1 is equal to str2 338 * - > 0 if str1 is greater than str2 339 */ 340 PJ_IDECL(int) pj_stricmp(const pj_str_t *str1, const pj_str_t *str2); 341 342 /** 343 * Perform lowercase comparison to the strings which consists of only 344 * alnum characters. More over, it will only return non-zero if both 345 * strings are not equal, and is not able to detect which string is 346 * 'less'. 347 * 348 * If non-alnum inputs are given, then the function may mistakenly 349 * treat two strings as equal while they're not. 350 * 351 * @param str1 The string to compare. 352 * @param str2 The string to compare. 353 * 354 * @return 355 * - 0 if str1 is equal to str2 356 * - (-1) if not equal. 357 */ 358 PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2); 341 359 342 360 /** -
pjproject/trunk/pjlib/include/pj/string_i.h
r66 r72 18 18 */ 19 19 20 #include <pj/pool.h> 21 20 22 PJ_IDEF(pj_str_t) pj_str(char *str) 21 23 { … … 158 160 PJ_IDEF(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2) 159 161 { 160 pj_ssize_t diff; 161 162 diff = str1->slen - str2->slen; 163 if (diff) { 164 return (int)diff; 165 } else { 166 return pj_native_strnicmp(str1->ptr, str2->ptr, str1->slen); 162 register int len = str1->slen; 163 if (len != str2->slen) { 164 return (int)(len - str2->slen); 165 } else if (len == 0) { 166 return 0; 167 } else { 168 return pj_native_strnicmp(str1->ptr, str2->ptr, len); 169 } 170 } 171 172 PJ_IDEF(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2) 173 { 174 register int len = str1->slen; 175 176 if (len != str2->slen) { 177 return -1; 178 } else if (len == 0) { 179 return 0; 180 } else { 181 register const pj_uint32_t *p1 = (pj_uint32_t*)str1->ptr, 182 *p2 = (pj_uint32_t*)str2->ptr; 183 while (len > 3 && (*p1 & 0x1F1F1F1F)==(*p2 & 0x1F1F1F1F)) 184 ++p1, ++p2, len-=4; 185 186 if (len > 3) 187 return -1; 188 #if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0 189 else if (len==3) 190 return ((*p1 & 0x001F1F1F)==(*p2 & 0x001F1F1F)) ? 0 : -1; 191 else if (len==2) 192 return ((*p1 & 0x00001F1F)==(*p2 & 0x00001F1F)) ? 0 : -1; 193 else if (len==1) 194 return ((*p1 & 0x0000001F)==(*p2 & 0x0000001F)) ? 0 : -1; 195 #else 196 else if (len==3) 197 return ((*p1 & 0x1F1F1F00)==(*p2 & 0x1F1F1F00)) ? 0 : -1; 198 else if (len==2) 199 return ((*p1 & 0x1F1F0000)==(*p2 & 0x1F1F0000)) ? 0 : -1; 200 else if (len==1) 201 return ((*p1 & 0x1F000000)==(*p2 & 0x1F000000)) ? 0 : -1; 202 #endif 203 else 204 return 0; 167 205 } 168 206 } -
pjproject/trunk/pjlib/src/pjlib-test/string.c
r65 r72 20 20 #include <pj/pool.h> 21 21 #include <pj/log.h> 22 #include <pj/os.h> 22 23 #include "test.h" 23 24 … … 64 65 #define UL_VALUE 3456789012UL 65 66 67 static int stricmp_test(void) 68 { 69 #define STRTEST(res,S1,S2,code) \ 70 do { \ 71 s1.ptr=S1; s1.slen=len; \ 72 s2.ptr=S2; s2.slen=len; \ 73 pj_get_timestamp(&t1); \ 74 if (pj_stricmp(&s1,&s2)!=res) return code; \ 75 pj_get_timestamp(&t2); \ 76 pj_sub_timestamp(&t2, &t1); \ 77 pj_add_timestamp(&e1, &t2); \ 78 pj_get_timestamp(&t1); \ 79 if (pj_stricmp_alnum(&s1,&s2)!=res) return code-1; \ 80 pj_get_timestamp(&t2); \ 81 pj_sub_timestamp(&t2, &t1); \ 82 pj_add_timestamp(&e2, &t2); \ 83 } while (0) 84 85 char *buf; 86 pj_str_t s1, s2; 87 pj_timestamp t1, t2, e1, e2, zero; 88 pj_uint32_t c1, c2; 89 int len; 90 91 e1.u32.hi = e1.u32.lo = e2.u32.hi = e2.u32.lo = 0; 92 93 pj_thread_sleep(0); 94 95 /* Compare empty strings. */ 96 len=0; 97 STRTEST( 0, "","",-500); 98 99 /* equal, length=1 100 * use buffer to simulate non-aligned string. 101 */ 102 buf = "a""A"; 103 len=1; 104 STRTEST( 0, "a",buf+0,-510); 105 STRTEST( 0, "a",buf+1,-512); 106 107 /* equal, length=2 108 * use buffer to simulate non-aligned string. 109 */ 110 buf = "aa""Aa""aA""AA"; 111 len=2; 112 STRTEST( 0, "aa",buf+0,-520); 113 STRTEST( 0, "aa",buf+2,-522); 114 STRTEST( 0, "aa",buf+4,-524); 115 STRTEST( 0, "aa",buf+6,-524); 116 117 /* equal, length=3 118 * use buffer to simulate non-aligned string. 119 */ 120 buf = "aaa""Aaa""aAa""aaA""AAa""aAA""AaA""AAA"; 121 len=3; 122 STRTEST( 0, "aaa",buf+0,-530); 123 STRTEST( 0, "aaa",buf+3,-532); 124 STRTEST( 0, "aaa",buf+6,-534); 125 STRTEST( 0, "aaa",buf+9,-536); 126 STRTEST( 0, "aaa",buf+12,-538); 127 STRTEST( 0, "aaa",buf+15,-540); 128 STRTEST( 0, "aaa",buf+18,-542); 129 STRTEST( 0, "aaa",buf+21,-534); 130 131 /* equal, length=4 */ 132 len=4; 133 STRTEST( 0, "aaaa","aaaa",-540); 134 STRTEST( 0, "aaaa","Aaaa",-542); 135 STRTEST( 0, "aaaa","aAaa",-544); 136 STRTEST( 0, "aaaa","aaAa",-546); 137 STRTEST( 0, "aaaa","aaaA",-548); 138 STRTEST( 0, "aaaa","AAaa",-550); 139 STRTEST( 0, "aaaa","aAAa",-552); 140 STRTEST( 0, "aaaa","aaAA",-554); 141 STRTEST( 0, "aaaa","AaAa",-556); 142 STRTEST( 0, "aaaa","aAaA",-558); 143 STRTEST( 0, "aaaa","AaaA",-560); 144 STRTEST( 0, "aaaa","AAAa",-562); 145 STRTEST( 0, "aaaa","aAAA",-564); 146 STRTEST( 0, "aaaa","AAaA",-566); 147 STRTEST( 0, "aaaa","AaAA",-568); 148 STRTEST( 0, "aaaa","AAAA",-570); 149 150 /* equal, length=5 */ 151 buf = "aaaAa""AaaaA""AaAaA""AAAAA"; 152 len=5; 153 STRTEST( 0, "aaaaa",buf+0,-580); 154 STRTEST( 0, "aaaaa",buf+5,-582); 155 STRTEST( 0, "aaaaa",buf+10,-584); 156 STRTEST( 0, "aaaaa",buf+15,-586); 157 158 /* not equal, length=1 */ 159 len=1; 160 STRTEST( -1, "a", "b", -600); 161 162 /* not equal, length=2 */ 163 buf = "ab""ba"; 164 len=2; 165 STRTEST( -1, "aa", buf+0, -610); 166 STRTEST( -1, "aa", buf+2, -612); 167 168 /* not equal, length=3 */ 169 buf = "aab""aba""baa"; 170 len=3; 171 STRTEST( -1, "aaa", buf+0, -620); 172 STRTEST( -1, "aaa", buf+3, -622); 173 STRTEST( -1, "aaa", buf+6, -624); 174 175 /* not equal, length=4 */ 176 buf = "aaab""aaba""abaa""baaa"; 177 len=4; 178 STRTEST( -1, "aaaa", buf+0, -630); 179 STRTEST( -1, "aaaa", buf+4, -632); 180 STRTEST( -1, "aaaa", buf+8, -634); 181 STRTEST( -1, "aaaa", buf+12, -636); 182 183 /* not equal, length=5 */ 184 buf="aaaab""aaaba""aabaa""abaaa""baaaa"; 185 len=5; 186 STRTEST( -1, "aaaaa", buf+0, -640); 187 STRTEST( -1, "aaaaa", buf+5, -642); 188 STRTEST( -1, "aaaaa", buf+10, -644); 189 STRTEST( -1, "aaaaa", buf+15, -646); 190 STRTEST( -1, "aaaaa", buf+20, -648); 191 192 zero.u32.hi = zero.u32.lo = 0; 193 c1 = pj_elapsed_cycle(&zero, &e1); 194 c2 = pj_elapsed_cycle(&zero, &e2); 195 196 if (c1 < c2) { 197 PJ_LOG(3,("", " error: pj_stricmp_alnum is slower than pj_stricmp!")); 198 return -700; 199 } 200 201 PJ_LOG(3, ("", " time: stricmp=%u, stricmp_alnum=%u (speedup=%d.%02dx)", 202 c1, c2, 203 (c1 * 100 / c2) / 100, 204 (c1 * 100 / c2) % 100)); 205 return 0; 206 #undef STRTEST 207 } 208 66 209 int string_test(void) 67 210 { … … 163 306 /* Done. */ 164 307 pj_pool_release(pool); 165 return 0; 308 309 return stricmp_test(); 166 310 } 167 311
Note: See TracChangeset
for help on using the changeset viewer.