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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of js_backend; 5 part of js_backend;
6 6
7 class ConstantEmitter { 7 class ConstantEmitter {
8 ConstantReferenceEmitter _referenceEmitter; 8 ConstantReferenceEmitter _referenceEmitter;
9 ConstantLiteralEmitter _literalEmitter; 9 ConstantLiteralEmitter _literalEmitter;
10 10
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 return js("!0"); 227 return js("!0");
228 } else { 228 } else {
229 // Use !1 for false. 229 // Use !1 for false.
230 return js("!1"); 230 return js("!1");
231 } 231 }
232 } else { 232 } else {
233 return constant.isTrue ? js('true') : js('false'); 233 return constant.isTrue ? js('true') : js('false');
234 } 234 }
235 } 235 }
236 236
237 /// Returns the escaped string for the given character [codeUnit].
238 ///
239 /// Returns `null` if the character doesn't need any escaping.
240 ///
241 /// Relevant sections of ECMA-262:
242 ///
243 /// 7.3 Line Terminators
244 /// LineTerminator ::
245 /// <LF>
246 /// <CR>
247 /// <LS>
248 /// <PS>
249 ///
250 /// 7.8.4 String Literals
251 /// StringLiteral ::
252 /// " DoubleStringCharacters? "
253 /// ' SingleStringCharacters? '
254 ///
255 /// DoubleStringCharacters ::
256 /// DoubleStringCharacter DoubleStringCharacters?
257 ///
258 /// DoubleStringCharacter ::
259 /// SourceCharacter but not one of " or \ or LineTerminator
260 /// \ EscapeSequence
261 /// LineContinuation
262 String _escapeChar(int codeUnit) {
263 switch (codeUnit) {
264 case $BACKSLASH: return r"\\";
265 case $DQ: return r'\"';
266 case $LF: return r'\n';
267 case $CR: return r'\r';
268 case $LS: return r'\u2028';
269 case $PS: return r'\u2029';
270 default: return null;
271 }
272 }
273
237 /** 274 /**
238 * Write the contents of the quoted string to a [CodeBuffer] in 275 * Write the contents of the quoted string to a [CodeBuffer] in
239 * a form that is valid as JavaScript string literal content. 276 * a form that is valid as JavaScript string literal content.
240 * The string is assumed quoted by double quote characters. 277 * The string is assumed quoted by double quote characters.
241 */ 278 */
242 @override 279 @override
243 jsAst.Expression visitString(StringConstantValue constant, [_]) { 280 jsAst.Expression visitString(StringConstantValue constant, [_]) {
244 StringBuffer sb = new StringBuffer(); 281 String str = constant.primitiveValue.slowToString();
245 writeJsonEscapedCharsOn(constant.primitiveValue.slowToString(), sb); 282 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.
246 return new jsAst.LiteralString('"$sb"'); 283 String replacement;
284 while (i < str.length) {
285 replacement = _escapeChar(str.codeUnitAt(i++));
286 if (replacement != null) {
287 break;
288 }
289 }
290 // In the common case we don't need any escaping and will not enter the if.
291 if (replacement != null) {
292 StringBuffer sb = new StringBuffer(str.substring(0, i - 1));
293 sb.write(replacement);
294 while (i < str.length) {
295 int codeUnit = str.codeUnitAt(i++);
296 replacement = _escapeChar(codeUnit);
297 if (replacement != null) {
298 sb.write(replacement);
299 } else {
300 sb.writeCharCode(codeUnit);
301 }
302 }
303 str = sb.toString();
304 }
305 return new jsAst.LiteralString('"$str"');
247 } 306 }
248 307
249 @override 308 @override
250 jsAst.Expression visitList(ListConstantValue constant, [_]) { 309 jsAst.Expression visitList(ListConstantValue constant, [_]) {
251 List<jsAst.Expression> elements = _array(constant.entries); 310 List<jsAst.Expression> elements = _array(constant.entries);
252 jsAst.ArrayInitializer array = new jsAst.ArrayInitializer(elements); 311 jsAst.ArrayInitializer array = new jsAst.ArrayInitializer(elements);
253 jsAst.Expression value = makeConstantListTemplate.instantiate([array]); 312 jsAst.Expression value = makeConstantListTemplate.instantiate([array]);
254 return maybeAddTypeArguments(constant.type, value); 313 return maybeAddTypeArguments(constant.type, value);
255 } 314 }
256 315
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 [value, argumentList]); 457 [value, argumentList]);
399 } 458 }
400 return value; 459 return value;
401 } 460 }
402 461
403 @override 462 @override
404 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { 463 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) {
405 return constantEmitter.reference(constant.referenced); 464 return constantEmitter.reference(constant.referenced);
406 } 465 }
407 } 466 }
OLDNEW
« 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