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

proposal: strconv: add a version of QuoteToASCII that doesn't add quotes #34145

Closed
the80srobot opened this issue Sep 6, 2019 · 7 comments
Closed

Comments

@the80srobot
Copy link

I'd like to propose exporting the function strconv.quoteWith.

Use case 1: strconv.QuoteWith(s, 0, true, false) to ASCII-escape a string without adding quotes. One might want to do this for terminal output or to escape filenames.

Use case 2: strconv.QuoteWith(s, '\'', false, false) to single-quote a string.

As an additional extension, consider making strconv.QuoteWith accept a func(rune)bool instead of two bool values. It would then be used like this:

Use case 1: strconv.QuoteWith(s, 0, func(r rune) bool { return !IsPrint(r) })
Use case 2: strconv.QuoteWith(s, 0, nil)

@gopherbot gopherbot added this to the Proposal milestone Sep 6, 2019
@ianlancetaylor ianlancetaylor changed the title Proposal: Add a version of strconv.QuoteToASCII that doesn't add quotes proposal: strconv: add a version of strconv.QuoteToASCII that doesn't add quotes Sep 6, 2019
@ianlancetaylor ianlancetaylor changed the title proposal: strconv: add a version of strconv.QuoteToASCII that doesn't add quotes proposal: strconv: add a version of QuoteToASCII that doesn't add quotes Sep 6, 2019
@ianlancetaylor
Copy link
Contributor

It would help to give a use case. Thanks.

@tmthrgd
Copy link
Contributor

tmthrgd commented Sep 7, 2019

You can do this already with strings.Trim like this:

s = strings.Trim(strconv.QuoteToASCII(s), `"`)

Try this playground example: https://play.golang.org/p/l-LKg504RDu

@the80srobot
Copy link
Author

My immediate use case is that I have a daemon that uploads log files in batches. It assigns each batch a human-readable source identifier (derived from the source file name). For ease of debugging and sysadmin tasks, it's helpful for the identifiers to be visible and readable in two contexts: a downstream processing system, and temporary on-disk spooling.

Escaping the string lets the program preserve identifier readability, while using them for filenames and downstream primary keys. The proposed API change would let me define a function that decides which runes need escaping. (The current solution hex-encodes the identifiers instead.)

@ccbrown
Copy link

ccbrown commented Sep 9, 2019

You can do this already with strings.Trim like this:

s = strings.Trim(strconv.QuoteToASCII(s), `"`)

It's not quite as simple as this. This doesn't work for strings ending in quotes, e.g. `"`.

@tmthrgd
Copy link
Contributor

tmthrgd commented Sep 10, 2019

@ccbrown Ah, good catch. Still this should work without any new API:

s = strconv.QuoteToASCII(s)
s = s[1:len(s)-1]

@rsc
Copy link
Contributor

rsc commented Sep 12, 2019

There's already a lot of API around quoting. Given that the existing API must stay, it does not seem particularly compelling to add new API to avoid @tmthrgd's suggestion of just slicing off the quotes.

Does this come up often enough, in a large enough variety of programs, to merit being in the standard library?

@rsc
Copy link
Contributor

rsc commented Sep 25, 2019

Given the lack of a compelling use case and how easy it is to write two lines of code instead, this seems like a likely decline.

Leaving open for a week for final comments.

@rsc rsc closed this as completed Oct 2, 2019
@golang golang locked and limited conversation to collaborators Oct 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants