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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/compiler/lib/src/js_backend/constant_emitter.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // Utilities for building JS ASTs at runtime. Contains a builder class 5 // Utilities for building JS ASTs at runtime. Contains a builder class
6 // and a parser that parses part of the language. 6 // and a parser that parses part of the language.
7 7
8 part of js_ast; 8 part of js_ast;
9 9
10 10
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 * context that expects a template. 282 * context that expects a template.
283 */ 283 */
284 Template expressionTemplateYielding(Node ast) { 284 Template expressionTemplateYielding(Node ast) {
285 return new Template.withExpressionResult(ast); 285 return new Template.withExpressionResult(ast);
286 } 286 }
287 287
288 Template statementTemplateYielding(Node ast) { 288 Template statementTemplateYielding(Node ast) {
289 return new Template.withStatementResult(ast); 289 return new Template.withStatementResult(ast);
290 } 290 }
291 291
292 static RegExp _stringEscapeRegExp =
293 new RegExp(r'["\\\n\r\b\t\v' + '\u2028\u2029]');
294
292 /// Creates a literal js string from [value]. 295 /// Creates a literal js string from [value].
293 LiteralString escapedString(String value) { 296 LiteralString escapedString(String value) {
294 // Start by escaping the backslashes. 297 // Relevant sections of ECMA-262:
295 String escaped = value.replaceAll('\\', '\\\\'); 298 //
299 // 7.3 Line Terminators
300 // LineTerminator ::
301 // <LF>
302 // <CR>
303 // <LS>
304 // <PS>
305 //
306 // 7.8.4 String Literals
307 // StringLiteral ::
308 // " DoubleStringCharacters? "
309 // ' SingleStringCharacters? '
310 //
311 // DoubleStringCharacters ::
312 // DoubleStringCharacter DoubleStringCharacters?
313 //
314 // DoubleStringCharacter ::
315 // SourceCharacter but not one of " or \ or LineTerminator
316 // \ EscapeSequence
317 // LineContinuation
318 //
296 // Do not escape unicode characters and ' because they are allowed in the 319 // Do not escape unicode characters and ' because they are allowed in the
297 // string literal anyway. 320 // string literal anyway.
298 escaped = escaped.replaceAllMapped(new RegExp('\n|"|\b|\t|\v'), (match) { 321 String escaped = value.replaceAllMapped(_stringEscapeRegExp, (Match match) {
299 switch (match.group(0)) { 322 switch (match.group(0)) {
323 case "\\" : return r"\\";
300 case "\n" : return r"\n"; 324 case "\n" : return r"\n";
325 case "\r" : return r"\r";
301 case "\"" : return r'\"'; 326 case "\"" : return r'\"';
302 case "\b" : return r"\b"; 327 case "\b" : return r"\b";
303 case "\t" : return r"\t"; 328 case "\t" : return r"\t";
304 case "\f" : return r"\f"; 329 case "\f" : return r"\f";
305 case "\v" : return r"\v"; 330 case "\v" : return r"\v";
331 case "\u2028" : return r"\u2028";
332 case "\u2029" : return r"\u2029";
306 } 333 }
307 }); 334 });
308 LiteralString result = string(escaped); 335 LiteralString result = string(escaped);
309 // We don't escape ' under the assumption that the string is wrapped 336 // We don't escape ' under the assumption that the string is wrapped
310 // into ". Verify that assumption. 337 // into ". Verify that assumption.
311 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0)); 338 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0));
312 return result; 339 return result;
313 } 340 }
314 341
315 /// Creates a literal js string from [value]. 342 /// Creates a literal js string from [value].
(...skipping 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after
1329 1356
1330 Catch parseCatch() { 1357 Catch parseCatch() {
1331 expectCategory(LPAREN); 1358 expectCategory(LPAREN);
1332 Declaration errorName = parseVariableDeclaration(); 1359 Declaration errorName = parseVariableDeclaration();
1333 expectCategory(RPAREN); 1360 expectCategory(RPAREN);
1334 expectCategory(LBRACE); 1361 expectCategory(LBRACE);
1335 Block body = parseBlock(); 1362 Block body = parseBlock();
1336 return new Catch(errorName, body); 1363 return new Catch(errorName, body);
1337 } 1364 }
1338 } 1365 }
OLDNEW
« 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