public IRubyObject scan(ThreadContext context, IRubyObject arg, Block block) {
Ruby runtime = context.getRuntime();
Frame frame = context.getPreviousFrame();
final RubyRegexp rubyRegex = getPattern(arg, true);
final Regex regex = rubyRegex.getPattern();
int range = value.begin + value.realSize;
final Matcher matcher = regex.matcher(value.bytes, value.begin, range);
matcher.value = 0; // implicit start argument to scanOnce(NG)
IRubyObject result;
if (!block.isGiven()) {
RubyArray ary = runtime.newArray();
if (regex.numberOfCaptures() == 0) {
while ((result = scanOnceNG(rubyRegex, matcher, range)) != null) ary.append(result);
} else {
while ((result = scanOnce(rubyRegex, matcher, range)) != null) ary.append(result);
}
if (ary.size() > 0) {
rubyRegex.updateBackRef(context, this, frame, matcher);
} else {
frame.setBackRef(runtime.getNil());
}
return ary;
} else {
byte[]bytes = value.bytes;
int size = value.realSize;
RubyMatchData match = null;
if (regex.numberOfCaptures() == 0) {
while ((result = scanOnceNG(rubyRegex, matcher, range)) != null) {
match = rubyRegex.updateBackRef(context, this, frame, matcher);
match.use();
block.yield(context, result);
modifyCheck(bytes, size);