Changeset 4208 for pjproject/trunk/pjlib/src/pj/hash.c
- Timestamp:
- Jul 18, 2012 7:52:33 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pj/hash.c
r3553 r4208 80 80 for (i=0; i<key->slen; ++i) { 81 81 pj_uint8_t c = key->ptr[i]; 82 char lower; 82 83 if (c & 64) 83 result[i]= (char)(c | 32);84 lower = (char)(c | 32); 84 85 else 85 result[i] = (char)c; 86 hval = hval * PJ_HASH_MULTIPLIER + result[i]; 86 lower = (char)c; 87 if (result) 88 result[i] = lower; 89 hval = hval * PJ_HASH_MULTIPLIER + lower; 87 90 } 88 91 #else 89 92 for (i=0; i<key->slen; ++i) { 90 result[i] = (char)pj_tolower(key->ptr[i]); 91 hval = hval * PJ_HASH_MULTIPLIER + result[i]; 93 char lower = (char)pj_tolower(key->ptr[i]); 94 if (result) 95 result[i] = lower; 96 hval = hval * PJ_HASH_MULTIPLIER + lower; 92 97 } 93 98 #endif … … 129 134 const void *key, unsigned keylen, 130 135 void *val, pj_uint32_t *hval, 131 void *entry_buf )136 void *entry_buf, pj_bool_t lower) 132 137 { 133 138 pj_uint32_t hash; … … 147 152 const pj_uint8_t *p = (const pj_uint8_t*)key; 148 153 for ( ; *p; ++p ) { 149 hash = hash * PJ_HASH_MULTIPLIER + *p; 154 if (lower) 155 hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p); 156 else 157 hash = hash * PJ_HASH_MULTIPLIER + *p; 150 158 } 151 159 keylen = p - (const unsigned char*)key; … … 154 162 *end = p + keylen; 155 163 for ( ; p!=end; ++p) { 156 hash = hash * PJ_HASH_MULTIPLIER + *p; 164 if (lower) 165 hash = hash * PJ_HASH_MULTIPLIER + pj_tolower(*p); 166 else 167 hash = hash * PJ_HASH_MULTIPLIER + *p; 157 168 } 158 169 } … … 168 179 p_entry = &entry->next, entry = *p_entry) 169 180 { 181 pj_str_t str; 182 183 if (lower) { 184 str.ptr = (char *)entry->key; 185 str.slen = keylen; 186 } 170 187 if (entry->hash==hash && entry->keylen==keylen && 171 pj_memcmp(entry->key, key, keylen)==0) 188 ((lower && pj_strnicmp2(&str, (const char *)key, keylen)==0) || 189 (!lower && pj_memcmp(entry->key, key, keylen)==0))) 172 190 { 173 191 break; … … 215 233 { 216 234 pj_hash_entry *entry; 217 entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL );235 entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_FALSE); 218 236 return entry ? entry->value : NULL; 219 237 } 220 238 221 PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht, 222 const void *key, unsigned keylen, pj_uint32_t hval, 223 void *value ) 239 PJ_DEF(void *) pj_hash_get_lower( pj_hash_table_t *ht, 240 const void *key, unsigned keylen, 241 pj_uint32_t *hval) 242 { 243 pj_hash_entry *entry; 244 entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL, PJ_TRUE); 245 return entry ? entry->value : NULL; 246 } 247 248 static void hash_set( pj_pool_t *pool, pj_hash_table_t *ht, 249 const void *key, unsigned keylen, pj_uint32_t hval, 250 void *value, void *entry_buf, pj_bool_t lower ) 224 251 { 225 252 pj_hash_entry **p_entry; 226 253 227 p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL); 254 p_entry = find_entry( pool, ht, key, keylen, value, &hval, entry_buf, 255 lower); 228 256 if (*p_entry) { 229 257 if (value == NULL) { … … 242 270 } 243 271 272 PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht, 273 const void *key, unsigned keylen, pj_uint32_t hval, 274 void *value ) 275 { 276 hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_FALSE); 277 } 278 279 PJ_DEF(void) pj_hash_set_lower( pj_pool_t *pool, pj_hash_table_t *ht, 280 const void *key, unsigned keylen, 281 pj_uint32_t hval, void *value ) 282 { 283 hash_set(pool, ht, key, keylen, hval, value, NULL, PJ_TRUE); 284 } 285 244 286 PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht, 245 287 const void *key, unsigned keylen, … … 247 289 void *value) 248 290 { 249 pj_hash_entry **p_entry; 250 251 p_entry = find_entry( NULL, ht, key, keylen, value, &hval, 252 (void*)entry_buf ); 253 if (*p_entry) { 254 if (value == NULL) { 255 /* delete entry */ 256 PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry)); 257 *p_entry = (*p_entry)->next; 258 --ht->count; 259 260 } else { 261 /* overwrite */ 262 (*p_entry)->value = value; 263 PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, 264 *p_entry, value)); 265 } 266 } 291 hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_FALSE); 292 } 293 294 PJ_DEF(void) pj_hash_set_np_lower( pj_hash_table_t *ht, 295 const void *key, unsigned keylen, 296 pj_uint32_t hval, 297 pj_hash_entry_buf entry_buf, 298 void *value) 299 { 300 hash_set(NULL, ht, key, keylen, hval, value, (void *)entry_buf, PJ_TRUE); 267 301 } 268 302
Note: See TracChangeset
for help on using the changeset viewer.