diff options
| author | Ben Hoyt <benhoyt@gmail.com> | 2019-03-27 07:36:27 -0400 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2019-03-27 13:52:52 +0000 |
| commit | f24e1099cb28d5ab793e5259c6ee2733227eb2f2 (patch) | |
| tree | d3de7d8aa2c4af9b0a5b4f1034f5dde6b6e3cdb8 /src/bytes/bytes.go | |
| parent | 39a51a4b0d698491baaa252e21be2a51516379ea (diff) | |
| download | go-f24e1099cb28d5ab793e5259c6ee2733227eb2f2.tar.xz | |
bytes: make TrimSpace return nil on all-space input
Issue #29122 introduced a subtle regression due to the way that
TrimFuncLeft is written: previously TrimSpace returned nil when given
an input of all whitespace, but with the #29122 changes it returned an
empty slice on all-space input.
This change adds a special case to the new, optimized TrimSpace to go
back to that behavior. 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.
This tweak doesn't change the TrimSpace benchmarks significantly.
Fixes #31038
Change-Id: Idb495d02b474054d2b2f593c2e318a7a6625688a
Reviewed-on: https://go-review.googlesource.com/c/go/+/169518
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/bytes/bytes.go')
| -rw-r--r-- | src/bytes/bytes.go | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 08fc14d837..bdd55fca4a 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -788,6 +788,11 @@ func TrimSpace(s []byte) []byte { // At this point s[start:stop] starts and ends with an ASCII // non-space bytes, so we're done. Non-ASCII cases have already // been handled above. + if start == stop { + // Special case to preserve previous TrimLeftFunc behavior, + // returning nil instead of empty slice if all spaces. + return nil + } return s[start:stop] } |
