CharTrie.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // ----------------------------------------------------------------------
  9. // CharTrie
  10. // ----------------------------------------------------------------------
  11. // FORWARD
  12. struct CharTrieEntry;
  13. class CharTrie : private Chars<char16>
  14. {
  15. friend class RuntimeCharTrie;
  16. static const int initCapacity = 4;
  17. CharTrieEntry* children;
  18. bool isAccepting;
  19. int capacity;
  20. int count;
  21. // Array of capacity entries, first count are used, in increasing character order
  22. inline bool Find(Char c, int& outi);
  23. public:
  24. inline CharTrie() : isAccepting(false), capacity(0), count(0), children(0) {}
  25. inline void Reset() { isAccepting = false; capacity = 0; count = 0; children = 0; }
  26. void FreeBody(ArenaAllocator* allocator);
  27. inline int Count() const { return count; }
  28. inline bool IsAccepting() const { return isAccepting; }
  29. inline void SetAccepting() { isAccepting = true; }
  30. CharTrie* Add(ArenaAllocator* allocator, Char c);
  31. bool IsDepthZero() const;
  32. bool IsDepthOne() const;
  33. #if ENABLE_REGEX_CONFIG_OPTIONS
  34. void Print(DebugWriter* w) const;
  35. #endif
  36. };
  37. struct CharTrieEntry : private Chars<char16>
  38. {
  39. Char c;
  40. CharTrie node;
  41. };
  42. // ----------------------------------------------------------------------
  43. // RuntimeCharTrie
  44. // ----------------------------------------------------------------------
  45. // FORWARD
  46. struct RuntimeCharTrieEntry;
  47. class RuntimeCharTrie : private Chars<char16>
  48. {
  49. int count;
  50. // Array of count entries, in increasing character order
  51. RuntimeCharTrieEntry* children;
  52. public:
  53. inline RuntimeCharTrie() : count(0), children(0) {}
  54. void FreeBody(ArenaAllocator* allocator);
  55. void CloneFrom(ArenaAllocator* allocator, const CharTrie& other);
  56. bool Match
  57. ( const Char* const input
  58. , const CharCount inputLength
  59. , CharCount &inputOffset
  60. #if ENABLE_REGEX_CONFIG_OPTIONS
  61. , RegexStats* stats
  62. #endif
  63. ) const;
  64. #if ENABLE_REGEX_CONFIG_OPTIONS
  65. void Print(DebugWriter* w) const;
  66. #endif
  67. };
  68. struct RuntimeCharTrieEntry : private Chars<char16>
  69. {
  70. Char c;
  71. RuntimeCharTrie node;
  72. };
  73. }