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