lib/regexp-examples/groups.rb in regexp-examples-0.3.1 vs lib/regexp-examples/groups.rb in regexp-examples-0.3.2
- old
+ new
@@ -51,24 +51,31 @@
# save first and last "-" if present
first = nil
last = nil
first = @chars.shift if @chars.first == "-"
last = @chars.pop if @chars.last == "-"
- # Replace all instances of e.g. ["a" "-" "z"] with ["a", "b", ..., "z"]
+ # Replace all instances of e.g. ["a", "-", "z"] with ["a", "b", ..., "z"]
while i = @chars.index("-")
- @chars[i-1..i+1] = (@chars[i-1]..@chars[i+1]).to_a
+ # Prevent infinite loops from expanding [",", "-", "."] to itself
+ # (Since ",".ord = 44, "-".ord = 45, ".".ord = 46)
+ if (@chars[i-1] == ',' && @chars[i+1] == '.')
+ first = '-'
+ @chars.delete_at(i)
+ else
+ @chars[i-1..i+1] = (@chars[i-1]..@chars[i+1]).to_a
+ end
end
# restore them back
@chars.unshift(first) if first
@chars.push(last) if last
end
def init_backslash_chars
@chars.each_with_index do |char, i|
if char == "\\"
if BackslashCharMap.keys.include?(@chars[i+1])
- @chars[i..i+1] = BackslashCharMap[@chars[i+1]]
+ @chars[i..i+1] = move_backslash_to_front( BackslashCharMap[@chars[i+1]] )
elsif @chars[i+1] == 'b'
@chars[i..i+1] = "\b"
elsif @chars[i+1] == "\\"
@chars.delete_at(i+1)
else
@@ -81,10 +88,18 @@
def result
(@negative ? (CharSets::Any - @chars) : @chars).map do |result|
GroupResult.new(result)
end
end
+
+ private
+ def move_backslash_to_front(chars)
+ if index = chars.index { |char| char == '\\' }
+ chars.unshift chars.delete_at(index)
+ end
+ chars
+ end
end
class DotGroup
def result
CharSets::Any.map do |result|
@@ -118,16 +133,13 @@
def initialize(left_repeaters, right_repeaters)
@left_repeaters = left_repeaters
@right_repeaters = right_repeaters
end
+
def result
- left_result = @left_repeaters.map do |repeater|
- RegexpExamples::permutations_of_strings([repeater.result])
- end
- right_result = @right_repeaters.map do |repeater|
- RegexpExamples::permutations_of_strings([repeater.result])
- end
+ left_result = RegexpExamples::map_results(@left_repeaters)
+ right_result = RegexpExamples::map_results(@right_repeaters)
left_result.concat(right_result).flatten.uniq.map do |result|
GroupResult.new(result)
end
end
end