HashFunc.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. /*
  6. * IMPORTANT:
  7. * This file does not compile stand alone. It was required so that
  8. * the same code could be built into a utility program comphash.exe as well
  9. * as the scripting dll's. This file is included in core\comphash.cpp
  10. * to be used by comphash.exe. It is included in core\scrutil.cpp where to
  11. * be used by jscript.dll and vbscript.dll.
  12. *
  13. * comphash.exe is a utility used in the build to generate a source code file
  14. * containing a table of hash values associated with strings needed by
  15. * jscript and vbscript. It is highly desirable to have a single definition
  16. * of the hash function so things don't go out of sync.
  17. */
  18. // scaffolding - define ULONG
  19. typedef unsigned long ULONG;
  20. ULONG CaseSensitiveComputeHashCch(LPCOLESTR prgch, long cch)
  21. {
  22. ULONG luHash = 0;
  23. while (cch-- > 0)
  24. luHash = 17 * luHash + *(char16 *)prgch++;
  25. return luHash;
  26. }
  27. ULONG CaseSensitiveComputeHashCch(LPCUTF8 prgch, long cch)
  28. {
  29. utf8::DecodeOptions options = utf8::doAllowThreeByteSurrogates;
  30. ULONG luHash = 0;
  31. while (cch-- > 0)
  32. luHash = 17 * luHash + utf8::Decode(prgch, prgch + 4, options); // WARNING: Assume cch correct, suppress end-of-buffer checking
  33. return luHash;
  34. }
  35. ULONG CaseSensitiveComputeHashCch(char const * prgch, long cch)
  36. {
  37. ULONG luHash = 0;
  38. while (cch-- > 0)
  39. {
  40. Assert(utf8::IsStartByte(*prgch) && !utf8::IsLeadByte(*prgch));
  41. luHash = 17 * luHash + *prgch++;
  42. }
  43. return luHash;
  44. }
  45. ULONG CaseInsensitiveComputeHash(LPCOLESTR posz)
  46. {
  47. ULONG luHash = 0;
  48. char16 ch;
  49. while (0 != (ch = *(char16 *)posz++))
  50. {
  51. if (ch <= 'Z' && ch >= 'A')
  52. ch += 'a' - 'A';
  53. luHash = 17 * luHash + ch;
  54. }
  55. return luHash;
  56. }