Skip to content

Commit

Permalink
[Truffle] Don't destroy the last regular expression match when we rea…
Browse files Browse the repository at this point in the history
…ch the end of searching in String#scan.
  • Loading branch information
nirvdrum committed Jan 8, 2015
1 parent 2e55827 commit b63047d
Showing 1 changed file with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ public Object scan(RubyString string) {
int end = 0;
int range = p + string.getBytes().getRealSize();

Object lastGoodMatchData = getContext().getCoreLibrary().getNilObject();

if (regex.numberOfCaptures() == 0) {
final ArrayList<RubyString> strings = new ArrayList<>();

Expand All @@ -327,9 +329,11 @@ public Object scan(RubyString string) {

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

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

setThread("$~", lastGoodMatchData);
return strings.toArray(new RubyString[strings.size()]);
} else {
final List<RubyArray> allMatches = new ArrayList<>();
Expand All @@ -344,9 +348,11 @@ public Object scan(RubyString string) {
allMatches.add(RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(),
((RubyMatchData) matchData).getCaptures()));

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

setThread("$~", lastGoodMatchData);
return allMatches.toArray(new Object[allMatches.size()]);
}
}
Expand Down

2 comments on commit b63047d

@nirvdrum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisseaton This looks a bit ugly. If you have thoughts on how to make it cleaner, please feel free to share.

@chrisseaton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's fine.

Please sign in to comment.