Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Truffle] Switched to Rubinius's String#scan.
Browse files Browse the repository at this point in the history
nirvdrum committed Jun 5, 2015
1 parent d59865b commit 87e4337
Showing 2 changed files with 0 additions and 94 deletions.
10 changes: 0 additions & 10 deletions spec/truffle/tags/core/string/scan_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1797,90 +1797,6 @@ private ByteList dumpCommon(RubyString string) {
}
}

@CoreMethod(names = "scan", required = 1, needsBlock = true, taintFromParameter = 0)
public abstract static class ScanNode extends YieldingCoreMethodNode {

public ScanNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public RubyBasicObject scan(RubyString string, RubyString regexpString, NotProvided block) {
final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), getByteList(regexpString), Option.DEFAULT);
return scan(string, regexp, block);
}

@Specialization
public RubyBasicObject scan(VirtualFrame frame, RubyString string, RubyString regexpString, RubyProc block) {
final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), getByteList(regexpString), Option.DEFAULT);
return scan(frame, string, regexp, block);
}

@Specialization
public RubyBasicObject scan(RubyString string, RubyRegexp regexp, NotProvided block) {
return ArrayNodes.fromObjects(getContext().getCoreLibrary().getArrayClass(), (Object[]) regexp.scan(string));
}

@Specialization
public RubyBasicObject scan(VirtualFrame frame, RubyString string, RubyRegexp regexp, RubyProc block) {
CompilerDirectives.transferToInterpreter();

// TODO (nirvdrum 12-Jan-15) Figure out a way to make this not just a complete copy & paste of RubyRegexp#scan.

final RubyContext context = getContext();

final byte[] stringBytes = getByteList(string).bytes();
final Encoding encoding = getByteList(string).getEncoding();
final Matcher matcher = regexp.getRegex().matcher(stringBytes);

int p = getByteList(string).getBegin();
int end = 0;
int range = p + getByteList(string).getRealSize();

Object lastGoodMatchData = nil();

if (regexp.getRegex().numberOfCaptures() == 0) {
while (true) {
Object matchData = regexp.matchCommon(string, false, true, matcher, p + end, range);

if (matchData == context.getCoreLibrary().getNilObject()) {
break;
}

RubyMatchData md = (RubyMatchData) matchData;
Object[] values = md.getValues();

assert values.length == 1;

yield(frame, block, values[0]);

lastGoodMatchData = matchData;
end = StringSupport.positionEndForScan(getByteList(string), matcher, encoding, p, range);
}

regexp.setThread("$~", lastGoodMatchData);
} else {
while (true) {
Object matchData = regexp.matchCommon(string, false, true, matcher, p + end, stringBytes.length);

if (matchData == context.getCoreLibrary().getNilObject()) {
break;
}

final Object[] captures = ((RubyMatchData) matchData).getCaptures();
yield(frame, block, ArrayNodes.createArray(context.getCoreLibrary().getArrayClass(), captures, captures.length));

lastGoodMatchData = matchData;
end = StringSupport.positionEndForScan(getByteList(string), matcher, encoding, p, range);
}

regexp.setThread("$~", lastGoodMatchData);
}

return string;
}
}

@CoreMethod(names = "setbyte", required = 2, raiseIfFrozenSelf = true)
@NodeChildren({
@NodeChild(type = RubyNode.class, value = "string"),

0 comments on commit 87e4337

Please sign in to comment.