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

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: Fix bad semicolon. 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 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 return new Template.withExpressionResult(ast); 290 return new Template.withExpressionResult(ast);
291 } 291 }
292 292
293 Template statementTemplateYielding(Node ast) { 293 Template statementTemplateYielding(Node ast) {
294 return new Template.withStatementResult(ast); 294 return new Template.withStatementResult(ast);
295 } 295 }
296 296
297 /// Creates a literal js string from [value]. 297 /// Creates a literal js string from [value].
298 LiteralString escapedString(String value) { 298 LiteralString escapedString(String value) {
299 // Start by escaping the backslashes. 299 // Start by escaping the backslashes.
300 String escaped = value.replaceAll('\\', '\\\\'); 300 String escaped = value.replaceAll('\\', '\\\\');
Lasse Reichstein Nielsen 2015/02/23 13:25:05 Why not combine the backslash-escaping in the othe
floitsch 2015/02/23 16:56:41 Done.
301 // Relevant sections of ECMA-262:
302 //
303 // 7.3 Line Terminators
304 // LineTerminator ::
305 // <LF>
306 // <CR>
307 // <LS>
308 // <PS>
309 //
310 // 7.8.4 String Literals
311 // StringLiteral ::
312 // " DoubleStringCharacters? "
313 // ' SingleStringCharacters? '
314 //
315 // DoubleStringCharacters ::
316 // DoubleStringCharacter DoubleStringCharacters?
317 //
318 // DoubleStringCharacter ::
319 // SourceCharacter but not one of " or \ or LineTerminator
320 // \ EscapeSequence
321 // LineContinuation
322 //
301 // Do not escape unicode characters and ' because they are allowed in the 323 // Do not escape unicode characters and ' because they are allowed in the
302 // string literal anyway. 324 // string literal anyway.
Lasse Reichstein Nielsen 2015/02/23 13:25:05 Technically, you don't have to escape \b, \t, \f a
floitsch 2015/02/23 16:56:41 Yes. My initial patch (that wasn't using this func
sra1 2015/02/23 17:47:27 I think we can have more than one version of this
303 escaped = escaped.replaceAllMapped(new RegExp('\n|"|\b|\t|\v'), (match) { 325 escaped = escaped.replaceAllMapped(
326 new RegExp('\n|"|\b|\t|\v|\u2028|\u2029'), (match) {
sra1 2015/02/23 17:47:27 new RegExp -> static variable.
floitsch 2015/02/23 18:16:45 It reads less nice, but done. Called "_stringEscap
Lasse Reichstein Nielsen 2015/02/24 14:14:21 How about: new RegExp(r'[\n\b\t\v"\u2028\u2029]'
floitsch 2015/02/24 20:49:44 Done.
304 switch (match.group(0)) { 327 switch (match.group(0)) {
305 case "\n" : return r"\n"; 328 case "\n" : return r"\n";
306 case "\"" : return r'\"'; 329 case "\"" : return r'\"';
307 case "\b" : return r"\b"; 330 case "\b" : return r"\b";
308 case "\t" : return r"\t"; 331 case "\t" : return r"\t";
309 case "\f" : return r"\f"; 332 case "\f" : return r"\f";
310 case "\v" : return r"\v"; 333 case "\v" : return r"\v";
334 case "\u2028" : return r"\u2028";
335 case "\u2029" : return r"\u2029";
311 } 336 }
312 }); 337 });
Lasse Reichstein Nielsen 2015/02/23 13:25:05 Does this have a measurable performance impact? Be
floitsch 2015/02/23 16:56:41 I tried with a string of 500000 \u2029 characters
313 LiteralString result = string(escaped); 338 LiteralString result = string(escaped);
314 // We don't escape ' under the assumption that the string is wrapped 339 // We don't escape ' under the assumption that the string is wrapped
315 // into ". Verify that assumption. 340 // into ". Verify that assumption.
Lasse Reichstein Nielsen 2015/02/23 13:25:05 Have you considered checking whether switching to
floitsch 2015/02/23 16:56:41 Of course. But I didn't yet go ahead and to the op
sra1 2015/02/23 17:47:26 This has an issue :-) https://code.google.com/p/da
316 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0)); 341 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0));
317 return result; 342 return result;
318 } 343 }
319 344
320 /// Creates a literal js string from [value]. 345 /// Creates a literal js string from [value].
321 /// 346 ///
322 /// Note that this function only puts quotes around [value]. It does not do 347 /// Note that this function only puts quotes around [value]. It does not do
323 /// any escaping, so use only when you can guarantee that [value] does not 348 /// any escaping, so use only when you can guarantee that [value] does not
324 /// contain newlines or backslashes. For escaping the string use 349 /// contain newlines or backslashes. For escaping the string use
325 /// [escapedString]. 350 /// [escapedString].
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
1327 Catch parseCatch() { 1352 Catch parseCatch() {
1328 expectCategory(LPAREN); 1353 expectCategory(LPAREN);
1329 String identifier = lastToken; 1354 String identifier = lastToken;
1330 expectCategory(ALPHA); 1355 expectCategory(ALPHA);
1331 expectCategory(RPAREN); 1356 expectCategory(RPAREN);
1332 expectCategory(LBRACE); 1357 expectCategory(LBRACE);
1333 Block body = parseBlock(); 1358 Block body = parseBlock();
1334 return new Catch(new VariableDeclaration(identifier), body); 1359 return new Catch(new VariableDeclaration(identifier), body);
1335 } 1360 }
1336 } 1361 }
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