Class: Regexgen::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/regexgen/trie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrie

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

#alphabetObject (readonly)

Returns the value of attribute alphabet.



9
10
11
# File 'lib/regexgen/trie.rb', line 9

def alphabet
  @alphabet
end

#rootObject (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

#minimizeObject



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_sObject



33
34
35
# File 'lib/regexgen/trie.rb', line 33

def to_s
  Regexgen.to_regex(minimize)
end