Skip to content
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

Use raw literal for string formatting #46

Merged
merged 1 commit into from
Sep 28, 2017
Merged

Use raw literal for string formatting #46

merged 1 commit into from
Sep 28, 2017

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Sep 28, 2017

When formatting strings, avoid using strconv.Quote if the string looks
like it has already been escaped. Instead, use a raw string literal
by wrapping the string with '`' characters if possible.
For now, we still use strconv.Quote if the input string contains newlines
to maintain the property that Format outputs a single line.

Also, prefix strings obtained by the Stringer.String method with a 's'.
This allows users to more easily distinguish when an output really is
of type string or if the String method was used.

When formatting strings, avoid using strconv.Quote if the string looks
like it has already been escaped. Instead, use a raw string literal
by wrapping the string with '`' characters if possible.
For now, we still use strconv.Quote if the input string contains newlines
to maintain the property that Format outputs a single line.

Also, prefix strings obtained by the Stringer.String method with a 's'.
This allows users to more easily distinguish when an output really is
of type string or if the String method was used.
Copy link
Collaborator

@neild neild left a comment

Choose a reason for hiding this comment

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

I note that the s"string" syntax is non-standard and will probably result in a fair bit of user confusion.

@dsnet
Copy link
Collaborator Author

dsnet commented Sep 28, 2017

I couldn't think of anything better. The fundamental problem is that Stringer.String is an irreversible transform. I considered using String() = as the prefix, but that looks overly verbose when formatting map keys or a slice of elements, each with a String method.

Regardless, I think some prefix is better than no prefix. The previous output was subtly lying to the user. We could avoid the Stringer.String altogether, but I believe there is value is having user defined outputs.

@neild
Copy link
Collaborator

neild commented Sep 28, 2017

Yeah, I don't have a better alternative, and I agree that marking Stringer output is worthwhile. It's still going to cause confusion, though.

@dsnet
Copy link
Collaborator Author

dsnet commented Sep 28, 2017

Submitting as is then. We can always adjust this in the future if someone has a better idea.

@dsnet dsnet merged commit 576e243 into master Sep 28, 2017
@dsnet dsnet deleted the format-string branch September 28, 2017 23:31
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.

None yet

2 participants