hash-string.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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
  4. it under the terms of the GNU General Public License as published by
  5. 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* @@ end of prolog @@ */
  15. #ifndef PARAMS
  16. # if __STDC__
  17. # define PARAMS(Args) Args
  18. # else
  19. # define PARAMS(Args) ()
  20. # endif
  21. #endif
  22. /* We assume to have `unsigned long int' value with at least 32 bits. */
  23. #define HASHWORDBITS 32
  24. /* Defines the so called `hashpjw' function by P.J. Weinberger
  25. [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
  26. 1986, 1987 Bell Telephone Laboratories, Inc.] */
  27. static unsigned long int hash_string PARAMS ((const char *__str_param));
  28. static inline unsigned long int
  29. hash_string (str_param)
  30. const char *str_param;
  31. {
  32. unsigned long int hval, g;
  33. const char *str = str_param;
  34. /* Compute the hash value for the given string. */
  35. hval = 0;
  36. while (*str != '\0')
  37. {
  38. hval <<= 4;
  39. hval += (unsigned long int) *str++;
  40. g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
  41. if (g != 0)
  42. {
  43. hval ^= g >> (HASHWORDBITS - 8);
  44. hval ^= g;
  45. }
  46. }
  47. return hval;
  48. }