Skip to content
Permalink
Browse files
Moved a helper method from RubyString to StringSupport.
  • Loading branch information
nirvdrum committed Dec 18, 2014
1 parent 20fb9e0 commit d2a0d62a78e4c4bba96eaf5a71e59257754c6cef
@@ -4140,19 +4140,6 @@ public IRubyObject scan(ThreadContext context, IRubyObject arg, Block block) {
return scan19(context, arg, block);
}

public static int positionEnd(ByteList value, Matcher matcher, Encoding enc, int begin, int range) {
int end = matcher.getEnd();
if (matcher.getBegin() == end) {
if (value.getRealSize() > end) {
return end + enc.length(value.getUnsafeBytes(), begin + end, range);
} else {
return end + 1;
}
} else {
return end;
}
}

private IRubyObject populateCapturesForScan(Ruby runtime, Matcher matcher, int range, int tuFlags, boolean is19) {
Region region = matcher.getRegion();
RubyArray result = getRuntime().newArray(region.numRegs);
@@ -4207,7 +4194,7 @@ private IRubyObject scanIter19(ThreadContext context, Regex pattern, Regex prepa
RubyMatchData match = null;
if (pattern.numberOfCaptures() == 0) {
while (RubyRegexp.matcherSearch(runtime, matcher, begin + end, range, Option.NONE) >= 0) {
end = positionEnd(value, matcher, enc, begin, range);
end = StringSupport.positionEndForScan(value, matcher, enc, begin, range);
match = RubyRegexp.createMatchData19(context, this, matcher, pattern);
match.regexp = regexp;
RubyString substr = makeShared19(runtime, matcher.getBegin(), matcher.getEnd() - matcher.getBegin());
@@ -4219,7 +4206,7 @@ private IRubyObject scanIter19(ThreadContext context, Regex pattern, Regex prepa
}
} else {
while (RubyRegexp.matcherSearch(runtime, matcher, begin + end, range, Option.NONE) >= 0) {
end = positionEnd(value, matcher, enc, begin, range);
end = StringSupport.positionEndForScan(value, matcher, enc, begin, range);
match = RubyRegexp.createMatchData19(context, this, matcher, pattern);
match.regexp = regexp;
match.infectBy(tuFlags);
@@ -4244,14 +4231,14 @@ private IRubyObject scanNoIter19(ThreadContext context, Regex pattern, Regex pre
int end = 0;
if (pattern.numberOfCaptures() == 0) {
while (RubyRegexp.matcherSearch(runtime, matcher, begin + end, range, Option.NONE) >= 0) {
end = positionEnd(value, matcher, enc, begin, range);
end = StringSupport.positionEndForScan(value, matcher, enc, begin, range);
RubyString substr = makeShared19(runtime, matcher.getBegin(), matcher.getEnd() - matcher.getBegin());
substr.infectBy(tuFlags);
ary.append(substr);
}
} else {
while (RubyRegexp.matcherSearch(runtime, matcher, begin + end, range, Option.NONE) >= 0) {
end = positionEnd(value, matcher, enc, begin, range);
end = StringSupport.positionEndForScan(value, matcher, enc, begin, range);
ary.append(populateCapturesForScan(runtime, matcher, range, tuFlags, true));
}
}
@@ -23,6 +23,7 @@
import org.jruby.truffle.runtime.RubyArguments;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
@@ -362,7 +363,7 @@ public Object scan(RubyString string) {

strings.add((RubyString) values[0]);

end = org.jruby.RubyString.positionEnd(string.getBytes(), matcher, encoding, p, range);
end = StringSupport.positionEndForScan(string.getBytes(), matcher, encoding, p, range);
}

return strings.toArray(new RubyString[strings.size()]);
@@ -388,7 +389,7 @@ public Object scan(RubyString string) {
RubyString[] matches = parts.toArray(new RubyString[parts.size()]);
strings.add(RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), matches));

end = org.jruby.RubyString.positionEnd(string.getBytes(), matcher, encoding, p, range);
end = StringSupport.positionEndForScan(string.getBytes(), matcher, encoding, p, range);
}

return strings.toArray(new Object[strings.size()]);
@@ -30,6 +30,7 @@
import org.jcodings.Encoding;
import org.jcodings.ascii.AsciiTables;
import org.jcodings.specific.ASCIIEncoding;
import org.joni.Matcher;
import org.jruby.Ruby;
import org.jruby.RubyObject;
import org.jruby.RubyString;
@@ -662,4 +663,17 @@ public static void TERM_FILL(byte[] ptrBytes, int ptr, int termlen) {
if (term_fill_len > 1)
Arrays.fill(ptrBytes, term_fill_ptr, term_fill_len, (byte)0);
}

public static int positionEndForScan(ByteList value, Matcher matcher, Encoding enc, int begin, int range) {
int end = matcher.getEnd();
if (matcher.getBegin() == end) {
if (value.getRealSize() > end) {
return end + enc.length(value.getUnsafeBytes(), begin + end, range);
} else {
return end + 1;
}
} else {
return end;
}
}
}

0 comments on commit d2a0d62

Please sign in to comment.