mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: port IndexFunc and LastIndexFunc from strings package
This CL basically applies the same changes as http://code.google.com/p/go/source/detail?r=5e0a29014e8e but for bytes package. R=r, rog CC=golang-dev https://golang.org/cl/1670052
This commit is contained in:
parent
2b3508425e
commit
e356f1d88f
4 changed files with 180 additions and 80 deletions
|
|
@ -420,25 +420,6 @@ var trimTests = []TrimTest{
|
|||
TrimTest{TrimRight, "☺\xc0", "☺", "☺\xc0"},
|
||||
}
|
||||
|
||||
// naiveTrimRight implements a version of TrimRight
|
||||
// by scanning forwards from the start of s.
|
||||
func naiveTrimRight(s string, cutset string) string {
|
||||
i := -1
|
||||
for j, r := range s {
|
||||
if IndexRune(cutset, r) == -1 {
|
||||
i = j
|
||||
}
|
||||
}
|
||||
if i >= 0 && s[i] >= utf8.RuneSelf {
|
||||
_, wid := utf8.DecodeRuneInString(s[i:])
|
||||
i += wid
|
||||
} else {
|
||||
i++
|
||||
}
|
||||
return s[0:i]
|
||||
}
|
||||
|
||||
|
||||
func TestTrim(t *testing.T) {
|
||||
for _, tc := range trimTests {
|
||||
actual := tc.f(tc.in, tc.cutset)
|
||||
|
|
@ -456,16 +437,14 @@ func TestTrim(t *testing.T) {
|
|||
if actual != tc.out {
|
||||
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
|
||||
}
|
||||
// test equivalence of TrimRight to naive version
|
||||
if tc.f == TrimRight {
|
||||
naive := naiveTrimRight(tc.in, tc.cutset)
|
||||
if naive != actual {
|
||||
t.Errorf("TrimRight(%q, %q) = %q, want %q", tc.in, tc.cutset, actual, naive)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type predicate struct {
|
||||
f func(r int) bool
|
||||
name string
|
||||
}
|
||||
|
||||
var isSpace = predicate{unicode.IsSpace, "IsSpace"}
|
||||
var isDigit = predicate{unicode.IsDigit, "IsDigit"}
|
||||
var isUpper = predicate{unicode.IsUpper, "IsUpper"}
|
||||
|
|
@ -476,11 +455,6 @@ var isValidRune = predicate{
|
|||
"IsValidRune",
|
||||
}
|
||||
|
||||
type predicate struct {
|
||||
f func(r int) bool
|
||||
name string
|
||||
}
|
||||
|
||||
type TrimFuncTest struct {
|
||||
f predicate
|
||||
in, out string
|
||||
|
|
@ -530,7 +504,7 @@ var indexFuncTests = []IndexFuncTest{
|
|||
IndexFuncTest{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
|
||||
IndexFuncTest{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
|
||||
|
||||
// broken unicode tests
|
||||
// tests of invalid UTF-8
|
||||
IndexFuncTest{"\x801", isDigit, 1, 1},
|
||||
IndexFuncTest{"\x80abc", isDigit, -1, -1},
|
||||
IndexFuncTest{"\xc0a\xc0", isValidRune, 1, 1},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue