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: need a StripSlashes function Un-quotes a quoted string. #43579

Open
LockGit opened this issue Jan 8, 2021 · 5 comments
Open

proposal: strings: need a StripSlashes function Un-quotes a quoted string. #43579

LockGit opened this issue Jan 8, 2021 · 5 comments
Labels
Projects
Milestone

Comments

@LockGit
Copy link

@LockGit 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

@ianlancetaylor ianlancetaylor commented Jan 8, 2021

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 Jan 8, 2021
@robpike
Copy link
Contributor

@robpike 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 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 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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Proposals
Incoming
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants