mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes, strings: add TrimPrefix and TrimSuffix
Everybody either gets confused and thinks this is TrimLeft/TrimRight or does this by hand which gets repetitive looking. R=rsc, kevlar CC=golang-dev https://golang.org/cl/7239044
This commit is contained in:
parent
fe14ee52cc
commit
e515d80d5d
21 changed files with 117 additions and 54 deletions
|
|
@ -794,8 +794,8 @@ func TestRunes(t *testing.T) {
|
|||
}
|
||||
|
||||
type TrimTest struct {
|
||||
f string
|
||||
in, cutset, out string
|
||||
f string
|
||||
in, arg, out string
|
||||
}
|
||||
|
||||
var trimTests = []TrimTest{
|
||||
|
|
@ -820,12 +820,17 @@ var trimTests = []TrimTest{
|
|||
{"TrimRight", "", "123", ""},
|
||||
{"TrimRight", "", "", ""},
|
||||
{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
|
||||
{"TrimPrefix", "aabb", "a", "abb"},
|
||||
{"TrimPrefix", "aabb", "b", "aabb"},
|
||||
{"TrimSuffix", "aabb", "a", "aabb"},
|
||||
{"TrimSuffix", "aabb", "b", "aab"},
|
||||
}
|
||||
|
||||
func TestTrim(t *testing.T) {
|
||||
for _, tc := range trimTests {
|
||||
name := tc.f
|
||||
var f func([]byte, string) []byte
|
||||
var fb func([]byte, []byte) []byte
|
||||
switch name {
|
||||
case "Trim":
|
||||
f = Trim
|
||||
|
|
@ -833,12 +838,21 @@ func TestTrim(t *testing.T) {
|
|||
f = TrimLeft
|
||||
case "TrimRight":
|
||||
f = TrimRight
|
||||
case "TrimPrefix":
|
||||
fb = TrimPrefix
|
||||
case "TrimSuffix":
|
||||
fb = TrimSuffix
|
||||
default:
|
||||
t.Errorf("Undefined trim function %s", name)
|
||||
}
|
||||
actual := string(f([]byte(tc.in), tc.cutset))
|
||||
var actual string
|
||||
if f != nil {
|
||||
actual = string(f([]byte(tc.in), tc.arg))
|
||||
} else {
|
||||
actual = string(fb([]byte(tc.in), []byte(tc.arg)))
|
||||
}
|
||||
if actual != tc.out {
|
||||
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
|
||||
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue