1 1.1 christos /* $NetBSD: hash-string.h,v 1.1.1.1 2016/01/14 00:11:27 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* Description of GNU message catalog format: string hashing function. 4 1.1 christos Copyright (C) 1995, 1997-1998, 2000-2003 Free Software Foundation, Inc. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify it 7 1.1 christos under the terms of the GNU Library General Public License as published 8 1.1 christos by the Free Software Foundation; either version 2, or (at your option) 9 1.1 christos any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 1.1 christos Library General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU Library General Public 17 1.1 christos License along with this program; if not, write to the Free Software 18 1.1 christos Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 19 1.1 christos USA. */ 20 1.1 christos 21 1.1 christos /* @@ end of prolog @@ */ 22 1.1 christos 23 1.1 christos /* We assume to have `unsigned long int' value with at least 32 bits. */ 24 1.1 christos #define HASHWORDBITS 32 25 1.1 christos 26 1.1 christos 27 1.1 christos /* Defines the so called `hashpjw' function by P.J. Weinberger 28 1.1 christos [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools, 29 1.1 christos 1986, 1987 Bell Telephone Laboratories, Inc.] */ 30 1.1 christos static inline unsigned long int 31 1.1 christos hash_string (const char *str_param) 32 1.1 christos { 33 1.1 christos unsigned long int hval, g; 34 1.1 christos const char *str = str_param; 35 1.1 christos 36 1.1 christos /* Compute the hash value for the given string. */ 37 1.1 christos hval = 0; 38 1.1 christos while (*str != '\0') 39 1.1 christos { 40 1.1 christos hval <<= 4; 41 1.1 christos hval += (unsigned char) *str++; 42 1.1 christos g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4)); 43 1.1 christos if (g != 0) 44 1.1 christos { 45 1.1 christos hval ^= g >> (HASHWORDBITS - 8); 46 1.1 christos hval ^= g; 47 1.1 christos } 48 1.1 christos } 49 1.1 christos return hval; 50 1.1 christos } 51