mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
39a51a4b0d
commit
f24e1099cb
2 changed files with 46 additions and 30 deletions
|
|
@ -788,6 +788,11 @@ func TrimSpace(s []byte) []byte {
|
||||||
// At this point s[start:stop] starts and ends with an ASCII
|
// At this point s[start:stop] starts and ends with an ASCII
|
||||||
// non-space bytes, so we're done. Non-ASCII cases have already
|
// non-space bytes, so we're done. Non-ASCII cases have already
|
||||||
// been handled above.
|
// 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]
|
return s[start:stop]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -909,54 +909,65 @@ func TestFieldsFunc(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test case for any function which accepts and returns a byte slice.
|
// Test case for any function which accepts and returns a byte slice.
|
||||||
// For ease of creation, we write the byte slices as strings.
|
// For ease of creation, we write the input byte slice as a string.
|
||||||
type StringTest struct {
|
type StringTest struct {
|
||||||
in, out string
|
in string
|
||||||
|
out []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
var upperTests = []StringTest{
|
var upperTests = []StringTest{
|
||||||
{"", ""},
|
{"", []byte("")},
|
||||||
{"abc", "ABC"},
|
{"abc", []byte("ABC")},
|
||||||
{"AbC123", "ABC123"},
|
{"AbC123", []byte("ABC123")},
|
||||||
{"azAZ09_", "AZAZ09_"},
|
{"azAZ09_", []byte("AZAZ09_")},
|
||||||
{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
|
{"\u0250\u0250\u0250\u0250\u0250", []byte("\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F")}, // grows one byte per char
|
||||||
}
|
}
|
||||||
|
|
||||||
var lowerTests = []StringTest{
|
var lowerTests = []StringTest{
|
||||||
{"", ""},
|
{"", []byte("")},
|
||||||
{"abc", "abc"},
|
{"abc", []byte("abc")},
|
||||||
{"AbC123", "abc123"},
|
{"AbC123", []byte("abc123")},
|
||||||
{"azAZ09_", "azaz09_"},
|
{"azAZ09_", []byte("azaz09_")},
|
||||||
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
|
{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", []byte("\u0251\u0251\u0251\u0251\u0251")}, // shrinks one byte per char
|
||||||
}
|
}
|
||||||
|
|
||||||
const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
|
const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
|
||||||
|
|
||||||
var trimSpaceTests = []StringTest{
|
var trimSpaceTests = []StringTest{
|
||||||
{"", ""},
|
{"", nil},
|
||||||
{"abc", "abc"},
|
{" a", []byte("a")},
|
||||||
{space + "abc" + space, "abc"},
|
{"b ", []byte("b")},
|
||||||
{" ", ""},
|
{"abc", []byte("abc")},
|
||||||
{" \t\r\n \t\t\r\r\n\n ", ""},
|
{space + "abc" + space, []byte("abc")},
|
||||||
{" \t\r\n x\t\t\r\r\n\n ", "x"},
|
{" ", nil},
|
||||||
{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
|
{"\u3000 ", nil},
|
||||||
{"1 \t\r\n2", "1 \t\r\n2"},
|
{" \u3000", nil},
|
||||||
{" x\x80", "x\x80"},
|
{" \t\r\n \t\t\r\r\n\n ", nil},
|
||||||
{" x\xc0", "x\xc0"},
|
{" \t\r\n x\t\t\r\r\n\n ", []byte("x")},
|
||||||
{"x \xc0\xc0 ", "x \xc0\xc0"},
|
{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", []byte("x\t\t\r\r\ny")},
|
||||||
{"x \xc0", "x \xc0"},
|
{"1 \t\r\n2", []byte("1 \t\r\n2")},
|
||||||
{"x \xc0 ", "x \xc0"},
|
{" x\x80", []byte("x\x80")},
|
||||||
{"x \xc0\xc0 ", "x \xc0\xc0"},
|
{" x\xc0", []byte("x\xc0")},
|
||||||
{"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
|
{"x \xc0\xc0 ", []byte("x \xc0\xc0")},
|
||||||
{"x ☺ ", "x ☺"},
|
{"x \xc0", []byte("x \xc0")},
|
||||||
|
{"x \xc0 ", []byte("x \xc0")},
|
||||||
|
{"x \xc0\xc0 ", []byte("x \xc0\xc0")},
|
||||||
|
{"x ☺\xc0\xc0 ", []byte("x ☺\xc0\xc0")},
|
||||||
|
{"x ☺ ", []byte("x ☺")},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute f on each test case. funcName should be the name of f; it's used
|
// Execute f on each test case. funcName should be the name of f; it's used
|
||||||
// in failure reports.
|
// in failure reports.
|
||||||
func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) {
|
func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
actual := string(f([]byte(tc.in)))
|
actual := f([]byte(tc.in))
|
||||||
if actual != tc.out {
|
if actual == nil && tc.out != nil {
|
||||||
|
t.Errorf("%s(%q) = nil; want %q", funcName, tc.in, tc.out)
|
||||||
|
}
|
||||||
|
if actual != nil && tc.out == nil {
|
||||||
|
t.Errorf("%s(%q) = %q; want nil", funcName, tc.in, actual)
|
||||||
|
}
|
||||||
|
if !Equal(actual, tc.out) {
|
||||||
t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
|
t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue