/
character.rb
56 lines (47 loc) · 1.09 KB
/
character.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Regin
class Character < Atom
attr_reader :quantifier
def initialize(value, options = {})
@quantifier = options[:quantifier]
super
end
def option_names
%w( quantifier ) + super
end
# Returns true if expression could be treated as a literal string.
#
# A Character is literal is there is no quantifier attached to it.
def literal?
quantifier.nil? && !ignorecase
end
def to_s(parent = false)
if !parent && ignorecase
"(?i-mx:#{value})#{quantifier}"
else
"#{value}#{quantifier}"
end
end
def to_regexp(anchored = false)
re = to_s(true)
re = "\\A#{re}\\Z" if anchored
Regexp.compile(re, ignorecase)
end
def match(char)
to_regexp(true).match(char)
end
def include?(char)
if ignorecase
value.downcase == char.downcase
else
value == char
end
end
def eql?(other) #:nodoc:
super && quantifier.eql?(other.quantifier)
end
def freeze #:nodoc:
quantifier.freeze if quantifier
super
end
end
end