Skip to content

Commit

Permalink
Moved a helper method from RubyString to StringSupport.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Dec 18, 2014
1 parent 20fb9e0 commit d2a0d62
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
21 changes: 4 additions & 17 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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));
}
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]);
Expand All @@ -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()]);
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/org/jruby/util/StringSupport.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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.