Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Unified Diff: dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart

Issue 11973018: Improve decoding of JS TypeError. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
diff --git a/dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart b/dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
index 6e4dccfb2b057aceb67c705d27ef2c06a2dfab45..58a6267b0b938ae2effe5c78a93391f4722e6de1 100644
--- a/dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
+++ b/dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart
@@ -77,7 +77,9 @@ stringReplaceJS(receiver, replacer, to) {
return JS('String', r'#.replace(#, #)', receiver, replacer, to);
}
-final RegExp quoteRegExp = new JSSyntaxRegExp(r'[-[\]{}()*+?.,\\^$|#\s]');
+const String ESCAPE_REGEXP = r'[-[\]{}()*+?.,\\^$|]';
+
+final RegExp quoteRegExp = new JSSyntaxRegExp(ESCAPE_REGEXP);
stringReplaceAllUnchecked(receiver, from, to) {
checkString(to);
@@ -86,14 +88,12 @@ stringReplaceAllUnchecked(receiver, from, to) {
if (receiver == "") {
return to;
} else {
- StringBuffer result = new StringBuffer();
+ String result = to;
int length = receiver.length;
- result.add(to);
for (int i = 0; i < length; i++) {
- result.add(receiver[i]);
- result.add(to);
+ result = '$result${receiver[i]}$to';
}
- return result.toString();
+ return result;
}
} else {
var quoter = regExpMakeNative(quoteRegExp, global: true);
@@ -125,25 +125,24 @@ stringReplaceAllFuncUnchecked(receiver, pattern, onMatch, onNonMatch) {
return stringReplaceAllStringFuncUnchecked(receiver, pattern,
onMatch, onNonMatch);
}
- StringBuffer buffer = new StringBuffer();
+ String result = '';
int startIndex = 0;
for (Match match in pattern.allMatches(receiver)) {
- buffer.add(onNonMatch(receiver.substring(startIndex, match.start)));
- buffer.add(onMatch(match));
- startIndex = match.end;
+ result =
+ '$result'
+ '${onNonMatch(receiver.substring(startIndex, match.start))}'
+ '${onMatch(match)}';
}
- buffer.add(onNonMatch(receiver.substring(startIndex)));
- return buffer.toString();
+ return '$result${onNonMatch(receiver.substring(startIndex))}';
}
stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch) {
// Pattern is the empty string.
- StringBuffer buffer = new StringBuffer();
int length = receiver.length;
int i = 0;
- buffer.add(onNonMatch(""));
+ String result = '${onNonMatch("")}';
while (i < length) {
- buffer.add(onMatch(new StringMatch(i, receiver, "")));
+ result = '$result${onMatch(new StringMatch(i, receiver, ""))}';
// Special case to avoid splitting a surrogate pair.
int code = receiver.charCodeAt(i);
if ((code & ~0x3FF) == 0xD800 && length > i + 1) {
@@ -151,17 +150,17 @@ stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch) {
code = receiver.charCodeAt(i + 1);
if ((code & ~0x3FF) == 0xDC00) {
// Matching trailing surrogate.
- buffer.add(onNonMatch(receiver.substring(i, i + 2)));
+ result = '$result${onNonMatch(receiver.substring(i, i + 2))}';
i += 2;
continue;
}
}
- buffer.add(onNonMatch(receiver[i]));
+ result = '$result${onNonMatch(receiver[i])}';
i++;
}
- buffer.add(onMatch(new StringMatch(i, receiver, "")));
- buffer.add(onNonMatch(""));
- return buffer.toString();
+ return
+ '$result${onMatch(new StringMatch(i, receiver, ""))}'
+ '${onNonMatch("")}';
}
stringReplaceAllStringFuncUnchecked(receiver, pattern, onMatch, onNonMatch) {
@@ -170,19 +169,19 @@ stringReplaceAllStringFuncUnchecked(receiver, pattern, onMatch, onNonMatch) {
return stringReplaceAllEmptyFuncUnchecked(receiver, onMatch, onNonMatch);
}
int length = receiver.length;
- StringBuffer buffer = new StringBuffer();
+ String result = '';
int startIndex = 0;
while (startIndex < length) {
int position = receiver.indexOf(pattern, startIndex);
if (position == -1) {
break;
}
- buffer.add(onNonMatch(receiver.substring(startIndex, position)));
- buffer.add(onMatch(new StringMatch(position, receiver, pattern)));
+ result =
+ '$result${onNonMatch(receiver.substring(startIndex, position))}'
+ '${onMatch(new StringMatch(position, receiver, pattern))}';
startIndex = position + patternLength;
}
- buffer.add(onNonMatch(receiver.substring(startIndex)));
- return buffer.toString();
+ return '$result${onNonMatch(receiver.substring(startIndex))}';
}

Powered by Google App Engine
This is Rietveld 408576698