Early exit on VM String.replaceAll

Change-Id: I2578200444935d221fab7f9fb4d515a9b2e75937
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/97145
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Stephen Adams 2019-03-18 22:04:10 +00:00 committed by commit-bot@chromium.org
parent 527b10879e
commit eda9ccb744

View file

@ -628,10 +628,13 @@ abstract class _StringBase implements String {
String replaceAll(Pattern pattern, String replacement) {
if (pattern == null) throw new ArgumentError.notNull("pattern");
if (replacement == null) throw new ArgumentError.notNull("replacement");
List matches = [];
int length = 0;
int replacementLength = replacement.length;
int startIndex = 0;
// String fragments that replace the the prefix [this] up to [startIndex].
List matches = [];
int length = 0; // Length of all fragments.
int replacementLength = replacement.length;
if (replacementLength == 0) {
for (Match match in pattern.allMatches(this)) {
length += _addReplaceSlice(matches, startIndex, match.start);
@ -645,6 +648,8 @@ abstract class _StringBase implements String {
startIndex = match.end;
}
}
// No match, or a zero-length match at start with zero-length replacement.
if (startIndex == 0 && length == 0) return this;
length += _addReplaceSlice(matches, startIndex, this.length);
bool replacementIsOneByte = replacement._isOneByte;
if (replacementIsOneByte &&