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

Unified Diff: pkg/compiler/lib/src/js/builder.dart

Issue 930263002: dart2js: Avoid escaping in strings if it's not necessary. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use charsets. Created 5 years, 10 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 | « no previous file | pkg/compiler/lib/src/js_backend/constant_emitter.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/js/builder.dart
diff --git a/pkg/compiler/lib/src/js/builder.dart b/pkg/compiler/lib/src/js/builder.dart
index 5d8da87f88b6f1de2f453fef6bdced2bf29cbec8..37ef7d10c01471fe955980f46d52fdf84aa72d77 100644
--- a/pkg/compiler/lib/src/js/builder.dart
+++ b/pkg/compiler/lib/src/js/builder.dart
@@ -289,20 +289,47 @@ class JsBuilder {
return new Template.withStatementResult(ast);
}
+ static RegExp _stringEscapeRegExp =
+ new RegExp(r'["\\\n\r\b\t\v' + '\u2028\u2029]');
+
/// Creates a literal js string from [value].
LiteralString escapedString(String value) {
- // Start by escaping the backslashes.
- String escaped = value.replaceAll('\\', '\\\\');
+ // Relevant sections of ECMA-262:
+ //
+ // 7.3 Line Terminators
+ // LineTerminator ::
+ // <LF>
+ // <CR>
+ // <LS>
+ // <PS>
+ //
+ // 7.8.4 String Literals
+ // StringLiteral ::
+ // " DoubleStringCharacters? "
+ // ' SingleStringCharacters? '
+ //
+ // DoubleStringCharacters ::
+ // DoubleStringCharacter DoubleStringCharacters?
+ //
+ // DoubleStringCharacter ::
+ // SourceCharacter but not one of " or \ or LineTerminator
+ // \ EscapeSequence
+ // LineContinuation
+ //
// Do not escape unicode characters and ' because they are allowed in the
// string literal anyway.
- escaped = escaped.replaceAllMapped(new RegExp('\n|"|\b|\t|\v'), (match) {
+ String escaped = value.replaceAllMapped(_stringEscapeRegExp, (Match match) {
switch (match.group(0)) {
+ case "\\" : return r"\\";
case "\n" : return r"\n";
+ case "\r" : return r"\r";
case "\"" : return r'\"';
case "\b" : return r"\b";
case "\t" : return r"\t";
case "\f" : return r"\f";
case "\v" : return r"\v";
+ case "\u2028" : return r"\u2028";
+ case "\u2029" : return r"\u2029";
}
});
LiteralString result = string(escaped);
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_backend/constant_emitter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698