lib/regexp-examples/groups.rb in regexp-examples-0.4.2 vs lib/regexp-examples/groups.rb in regexp-examples-0.5.0
- old
+ new
@@ -14,24 +14,47 @@
end
def all_subgroups
[self, subgroups].flatten.reject { |subgroup| subgroup.group_id.nil? }
end
+
+ def swapcase
+ # Override to preserve subgroups
+ GroupResult.new(super.to_s, group_id, subgroups)
+ end
end
+ module GroupWithOptions
+ attr_reader :options
+ def result
+ group_result = super
+ if options[:ignorecase]
+ group_result
+ .concat( group_result.map(&:swapcase) )
+ .uniq
+ else
+ group_result
+ end
+ end
+ end
+
class SingleCharGroup
- def initialize(char)
+ prepend GroupWithOptions
+ def initialize(char, options)
@char = char
+ @options = options
end
def result
[GroupResult.new(@char)]
end
end
class CharGroup
- def initialize(chars)
+ prepend GroupWithOptions
+ def initialize(chars, options)
@chars = chars
+ @options = options
if chars[0] == "^"
@negative = true
@chars = @chars[1..-1]
else
@negative = false
@@ -41,10 +64,11 @@
init_ranges
end
def init_ranges
# 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"]
@@ -93,21 +117,28 @@
chars
end
end
class DotGroup
+ prepend GroupWithOptions
+ def initialize(options={})
+ @options = options
+ end
+
def result
CharSets::Any.map do |result|
GroupResult.new(result)
end
end
end
class MultiGroup
+ prepend GroupWithOptions
attr_reader :group_id
- def initialize(groups, group_id)
+ def initialize(groups, group_id, options)
@groups = groups
@group_id = group_id
+ @options = options
end
# Generates the result of each contained group
# and adds the filled group of each result to
# itself