2
0

RegexKey.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Field(const char16 *) source;
  13. Field(int) length;
  14. Field(RegexFlags) flags;
  15. public:
  16. RegexKey() : source(nullptr), length(0), flags(static_cast<RegexFlags>(0))
  17. {
  18. }
  19. RegexKey(const char16 *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(const RegexKey& other)
  26. : source(other.source), length(other.length), flags(other.flags)
  27. {}
  28. RegexKey &operator =(const void *const nullValue)
  29. {
  30. // Needed to support KeyValueEntry::Clear for dictionaries
  31. Assert(!nullValue);
  32. source = nullptr;
  33. length = 0;
  34. flags = static_cast<RegexFlags>(0);
  35. return *this;
  36. }
  37. const char16 *Source() const
  38. {
  39. return source;
  40. }
  41. int Length() const
  42. {
  43. return length;
  44. }
  45. RegexFlags Flags() const
  46. {
  47. return flags;
  48. }
  49. };
  50. struct RegexKeyComparer
  51. {
  52. inline static bool Equals(const RegexKey &key1, const RegexKey &key2)
  53. {
  54. return
  55. Js::InternalStringComparer::Equals(
  56. Js::InternalString(key1.Source(), key1.Length()),
  57. Js::InternalString(key2.Source(), key2.Length())) &&
  58. key1.Flags() == key2.Flags();
  59. }
  60. inline static hash_t GetHashCode(const RegexKey &key)
  61. {
  62. return Js::InternalStringComparer::GetHashCode(Js::InternalString(key.Source(), key.Length()));
  63. }
  64. };
  65. }
  66. template<>
  67. struct DefaultComparer<UnifiedRegex::RegexKey> : public UnifiedRegex::RegexKeyComparer
  68. {
  69. };