Skip to content

Commit

Permalink
[Truffle] Adding range support to MatchData#[].
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Apr 2, 2015
1 parent cad1c3c commit 53cf12a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
@@ -1,2 +1 @@
fails:MatchData#[] supports accessors [start, length]
fails:MatchData#[] supports ranges [start..end]
Expand Up @@ -25,10 +25,13 @@
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyMatchData;
import org.jruby.truffle.runtime.core.RubyRange;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.core.RubySymbol;
import org.jruby.util.ByteList;

import java.util.Arrays;

@CoreClass(name = "MatchData")
public abstract class MatchDataNodes {

Expand Down Expand Up @@ -92,7 +95,7 @@ public Object getIndex(RubyMatchData matchData, RubyString index) {
}
}

@Specialization(guards = { "!isRubySymbol(arguments[1])", "!isRubyString(arguments[1])" })
@Specialization(guards = {"!isRubySymbol(arguments[1])", "!isRubyString(arguments[1])", "!isIntegerFixnumRange(arguments[1])"})
public Object getIndex(VirtualFrame frame, RubyMatchData matchData, Object index) {
notDesignedForCompilation();

Expand All @@ -104,6 +107,18 @@ public Object getIndex(VirtualFrame frame, RubyMatchData matchData, Object index
return getIndex(matchData, toIntNode.executeIntegerFixnum(frame, index));
}

@Specialization(guards = {"!isRubySymbol(arguments[1])", "!isRubyString(arguments[1])"})
public Object getIndex(VirtualFrame frame, RubyMatchData matchData, RubyRange.IntegerFixnumRange range) {
final Object[] values = matchData.getValues();
final int normalizedIndex = RubyArray.normalizeIndex(values.length, range.getBegin());
final int end = RubyArray.normalizeIndex(values.length, range.getEnd());
final int exclusiveEnd = RubyArray.clampExclusiveIndex(values.length, range.doesExcludeEnd() ? end : end + 1);
final int length = exclusiveEnd - normalizedIndex;

final Object[] store = Arrays.copyOfRange(values, normalizedIndex, normalizedIndex + length);
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), store, length);
}

}

@CoreMethod(names = "begin", required = 1, lowerFixnumParameters = 1)
Expand Down

0 comments on commit 53cf12a

Please sign in to comment.