hash-string.h 1.9 KB

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