HashFunc.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. ULONG CaseSensitiveComputeHash(LPCOLESTR prgch, LPCOLESTR end)
  19. {
  20. ULONG luHash = 0;
  21. while (prgch < end)
  22. {
  23. luHash = 17 * luHash + *(char16 *)prgch++;
  24. }
  25. return luHash;
  26. }
  27. ULONG CaseSensitiveComputeHash(LPCUTF8 prgch, LPCUTF8 end)
  28. {
  29. utf8::DecodeOptions options = utf8::doAllowThreeByteSurrogates;
  30. ULONG luHash = 0;
  31. while (prgch < end)
  32. {
  33. luHash = 17 * luHash + utf8::Decode(prgch, end, options);
  34. }
  35. return luHash;
  36. }
  37. ULONG CaseSensitiveComputeHash(char const * prgch, char const * end)
  38. {
  39. ULONG luHash = 0;
  40. while (prgch < end)
  41. {
  42. Assert(utf8::IsStartByte(*prgch) && !utf8::IsLeadByte(*prgch));
  43. luHash = 17 * luHash + *prgch++;
  44. }
  45. return luHash;
  46. }
  47. ULONG CaseInsensitiveComputeHash(LPCOLESTR posz)
  48. {
  49. ULONG luHash = 0;
  50. char16 ch;
  51. while (0 != (ch = *(char16 *)posz++))
  52. {
  53. if (ch <= 'Z' && ch >= 'A')
  54. ch += 'a' - 'A';
  55. luHash = 17 * luHash + ch;
  56. }
  57. return luHash;
  58. }