1 1.1 christos /* Implements a string hashing function. 2 1.1 christos Copyright (C) 1995, 1997, 1998, 2000, 2003 Free Software Foundation, Inc. 3 1.1 christos This file is part of the GNU C Library. 4 1.1 christos 5 1.1 christos The GNU C Library is free software; you can redistribute it and/or 6 1.1 christos modify it under the terms of the GNU Lesser General Public 7 1.1 christos License as published by the Free Software Foundation; either 8 1.1 christos version 2.1 of the License, or (at your option) any later version. 9 1.1 christos 10 1.1 christos The GNU C Library is distributed in the hope that it will be useful, 11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 1.1 christos Lesser General Public License for more details. 14 1.1 christos 15 1.1 christos You should have received a copy of the GNU Lesser General Public 16 1.1 christos License along with the GNU C Library; if not, write to the Free 17 1.1 christos Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 1.1 christos Boston, MA 02110-1301, USA. */ 19 1.1 christos 20 1.1 christos #ifdef HAVE_CONFIG_H 21 1.1 christos # include <config.h> 22 1.1 christos #endif 23 1.1 christos 24 1.1 christos /* Specification. */ 25 1.1 christos #include "hash-string.h" 26 1.1 christos 27 1.1 christos 28 1.1 christos /* Defines the so called `hashpjw' function by P.J. Weinberger 29 1.1 christos [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools, 30 1.1 christos 1986, 1987 Bell Telephone Laboratories, Inc.] */ 31 1.1 christos unsigned long int 32 1.1 christos __hash_string (const char *str_param) 33 1.1 christos { 34 1.1 christos unsigned long int hval, g; 35 1.1 christos const char *str = str_param; 36 1.1 christos 37 1.1 christos /* Compute the hash value for the given string. */ 38 1.1 christos hval = 0; 39 1.1 christos while (*str != '\0') 40 1.1 christos { 41 1.1 christos hval <<= 4; 42 1.1 christos hval += (unsigned char) *str++; 43 1.1 christos g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4)); 44 1.1 christos if (g != 0) 45 1.1 christos { 46 1.1 christos hval ^= g >> (HASHWORDBITS - 8); 47 1.1 christos hval ^= g; 48 1.1 christos } 49 1.1 christos } 50 1.1 christos return hval; 51 1.1 christos } 52