blob: b5a7d4132c45a85bea86e3e1c464d1fde4b97be1 [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* hash-string.h - Description of GNU message catalog format: string hashing function. */
Jari Aaltob80f6442004-07-27 13:29:18 +00002
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1995, 1997, 1998, 2000, 2001, 2005-2009 Free Software Foundation, Inc.
Jari Aaltob80f6442004-07-27 13:29:18 +00004
Jari Aalto31859422009-01-12 13:36:28 +00005 This file is part of GNU Bash.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
Jari Aaltob80f6442004-07-27 13:29:18 +000013 but WITHOUT ANY WARRANTY; without even the implied warranty of
Jari Aalto31859422009-01-12 13:36:28 +000014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
Jari Aaltob80f6442004-07-27 13:29:18 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aaltob80f6442004-07-27 13:29:18 +000020
21/* @@ end of prolog @@ */
22
23#ifndef PARAMS
24# if __STDC__ || defined __GNUC__ || defined __SUNPRO_C || defined __cplusplus || __PROTOTYPES
25# define PARAMS(Args) Args
26# else
27# define PARAMS(Args) ()
28# endif
29#endif
30
31/* We assume to have `unsigned long int' value with at least 32 bits. */
32#define HASHWORDBITS 32
33
34
35/* Defines the so called `hashpjw' function by P.J. Weinberger
36 [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
37 1986, 1987 Bell Telephone Laboratories, Inc.] */
38static unsigned long int hash_string PARAMS ((const char *__str_param));
39
40static inline unsigned long int
41hash_string (str_param)
42 const char *str_param;
43{
44 unsigned long int hval, g;
45 const char *str = str_param;
46
47 /* Compute the hash value for the given string. */
48 hval = 0;
49 while (*str != '\0')
50 {
51 hval <<= 4;
52 hval += (unsigned long int) *str++;
53 g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
54 if (g != 0)
55 {
56 hval ^= g >> (HASHWORDBITS - 8);
57 hval ^= g;
58 }
59 }
60 return hval;
61}