You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash-string.h 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Description of GNU message catalog format: string hashing function.
  2. Copyright (C) 1995, 1997-1998, 2000-2003 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. /* We assume to have `unsigned long int' value with at least 32 bits. */
  17. #define HASHWORDBITS 32
  18. /* Defines the so called `hashpjw' function by P.J. Weinberger
  19. [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
  20. 1986, 1987 Bell Telephone Laboratories, Inc.] */
  21. static inline unsigned long int
  22. hash_string (const char *str_param)
  23. {
  24. unsigned long int hval, g;
  25. const char *str = str_param;
  26. /* Compute the hash value for the given string. */
  27. hval = 0;
  28. while (*str != '\0')
  29. {
  30. hval <<= 4;
  31. hval += (unsigned char) *str++;
  32. g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
  33. if (g != 0)
  34. {
  35. hval ^= g >> (HASHWORDBITS - 8);
  36. hval ^= g;
  37. }
  38. }
  39. return hval;
  40. }