Skip to content

Centralize and improve String literal handling #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2018
Merged

Conversation

kevmoo
Copy link
Collaborator

@kevmoo kevmoo commented Apr 2, 2018

No description provided.

@kevmoo kevmoo requested a review from natebosch April 2, 2018 15:57
@@ -11,6 +11,42 @@ import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'

import 'package:source_gen/source_gen.dart';

// TODO: still need handle tripe singe/double quotes within `value`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in triple

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

@@ -11,6 +11,42 @@ import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'

import 'package:source_gen/source_gen.dart';

// TODO: still need handle tripe singe/double quotes within `value`
String escapeDartString(String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

// TODO: still need handle tripe singe/double quotes within `value`
String escapeDartString(String value) {
if (value.contains('\n')) {
return "r'''\n$value'''";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the leading /n?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because one leading \n is ignored. So if it's the first character in the string, it's skipped.

Copy link

@lrhn lrhn Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also ignores leading white space if that's all there is before the first line terminator, so yes, the leading newlines is a good idea).

You might also want to check for \r.


if (containsSingleQuote) {
if (value.contains('"')) {
// `value` contains both single and double quotes as well as `$`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't know if it contains $ here. Are we assuming that it could so may as well escape it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Will dig in a bit...

// The only safe way to wrap the content is to escape all of the
// problematic characters.
var string = value
.replaceAll('\$', '\\\$')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent between '\$' and r'$' from line 21. How about .replaceAll(r'$', r'\$')

Similarly for the quotes I think it's more readable with r than with double slash.

@kevmoo
Copy link
Collaborator Author

kevmoo commented Apr 2, 2018

@natebosch – PTAL

var string = value
.replaceAll('\$', '\\\$')
.replaceAll('"', '\\"')
.replaceAll("'", "\\'");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing three replacements operation, where at least two are known to change the string, may be a little costly on large strings.
On the other hand, it's probably much cheaper than the alternative:

  var string = value.replaceAll(new RegExp(r"""(?=[$'"])""", r"\");

(even if the RegExp and replacement function are both made constant).

Consider whether it might be worth it as a special case for large strings.

Also still a problem if the string contains \$ already.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will investigate in a follow-up CL. This is strictly better than what we had before...I think...

@kevmoo kevmoo merged commit 5f3977d into master Apr 2, 2018
@kevmoo kevmoo deleted the string_literals branch April 2, 2018 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants