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

Unified Diff: pkg/compiler/lib/src/js_backend/constant_emitter.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: Update comment. 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
Index: pkg/compiler/lib/src/js_backend/constant_emitter.dart
diff --git a/pkg/compiler/lib/src/js_backend/constant_emitter.dart b/pkg/compiler/lib/src/js_backend/constant_emitter.dart
index 2773144dbc285b195e1ca13073a83b28f6c2610b..72a527d0128f467ddd49cc3bb1c469c0c56e6042 100644
--- a/pkg/compiler/lib/src/js_backend/constant_emitter.dart
+++ b/pkg/compiler/lib/src/js_backend/constant_emitter.dart
@@ -234,6 +234,43 @@ class ConstantLiteralEmitter
}
}
+ /// Returns the escaped string for the given character [codeUnit].
+ ///
+ /// Returns `null` if the character doesn't need any escaping.
+ ///
+ /// 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
+ String _escapeChar(int codeUnit) {
+ switch (codeUnit) {
+ case $BACKSLASH: return r"\\";
+ case $DQ: return r'\"';
+ case $LF: return r'\n';
+ case $CR: return r'\r';
+ case $LS: return r'\u2028';
+ case $PS: return r'\u2029';
+ default: return null;
+ }
+ }
+
/**
* Write the contents of the quoted string to a [CodeBuffer] in
* a form that is valid as JavaScript string literal content.
@@ -241,9 +278,31 @@ class ConstantLiteralEmitter
*/
@override
jsAst.Expression visitString(StringConstantValue constant, [_]) {
- StringBuffer sb = new StringBuffer();
- writeJsonEscapedCharsOn(constant.primitiveValue.slowToString(), sb);
- return new jsAst.LiteralString('"$sb"');
+ String str = constant.primitiveValue.slowToString();
+ int i = 0;
sra1 2015/02/17 20:03:53 JsBuilder.escapedString already does this. It is n
floitsch 2015/02/20 13:55:50 Done.
+ String replacement;
+ while (i < str.length) {
+ replacement = _escapeChar(str.codeUnitAt(i++));
+ if (replacement != null) {
+ break;
+ }
+ }
+ // In the common case we don't need any escaping and will not enter the if.
+ if (replacement != null) {
+ StringBuffer sb = new StringBuffer(str.substring(0, i - 1));
+ sb.write(replacement);
+ while (i < str.length) {
+ int codeUnit = str.codeUnitAt(i++);
+ replacement = _escapeChar(codeUnit);
+ if (replacement != null) {
+ sb.write(replacement);
+ } else {
+ sb.writeCharCode(codeUnit);
+ }
+ }
+ str = sb.toString();
+ }
+ return new jsAst.LiteralString('"$str"');
}
@override
« no previous file with comments | « no previous file | tests/compiler/dart2js/string_escapes2_test.dart » ('j') | tests/compiler/dart2js/string_escapes2_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698