Class: Regexgen::Trie
- Inherits:
-
Object
- Object
- Regexgen::Trie
- Defined in:
- lib/regexgen/trie.rb
Instance Attribute Summary collapse
-
#alphabet ⇒ Object
readonly
Returns the value of attribute alphabet.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add(str) ⇒ Object
- #add_all(strs) ⇒ Object
-
#initialize ⇒ Trie
constructor
A new instance of Trie.
- #minimize ⇒ Object
- #to_regex(flags = nil) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ Trie
Returns a new instance of Trie.
11 12 13 14 |
# File 'lib/regexgen/trie.rb', line 11 def initialize @alphabet = Set.new @root = State.new end |
Instance Attribute Details
#alphabet ⇒ Object (readonly)
Returns the value of attribute alphabet.
9 10 11 |
# File 'lib/regexgen/trie.rb', line 9 def alphabet @alphabet end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
9 10 11 |
# File 'lib/regexgen/trie.rb', line 9 def root @root end |
Instance Method Details
#add(str) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/regexgen/trie.rb', line 16 def add(str) node = @root str.each_char do |char| @alphabet.add(char) node = node.transitions[char] end node.accepting = true end |
#add_all(strs) ⇒ Object
25 26 27 |
# File 'lib/regexgen/trie.rb', line 25 def add_all(strs) strs.each(&method(:add)) end |
#minimize ⇒ Object
29 30 31 |
# File 'lib/regexgen/trie.rb', line 29 def minimize Regexgen.minimize(@root) end |
#to_regex(flags = nil) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/regexgen/trie.rb', line 37 def to_regex(flags = nil) flags_i = 0 flags_i |= Regexp::EXTENDED if flags&.include?('x') flags_i |= Regexp::IGNORECASE if flags&.include?('i') flags_i |= Regexp::MULTILINE if flags&.include?('m') Regexp.new(to_s, flags_i) end |
#to_s ⇒ Object
33 34 35 |
# File 'lib/regexgen/trie.rb', line 33 def to_s Regexgen.to_regex(minimize) end |