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: strings: add StripSlashes like PHP stripslashes #43579

Closed
LockGit opened this issue Jan 8, 2021 · 8 comments
Closed

proposal: strings: add StripSlashes like PHP stripslashes #43579

LockGit opened this issue Jan 8, 2021 · 8 comments

Comments

@LockGit
Copy link

LockGit commented Jan 8, 2021

Provide a new function, like any other language, function can Un-quotes a quoted string.

Describe:
Returns a string with backslashes stripped off. (' becomes ' and so on.)
Double backslashes (\) are made into a single backslash ().

For example:

fmt.Println(StripSlashes(`\\\\1`))  
// will print  "\\1"

other language function like: https://www.php.net/manual/en/function.stripslashes.php

See pull request:
#43492

Relates to the proposal:
https://go-review.googlesource.com/c/go/+/281372

@gopherbot gopherbot added this to the Proposal milestone Jan 8, 2021
@ianlancetaylor
Copy link
Contributor

Please describe exactly what the proposed function will do. For example, please give the documentation comment that would appear in https://golang.org/pkg/strings. Thanks.

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jan 8, 2021
@robpike
Copy link
Contributor

robpike commented Jan 8, 2021

Doesn't strconv.Unquote do the job you need? You need to attach double quotes to the string, but that is a trivial adjustment.

https://play.golang.org/p/UcgucN3TB_h

@LockGit
Copy link
Author

LockGit commented Jan 8, 2021

https://play.golang.org/p/THD-0MyJmnW

If the string contains quotation marks in the middle.It looks more difficult to handle.

@robpike
Copy link
Contributor

robpike commented Jan 9, 2021

That is unfortunate, but the code is telling you the truth: That string is not a valid Go string to be unquoted, as it contains unescaped double quotes. You are asking to solve a problem that is not well defined within the Go environment.

Go has distinct rules for backslashes that are context-dependent. " is legal only in a double-quoted string, while ' is legal only in a rune constant. That is why strconv.Unquote requires the quote character to be included: it defines the rules for unquoting.

You are asking for a new set of rules, and so it seems unlikely to be something needed in the standard Go library. I suggest you write the code that does what you need, and use.

@rsc
Copy link
Contributor

rsc commented Jan 13, 2021

PHP stripslashes is essentially PHP unquote, as far as I can tell.
If you need to unquote Go strings, use strconv.Unquote.
If you need to unquote another syntax, you should use a syntax-specific unquoter.
It's impossible to write a "generic" unquoter because you wouldn't know which syntaxes to allow or not.

@rsc rsc changed the title proposal: strings: need a StripSlashes function Un-quotes a quoted string. proposal: strings: add StripSlashes like PHP stripslashes Jun 22, 2022
@rsc rsc moved this from Incoming to Active in Proposals (old) Jun 22, 2022
@rsc
Copy link
Contributor

rsc commented Jun 22, 2022

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Active to Likely Decline in Proposals (old) Jul 1, 2022
@rsc
Copy link
Contributor

rsc commented Jul 1, 2022

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc rsc moved this from Likely Decline to Declined in Proposals (old) Jul 13, 2022
@rsc
Copy link
Contributor

rsc commented Jul 13, 2022

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Jul 13, 2022
@golang golang locked and limited conversation to collaborators Jul 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

5 participants