-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
bytes, strings: add ReplaceAll #27864
Comments
That would be |
Thanks for the downvote. I created a PR here https://go-review.googlesource.com/c/go/+/137456 and the implementation is exactly to call My problem with the current state of affairs is that I find myself looking up the syntax of this every time I use it. Or if I am reviewing someone else's code I stumble over the |
Numbers would be interesting here. What percentage of strings.Replace calls in the wild hard-code -1? |
> What percentage of strings.Replace calls in the wild hard-code -1? I guestimate that on the small sample of my own code it's about 99%. Yet I don't think a function that just sets a default value to one of the parameters of another stdlib function belongs to the stdlib. |
@bradfitz From my company's codebase, |
@cznic Btw, there are these helper in the standard library already: from powser2: func Print(U PS) {
Printn(U, 1000000000)
} from draw: func Draw(dst Image, r image.Rectangle, src image.Image, sp image.Point, op Op) {
DrawMask(dst, r, src, sp, nil, image.Point{}, op)
} from ast: func Inspect(node Node, f func(Node) bool) {
Walk(inspector(f), node)
} |
@HaraldNordgren powser2.go is a test, not library code. |
Or strings.Fields and strings.FieldFunc. Or http.ListenAndServe vs http.Server.ListenAndServe. Or regexp.Match vs regexp.Compile+Match. Or regexp.MustCompile vs regexp.Compile, or template.Parse vs template.MustParse. I don't agree with @cznic thumbs down for this; the standard library is full of convenient-vs-lower-level versions of things. I'd rather see numbers. |
in my personal/work code: 16 calls, all -1. |
In Perkeep, 60 use -1 and 43 use 1. No other values. |
There are 265 calls to strings.Replace in the main Go repo, of which 216 use -1. |
In the Go corpus, excluding vendored code, 1034 of 2213 calls to strings.Replace hard-code -1. I'm with cznic on this one. Nearly all of the examples provided by bradfitz are more complex (syntactically and semantically) than providing a trivial default value for an argument. Panicking instead of returning an error is a significant change in API. regexp.Match encapsulates two separate operations. strings.Field is far more complex, providing an optimized implementation and pulling in additional dependencies. In all of these cases, there's really no question as to which function should be used. I would rather not, however, tell people to use ReplaceAll instead of Replace, and I wouldn't want to see a mix of old and new code, either. |
That's somewhat inevitable with the standard library's policy of never removing old stuff or mistakes. e.g. we have a mix of Seek values |
Excluding generated and vendored code. In k8s codebase. in moby code base: |
regexp.Regexp has a ReplaceAll, so the name is at least with precedent. If we had it to do over again I'd make Replace like Split, where the default is "all" and we could have ReplaceN for a limited count. But we don't have it to do over again. So ReplaceAll seems OK. Should be done to both strings and bytes though. |
Replace(s, old, new, -1)
ReplaceAll(s, old, new) One character saved. One minute wasted for a Go programmer to lookup the new API function documentation. Times ~1 million of them. |
FYI. At Google, 65% of all Edit: looks like I'm late to the party and this is already accepted. Ignore me. |
That's hardcore. |
Change https://golang.org/cl/137855 mentions this issue: |
Change https://golang.org/cl/137856 mentions this issue: |
I omitted vendor directories and anything necessary for bootstrapping. (Tested by bootstrapping with Go 1.4) Updates #27864 Change-Id: I7d9b68d0372d3a34dee22966cca323513ece7e8a Reviewed-on: https://go-review.googlesource.com/137856 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
@bradfitz don't you think following encourages bad naming patterns since
reviewer: don't use built-in as an argument name ? |
Shadowing built-ins is an explicit feature of the language; nobody will get confused by a variable named |
@dominikh thanks for your insight.
Sorry for noise. Will be more careful about it next time. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: