RegexKey.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #pragma once
  6. namespace UnifiedRegex
  7. {
  8. enum RegexFlags : uint8;
  9. class RegexKey
  10. {
  11. private:
  12. const wchar_t *source;
  13. int length;
  14. RegexFlags flags;
  15. public:
  16. RegexKey() : source(nullptr), length(0), flags(static_cast<RegexFlags>(0))
  17. {
  18. }
  19. RegexKey(const wchar_t *const source, const int length, const RegexFlags flags)
  20. : source(source), length(length), flags(flags)
  21. {
  22. Assert(source);
  23. Assert(length >= 0);
  24. }
  25. RegexKey &operator =(const void *const nullValue)
  26. {
  27. // Needed to support KeyValueEntry::Clear for dictionaries
  28. Assert(!nullValue);
  29. source = nullptr;
  30. length = 0;
  31. flags = static_cast<RegexFlags>(0);
  32. return *this;
  33. }
  34. const wchar_t *Source() const
  35. {
  36. return source;
  37. }
  38. int Length() const
  39. {
  40. return length;
  41. }
  42. RegexFlags Flags() const
  43. {
  44. return flags;
  45. }
  46. };
  47. struct RegexKeyComparer
  48. {
  49. __inline static bool Equals(const RegexKey &key1, const RegexKey &key2)
  50. {
  51. return
  52. Js::InternalStringComparer::Equals(
  53. Js::InternalString(key1.Source(), key1.Length()),
  54. Js::InternalString(key2.Source(), key2.Length())) &&
  55. key1.Flags() == key2.Flags();
  56. }
  57. __inline static hash_t GetHashCode(const RegexKey &key)
  58. {
  59. return Js::InternalStringComparer::GetHashCode(Js::InternalString(key.Source(), key.Length()));
  60. }
  61. };
  62. }
  63. template<>
  64. struct DefaultComparer<UnifiedRegex::RegexKey> : public UnifiedRegex::RegexKeyComparer
  65. {
  66. };