InternalStringNoCaseComparer.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include "CommonDataStructuresPch.h"
  6. #include "DataStructures/CharacterBuffer.h"
  7. #include "DataStructures/InternalStringNoCaseComparer.h"
  8. namespace JsUtil
  9. {
  10. bool NoCaseComparer<JsUtil::CharacterBuffer<WCHAR>>::Equals(JsUtil::CharacterBuffer<WCHAR> const& s1, JsUtil::CharacterBuffer<WCHAR> const& s2)
  11. {
  12. return (s1.GetLength() == s2.GetLength()) && (NoCaseComparer<JsUtil::CharacterBuffer<WCHAR>>::Compare(s1, s2)==0);
  13. }
  14. hash_t NoCaseComparer<JsUtil::CharacterBuffer<WCHAR>>::GetHashCode(JsUtil::CharacterBuffer<WCHAR> const& s1)
  15. {
  16. const char16* s = s1.GetBuffer();
  17. size_t length = s1.GetLength();
  18. hash_t hash = CC_HASH_OFFSET_VALUE;
  19. for (size_t i = 0; i < length; i++)
  20. {
  21. CC_HASH_LOGIC(hash, tolower(s[i]));
  22. }
  23. return ((hash & 0x7fffffff) << 1) | 1;
  24. }
  25. int NoCaseComparer<JsUtil::CharacterBuffer<WCHAR>>::Compare(JsUtil::CharacterBuffer<WCHAR> const& s1, JsUtil::CharacterBuffer<WCHAR> const& s2)
  26. {
  27. if (s1.GetLength() != s2.GetLength()) return +1;
  28. int count = s1.GetLength();
  29. const char16* buf1 = s1.GetBuffer();
  30. const char16* buf2 = s2.GetBuffer();
  31. for (int i=0; i < count; i++)
  32. {
  33. if (tolower(buf1[i]) != tolower(buf2[i]))
  34. {
  35. return (buf1[i] < buf2[i] ? -1 : +1);
  36. }
  37. }
  38. return (0);
  39. }
  40. }