-
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: TrimSpace returns non-nil byte slice when given all-space input #31038
Comments
This is unfortunate, because "returns a subslice of s" to me suggests it returns an empty slice, not nil, on an all-space input. (Might not be worth breaking, though.) |
This is unfortunate, indeed, but we should try to preserve the existing behavior. From a glance, looks like we just need to replace |
@josharian Yes, I'm interested in following up. What timeline are we looking at? I can probably do this in the next 7-10 days. Are we definitely sure we want to preserve backwards compatibility here, and the weirdness? Or move to always returning a non-nil slice? |
Sooner is better of course, but we have at least a month worst case. If it is remotely reasonable, let’s remain perfectly backwards compatible, weird or no. |
Spoke with @ianlancetaylor about this and while it is odd behavior and people shouldn't be relying on these functions returning a nil slice in practice, it's not worth the breakage of code that does. |
Change https://golang.org/cl/169518 mentions this issue: |
Submitted. While I was in there, I noticed there were no tests for |
Some tests would be nice, yes, in a separate CL. No need to open an issue. Thanks. |
Change https://golang.org/cl/170061 mentions this issue: |
When I was working on the fix for #31038 (make TrimSpace return nil on all-space input) I noticed that there were no tests for TrimLeftFunc and TrimRightFunc, including the funky nil behavior. So add some! I've just reused the existing TrimFunc test cases for TrimLeftFunc and TrimRightFunc, as well as adding new tests for the empty string and all-trimmed cases (which test the nil-returning behavior of TrimFunc and TrimLeftFunc). Change-Id: Ib580d4364e9b3c91350305f9d9873080d7862904 Reviewed-on: https://go-review.googlesource.com/c/go/+/170061 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
No. This is a regression from go1.12.1 and earlier.
On tip (1.13) it outputs
false
while on previous versions it printstrue
. This was caused by https://golang.org/cl/152917 because it no longer usesbytes.TrimFunc(s, unicode.IsSpace)
which callsbytes.TrimLeftFunc
on an empty string, causing it to return nil due toindexFunc
returning -1:It seems weird that
TrimRightFunc
returns a non-nil slice every time, whileTrimLeftFunc
returns a nil slice if it can't find a rune that satisfiesf(c)
, but I imagine changing that behavior would cause more breakage in weird places.@benhoyt @josharian
The text was updated successfully, but these errors were encountered: