lib/contrast/agent/assess/policy/propagator/select.rb in contrast-agent-3.10.2 vs lib/contrast/agent/assess/policy/propagator/select.rb in contrast-agent-3.11.0

- old
+ new

@@ -10,13 +10,10 @@ # # Disclaimer: there may be a better way, but we're # in a 'get it work' state. hopefully, we'll be in # a 'get it right' state soon. class Select - include Contrast::Components::Interface - access_component :logging - class << self def select_tagger patcher, preshift, ret, _block source = preshift.object args = preshift.args @@ -47,35 +44,51 @@ ret end private + def handle_integer args, arg, source + length = args[1] || 1 + # (void) negative range + arg += source.length if arg.negative? + Contrast::Agent::Assess::AdjustedSpan.new(arg, arg + length) + end + + def handle_string arg, source + idx = source.index(arg) + Contrast::Agent::Assess::AdjustedSpan.new(idx, idx + arg.length) + end + + def handle_regexp args, arg, source + match_data = arg.match(source) + # nil has the same meaning as 0. use full match + group = args[1] || 0 + Contrast::Agent::Assess::AdjustedSpan.new(match_data.begin(group), match_data.end(group)) + end + + def handle_range arg, source + start = arg.begin + finish = arg.end + + # (void) negative range + start += source.length if start.negative? + finish += source.length if finish.negative? + finish += 1 unless arg.exclude_end? + + Contrast::Agent::Assess::AdjustedSpan.new(start, finish) + end + def determine_select_range source, args arg = args[0] case arg when Integer - length = args[1] || 1 - # (void) negative range - arg += source.length if arg.negative? - Contrast::Agent::Assess::AdjustedSpan.new(arg, arg + length) + handle_integer(args, arg, source) when String - idx = source.index(arg) - Contrast::Agent::Assess::AdjustedSpan.new(idx, idx + arg.length) + handle_string(arg, source) when Regexp - match_data = arg.match(source) - # nil has the same meaning as 0. use full match - group = args[1] || 0 - Contrast::Agent::Assess::AdjustedSpan.new(match_data.begin(group), match_data.end(group)) + handle_regexp(args, arg, source) when Range - start = arg.begin - finish = arg.end - - # (void) negative range - start += source.length if start.negative? - finish += source.length if finish.negative? - finish += 1 unless arg.exclude_end? - - Contrast::Agent::Assess::AdjustedSpan.new(start, finish) + handle_range(arg, source) end end end end end