-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: strconv: add a version of QuoteToASCII that doesn't add quotes #34145
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
Comments
It would help to give a use case. Thanks. |
You can do this already with s = strings.Trim(strconv.QuoteToASCII(s), `"`) Try this playground example: https://play.golang.org/p/l-LKg504RDu |
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.) |
It's not quite as simple as this. This doesn't work for strings ending in quotes, e.g. |
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? |
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. |
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 afunc(rune)bool
instead of twobool
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)
The text was updated successfully, but these errors were encountered: