InternalStringNoCaseComparer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. uint NoCaseComparer<JsUtil::CharacterBuffer<WCHAR>>::GetHashCode(JsUtil::CharacterBuffer<WCHAR> const& s1)
  15. {
  16. const char16* s = s1.GetBuffer();
  17. size_t length = s1.GetLength();
  18. uint hash = 0;
  19. for (size_t i = 0; i < length; i++)
  20. {
  21. hash = _rotl(hash, 7);
  22. hash ^= tolower(s[i]);
  23. }
  24. return ((hash & 0x7fffffff) << 1) | 1;
  25. }
  26. int NoCaseComparer<JsUtil::CharacterBuffer<WCHAR>>::Compare(JsUtil::CharacterBuffer<WCHAR> const& s1, JsUtil::CharacterBuffer<WCHAR> const& s2)
  27. {
  28. if (s1.GetLength() != s2.GetLength()) return +1;
  29. int count = s1.GetLength();
  30. const char16* buf1 = s1.GetBuffer();
  31. const char16* buf2 = s2.GetBuffer();
  32. for (int i=0; i < count; i++)
  33. {
  34. if (tolower(buf1[i]) != tolower(buf2[i]))
  35. {
  36. return (buf1[i] < buf2[i] ? -1 : +1);
  37. }
  38. }
  39. return (0);
  40. }
  41. }