private IRubyObject scan(IRubyObject regex, boolean succptr, boolean getstr, boolean headonly) {
Ruby runtime = getRuntime();
if (!(regex instanceof RubyRegexp)) throw runtime.newTypeError("wrong argument type " + regex.getMetaClass() + " (expected Regexp)");
check();
Regex pattern = ((RubyRegexp)regex).preparePattern(str);
clearMatched();
int rest = str.getByteList().getRealSize() - pos;
if (rest < 0) return getRuntime().getNil();
ByteList value = str.getByteList();
Matcher matcher = pattern.matcher(value.getUnsafeBytes(), value.getBegin() + pos, value.getBegin() + value.getRealSize());
final int ret;
if (headonly) {
ret = RubyRegexp.matcherMatch(runtime, matcher, value.getBegin() + pos, value.getBegin() + value.getRealSize(), Option.NONE);
} else {