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

Unified Diff: sdk/lib/_internal/js_runtime/lib/string_helper.dart

Issue 1557673002: js_runtime: streamline stringReplaceAllUnchecked (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: fix Created 4 years, 12 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
« no previous file with comments | « sdk/lib/_internal/js_runtime/lib/js_helper.dart ('k') | tests/corelib/string_replace_all_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/js_runtime/lib/string_helper.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/string_helper.dart b/sdk/lib/_internal/js_runtime/lib/string_helper.dart
index 9ce91f8a179d7505935707a47ea45e8fef2c2f6d..24e21f7fbd02c88baee8badfbe3d56838a5863fc 100644
--- a/sdk/lib/_internal/js_runtime/lib/string_helper.dart
+++ b/sdk/lib/_internal/js_runtime/lib/string_helper.dart
@@ -130,7 +130,12 @@ stringReplaceFirstRE(receiver, regexp, replacement, startIndex) {
return stringReplaceRangeUnchecked(receiver, start, end, replacement);
}
-const String ESCAPE_REGEXP = r'[[\]{}()*+?.\\^$|]';
+
+/// Returns a string for a RegExp pattern that matches [string]. This is done by
+/// escaping all RegExp metacharacters.
+quoteStringForRegExp(string) {
+ return JS('String', r'#.replace(/[[\]{}()*+?.\\^$|]/g, "\\$&")', string);
+}
stringReplaceAllUnchecked(receiver, pattern, replacement) {
checkString(replacement);
@@ -149,8 +154,7 @@ stringReplaceAllUnchecked(receiver, pattern, replacement) {
return result.toString();
}
} else {
- var quoter = JS('', "new RegExp(#, 'g')", ESCAPE_REGEXP);
- var quoted = JS('String', r'#.replace(#, "\\$&")', pattern, quoter);
+ var quoted = quoteStringForRegExp(pattern);
var replacer = JS('', "new RegExp(#, 'g')", quoted);
return stringReplaceJS(receiver, replacer, replacement);
}
« no previous file with comments | « sdk/lib/_internal/js_runtime/lib/js_helper.dart ('k') | tests/corelib/string_replace_all_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698