CharTrie.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Copyright (c) ChakraCore Project Contributors. All rights reserved.
  4. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. //-------------------------------------------------------------------------------------------------------
  6. #pragma once
  7. namespace UnifiedRegex
  8. {
  9. // ----------------------------------------------------------------------
  10. // CharTrie
  11. // ----------------------------------------------------------------------
  12. // FORWARD
  13. struct CharTrieEntry;
  14. class CharTrie : private Chars<char16>
  15. {
  16. friend class RuntimeCharTrie;
  17. static const int initCapacity = 4;
  18. CharTrieEntry* children;
  19. bool isAccepting;
  20. int capacity;
  21. int count;
  22. // Array of capacity entries, first count are used, in increasing character order
  23. inline bool Find(Char c, int& outi);
  24. public:
  25. inline CharTrie() : isAccepting(false), capacity(0), count(0), children(0) {}
  26. inline void Reset() { isAccepting = false; capacity = 0; count = 0; children = 0; }
  27. void FreeBody(ArenaAllocator* allocator);
  28. inline int Count() const { return count; }
  29. inline bool IsAccepting() const { return isAccepting; }
  30. inline void SetAccepting() { isAccepting = true; }
  31. CharTrie* Add(ArenaAllocator* allocator, Char c);
  32. bool IsDepthZero() const;
  33. bool IsDepthOne() const;
  34. #if ENABLE_REGEX_CONFIG_OPTIONS
  35. void Print(DebugWriter* w) const;
  36. #endif
  37. };
  38. struct CharTrieEntry : private Chars<char16>
  39. {
  40. Char c;
  41. CharTrie node;
  42. };
  43. // ----------------------------------------------------------------------
  44. // RuntimeCharTrie
  45. // ----------------------------------------------------------------------
  46. // FORWARD
  47. struct RuntimeCharTrieEntry;
  48. class RuntimeCharTrie : private Chars<char16>
  49. {
  50. int count;
  51. // Array of count entries, in increasing character order
  52. RuntimeCharTrieEntry* children;
  53. public:
  54. inline RuntimeCharTrie() : count(0), children(0) {}
  55. void FreeBody(ArenaAllocator* allocator);
  56. void CloneFrom(Js::ScriptContext* scriptContext, ArenaAllocator* allocator, const CharTrie& other);
  57. bool Match
  58. ( const Char* const input
  59. , const CharCount inputLength
  60. , CharCount &inputOffset
  61. #if ENABLE_REGEX_CONFIG_OPTIONS
  62. , RegexStats* stats
  63. #endif
  64. ) const;
  65. #if ENABLE_REGEX_CONFIG_OPTIONS
  66. void Print(DebugWriter* w) const;
  67. #endif
  68. };
  69. struct RuntimeCharTrieEntry : private Chars<char16>
  70. {
  71. Char c;
  72. RuntimeCharTrie node;
  73. };
  74. }