-
Notifications
You must be signed in to change notification settings - Fork 18k
strings: add CutPrefix and CutSuffix #42537
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
|
I find |
|
It's unclear whether we need these, but at the very least we'd need better names. |
(With all respect) It feels the proposal lacks the use cases where the new functions required. Knowing that the current implementation of both
can't one always use - if strings.HasPrefix(suffix, "/vendor/") {
- return strings.TrimPrefix(suffix, "/vendor/")
- }
+ return strings.TrimPrefix(suffix, "/vendor/") |
@narqo no. The places where you use For example, you suggested
but those are not the same thing, because your version returns if I listed the filenames that all my examples come from. You can go check out that example in context and you'll see it looks like this:
|
Putting on hold with Cut. |
I adopted @ianlancetaylor's suggested names. In the intervening time since filing this proposal, I haven't thought of a better name. But I've used these functions constantly, and I've ended up copy-pasting them into many of my projects. |
@rsc Cut has been proposed, accepted, and implemented. Can we un-hold this proposal now? |
Moved out of proposal-hold. Do these add enough value over just using |
@ianlancetaylor I'm not sure I see how To me, using
With
Using
Is that what you had in mind? To me, that is much less clear (since it doesn't say "suffix"). If I had to write this using only the strings functions that exist today, I wouldn't use
That at least says "suffix" and it avoids writing the suffix string (".txt") twice, but it requires an extra line and a separate string variable in comparison with the |
Cut doesn't change things but these seem remarkably minor. HasPrefix works well for this use case. |
This proposal has been added to the active column of the proposals project |
It doesn't seem like these are providing enough value to add to the standard library. Does anyone want to make a case for accepting? |
The reasoning for why this belongs in the standard library has two parts. These functions are improvements over various poor patterns that people write in their absence. Without These functions improve a large number of sites. I didn't address frequency of use in my original proposal so I took a quick skim now. Using the Go source tree (and just grep, so these numbers are a little rough):
So Then I examined a random sampling of 50 For
For
Extrapolating from these samples, I estimate that 288/1029 calls to By way of comparison, the repo contains 115 uses of |
These statistics do make a good case. Thank you for taking the time to gather them. strings.TrimmedPrefix is a bit too long a name. Given that we have strings.Cut that returns a 'found' boolean already, which is what we want to add here, maybe strings.CutPrefix and strings.CutSuffix? |
Retitled, and will move to likely accept. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/407176 mentions this issue: |
I forgot to mention it in the proposal, but we should also add these functions to bytes, correct? To keep parity? |
Change https://go.dev/cl/432895 mentions this issue: |
Change https://go.dev/cl/433275 mentions this issue: |
Updates #42537 Change-Id: Ice23d7d36bbede27551cbc086119694f6a3b5e4a GitHub-Last-Rev: 0d65208 GitHub-Pull-Request: #55347 Reviewed-on: https://go-review.googlesource.com/c/go/+/432895 Reviewed-by: Benny Siegert <bsiegert@gmail.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Updates #42537 Change-Id: I2d4c5e911c8a2ddfe9a976896b05d3cd8be61f6b GitHub-Last-Rev: a87597d GitHub-Pull-Request: #55830 Reviewed-on: https://go-review.googlesource.com/c/go/+/433275 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Benny Siegert <bsiegert@gmail.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
Fixes golang#42537 Change-Id: Ie03c2614ffee30ebe707acad6b9f6c28fb134a45 Reviewed-on: https://go-review.googlesource.com/c/go/+/407176 Reviewed-by: Benny Siegert <bsiegert@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Changkun Ou <mail@changkun.de> Reviewed-by: Ian Lance Taylor <iant@google.com> (cherry picked from commit 6800559)
Inspired by #40135, I have another proposal for an addition to the strings package (and by extension, I suppose, the bytes package).
The strings functions I most frequently wish for are variants of
TrimPrefix
andTrimSuffix
which report whether they did any trimming.Lacking these functions, Go authors resort to fragile code, often writing a prefix/suffix literal twice or hard-coding its length:
Of course, a function like
TrimmedPrefix
is easy to write and can exist outside of the standard library. At my company we have these functions in an internal string helper package and they see regular use. But certainly I wouldn't pull in a dependency for such a tiny helper function and so when I'm working on my own projects I generally just use the above workarounds.Here are some examples from the Go source tree along with how they could be altered to use
TrimmedPrefix
/TrimmedSuffix
:src/cmd/go/internal/modload/load.go
:becomes
src/cmd/go/proxy_test.go
:becomes
src/testing/benchmark.go
:becomes
src/testing/fstest/mapfs.go
:becomes
src/mime/mediatype.go
:becomes
test/run.go
:becomes
test/run.go
:becomes
src/runtime/testdata/testprog/traceback_ancestors.go
:becomes
Update 2022-03-26: Changed proposed names to
TrimmedPrefix
/TrimmedSuffix
(per @ianlancetaylor's suggestion).The text was updated successfully, but these errors were encountered: