byte[]bytes = value.bytes;
final Matcher matcher = regex.matcher(bytes, begin, range);
boolean lastNull = false;
RubyArray result = runtime.newArray();
final Encoding enc = regex.getEncoding();
if (regex.numberOfCaptures() == 0) { // shorter path, no captures defined, no region will be returned
while ((end = matcher.search(start, range, Option.NONE)) >= 0) {
if (start == end + begin && matcher.getBegin() == matcher.getEnd()) {
if (value.realSize == 0) {
result.append(newEmptyString(runtime, getMetaClass()));
break;
} else if (lastNull) {
result.append(substr(runtime, beg, enc.length(bytes, begin + beg, range)));
beg = start - begin;
} else {
if (start == range) {
start++;
} else {
start += enc.length(bytes, start, range);
}
lastNull = true;
continue;
}
} else {
result.append(substr(beg, end - beg));
beg = matcher.getEnd();
start = begin + matcher.getEnd();
}
lastNull = false;
if (limit && lim <= ++i) break;
}
} else {
while ((end = matcher.search(start, range, Option.NONE)) >= 0) {
final Region region = matcher.getRegion();
if (start == end + begin && region.beg[0] == region.end[0]) {
if (value.realSize == 0) {
result.append(newEmptyString(runtime, getMetaClass()));
break;
} else if (lastNull) {
result.append(substr(beg, enc.length(bytes, begin + beg, range)));
beg = start - begin;
} else {
if (start == range) {
start++;
} else {
start += enc.length(bytes, start, range);
}
lastNull = true;
continue;
}
} else {