Class: Packr::RegexpGroup
Direct Known Subclasses
Parser
Defined Under Namespace
Classes: Item
Constant Summary
collapse
- IGNORE =
"\\0"
- BACK_REF =
/\\(\d+)/
- ESCAPE_CHARS =
/\\./
- ESCAPE_BRACKETS =
/\(\?[:=!]|\[[^\]]+\]/
- BRACKETS =
/\(/
- LOOKUP =
/\\(\d+)/
- LOOKUP_SIMPLE =
/^\\\d+$/
Instance Attribute Summary
Attributes inherited from Collection
#keys, #values
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Collection
#add, #clear, #copy, create, #each, #get_at, #get_keys, #index_of, #item, #map, #merge, #put, #put_at, #remove, #remove_at, #reverse!, #size, #slice, #sort!
Methods inherited from Map
#clear, #copy, #each, #get, #get_keys, #get_values, #has?, #merge, #put, #remove, #size, #union
Constructor Details
#initialize(values = nil, ignore_case = false) ⇒ RegexpGroup
Returns a new instance of RegexpGroup.
12
13
14
15
|
# File 'lib/packr/regexp_group.rb', line 12
def initialize(values = nil, ignore_case = false)
super(values)
@ignore_case = !!ignore_case
end
|
Class Method Details
.count(expression) ⇒ Object
114
115
116
117
118
|
# File 'lib/packr/regexp_group.rb', line 114
def self.count(expression)
expression = expression.to_s.gsub(ESCAPE_CHARS, "").gsub(ESCAPE_BRACKETS, "")
expression.scan(BRACKETS).length
end
|
Instance Method Details
#exec(string, override = nil) ⇒ Object
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
|
# File 'lib/packr/regexp_group.rb', line 17
def exec(string, override = nil)
string = string.to_s return string if @keys.empty?
override = 0 if override == IGNORE
string.gsub(Regexp.new(self.to_s, @ignore_case && Regexp::IGNORECASE)) do |match|
offset, i, result = 1, 0, match
arguments = [match] + $~.captures + [$~.begin(0), string]
each do |item, key|
nxt = offset + item.length + 1
if arguments[offset] replacement = override.nil? ? item.replacement : override
case replacement
when Proc
result = replacement.call(*arguments[offset...nxt])
when Numeric
result = arguments[offset + replacement]
else
result = replacement
end
end
offset = nxt
end
result
end
end
|
#insert_at(index, expression, replacement) ⇒ Object
44
45
46
47
|
# File 'lib/packr/regexp_group.rb', line 44
def insert_at(index, expression, replacement)
expression = expression.is_a?(Regexp) ? expression.source : expression.to_s
super(index, expression, replacement)
end
|
#test(string) ⇒ Object
49
50
51
|
# File 'lib/packr/regexp_group.rb', line 49
def test(string)
exec(string) != string
end
|
#to_s ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'lib/packr/regexp_group.rb', line 53
def to_s
offset = 1
"(" + map { |item, key|
expression = item.to_s.gsub(BACK_REF) { |m| "\\" + (offset + $1.to_i) }
offset += item.length + 1
expression
}.join(")|(") + ")"
end
|