Commit e356f1d8 authored by Fazlul Shahriar's avatar Fazlul Shahriar Committed by Rob Pike

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
parent 2b350842
...@@ -127,7 +127,7 @@ func LastIndex(s, sep []byte) int { ...@@ -127,7 +127,7 @@ func LastIndex(s, sep []byte) int {
return -1 return -1
} }
// IndexAny interprets s as a sequence of UTF-8 encoded Unicode code points. // IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points.
// It returns the byte index of the first occurrence in s of any of the Unicode // It returns the byte index of the first occurrence in s of any of the Unicode
// code points in chars. It returns -1 if chars is empty or if there is no code // code points in chars. It returns -1 if chars is empty or if there is no code
// point in common. // point in common.
...@@ -278,7 +278,7 @@ func HasSuffix(s, suffix []byte) bool { ...@@ -278,7 +278,7 @@ func HasSuffix(s, suffix []byte) bool {
// Map returns a copy of the byte array s with all its characters modified // Map returns a copy of the byte array s with all its characters modified
// according to the mapping function. If mapping returns a negative value, the character is // according to the mapping function. If mapping returns a negative value, the character is
// dropped from the string with no replacement. The characters in s and the // dropped from the string with no replacement. The characters in s and the
// output are interpreted as UTF-8 encoded Unicode code points. // output are interpreted as UTF-8-encoded Unicode code points.
func Map(mapping func(rune int) int, s []byte) []byte { func Map(mapping func(rune int) int, s []byte) []byte {
// In the worst case, the array can grow when mapped, making // In the worst case, the array can grow when mapped, making
// things unpleasant. But it's so rare we barge in assuming it's // things unpleasant. But it's so rare we barge in assuming it's
...@@ -378,50 +378,102 @@ func Title(s []byte) []byte { ...@@ -378,50 +378,102 @@ func Title(s []byte) []byte {
s) s)
} }
// TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8 encoded // TrimLeftFunc returns a subslice of s by slicing off all leading UTF-8-encoded
// Unicode code points c that satisfy f(c). // Unicode code points c that satisfy f(c).
func TrimLeftFunc(s []byte, f func(r int) bool) []byte { func TrimLeftFunc(s []byte, f func(r int) bool) []byte {
var start, wid int i := indexFunc(s, f, false)
for start = 0; start < len(s); start += wid { if i == -1 {
wid = 1 return nil
}
return s[i:]
}
// TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8
// encoded Unicode code points c that satisfy f(c).
func TrimRightFunc(s []byte, f func(r int) bool) []byte {
i := lastIndexFunc(s, f, false)
if i >= 0 && s[i] >= utf8.RuneSelf {
_, wid := utf8.DecodeRune(s[i:])
i += wid
} else {
i++
}
return s[0:i]
}
// TrimFunc returns a subslice of s by slicing off all leading and trailing
// UTF-8-encoded Unicode code points c that satisfy f(c).
func TrimFunc(s []byte, f func(r int) bool) []byte {
return TrimRightFunc(TrimLeftFunc(s, f), f)
}
// IndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
// It returns the byte index in s of the first Unicode
// code point satisfying f(c), or -1 if none do.
func IndexFunc(s []byte, f func(r int) bool) int {
return indexFunc(s, f, true)
}
// LastIndexFunc interprets s as a sequence of UTF-8-encoded Unicode code points.
// It returns the byte index in s of the last Unicode
// code point satisfying f(c), or -1 if none do.
func LastIndexFunc(s []byte, f func(r int) bool) int {
return lastIndexFunc(s, f, true)
}
// indexFunc is the same as IndexFunc except that if
// truth==false, the sense of the predicate function is
// inverted.
func indexFunc(s []byte, f func(r int) bool, truth bool) int {
start := 0
for start < len(s) {
wid := 1
rune := int(s[start]) rune := int(s[start])
if rune >= utf8.RuneSelf { if rune >= utf8.RuneSelf {
rune, wid = utf8.DecodeRune(s[start:]) rune, wid = utf8.DecodeRune(s[start:])
} }
if !f(rune) { if f(rune) == truth {
break return start
} }
start += wid
} }
return s[start:] return -1
} }
// TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8 // lastIndexFunc is the same as LastIndexFunc except that if
// encoded Unicode code points c that satisfy f(c). // truth==false, the sense of the predicate function is
func TrimRightFunc(s []byte, f func(r int) bool) []byte { // inverted.
var end, wid int func lastIndexFunc(s []byte, f func(r int) bool, truth bool) int {
for end = len(s); end > 0; end -= wid { end := len(s)
wid = 1 for end > 0 {
rune := int(s[end-wid]) start := end - 1
rune := int(s[start])
if rune >= utf8.RuneSelf { if rune >= utf8.RuneSelf {
// Back up & look for beginning of rune. Mustn't pass start. // Back up & look for beginning of rune. Mustn't pass start.
for wid = 2; end-wid >= 0 && !utf8.RuneStart(s[end-wid]); wid++ { for start--; start >= 0; start-- {
} if utf8.RuneStart(s[start]) {
if end-wid < 0 { // invalid UTF-8 sequence; stop processing
break break
} }
rune, wid = utf8.DecodeRune(s[end-wid : end])
}
if !f(rune) {
break
} }
if start < 0 {
return -1
} }
return s[0:end] var wid int
} rune, wid = utf8.DecodeRune(s[start:end])
// TrimFunc returns a subslice of s by slicing off all leading and trailing // If we've decoded fewer bytes than we expected,
// UTF-8 encoded Unicode code points c that satisfy f(c). // we've got some invalid UTF-8, so make sure we return
func TrimFunc(s []byte, f func(r int) bool) []byte { // the last possible index in s.
return TrimRightFunc(TrimLeftFunc(s, f), f) if start+wid < end && f(utf8.RuneError) == truth {
return end - 1
}
}
if f(rune) == truth {
return start
}
end = start
}
return -1
} }
func makeCutsetFunc(cutset string) func(rune int) bool { func makeCutsetFunc(cutset string) func(rune int) bool {
...@@ -436,19 +488,19 @@ func makeCutsetFunc(cutset string) func(rune int) bool { ...@@ -436,19 +488,19 @@ func makeCutsetFunc(cutset string) func(rune int) bool {
} }
// Trim returns a subslice of s by slicing off all leading and // Trim returns a subslice of s by slicing off all leading and
// trailing UTF-8 encoded Unicode code points contained in cutset. // trailing UTF-8-encoded Unicode code points contained in cutset.
func Trim(s []byte, cutset string) []byte { func Trim(s []byte, cutset string) []byte {
return TrimFunc(s, makeCutsetFunc(cutset)) return TrimFunc(s, makeCutsetFunc(cutset))
} }
// TrimLeft returns a subslice of s by slicing off all leading // TrimLeft returns a subslice of s by slicing off all leading
// UTF-8 encoded Unicode code points contained in cutset. // UTF-8-encoded Unicode code points contained in cutset.
func TrimLeft(s []byte, cutset string) []byte { func TrimLeft(s []byte, cutset string) []byte {
return TrimLeftFunc(s, makeCutsetFunc(cutset)) return TrimLeftFunc(s, makeCutsetFunc(cutset))
} }
// TrimRight returns a subslice of s by slicing off all trailing // TrimRight returns a subslice of s by slicing off all trailing
// UTF-8 encoded Unicode code points that are contained in cutset. // UTF-8-encoded Unicode code points that are contained in cutset.
func TrimRight(s []byte, cutset string) []byte { func TrimRight(s []byte, cutset string) []byte {
return TrimRightFunc(s, makeCutsetFunc(cutset)) return TrimRightFunc(s, makeCutsetFunc(cutset))
} }
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
. "bytes" . "bytes"
"testing" "testing"
"unicode" "unicode"
"utf8"
) )
func eq(a, b []string) bool { func eq(a, b []string) bool {
...@@ -367,8 +368,14 @@ var trimSpaceTests = []StringTest{ ...@@ -367,8 +368,14 @@ var trimSpaceTests = []StringTest{
StringTest{" \t\r\n x\t\t\r\r\n\n ", "x"}, StringTest{" \t\r\n x\t\t\r\r\n\n ", "x"},
StringTest{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"}, StringTest{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
StringTest{"1 \t\r\n2", "1 \t\r\n2"}, StringTest{"1 \t\r\n2", "1 \t\r\n2"},
StringTest{" x\x80", "x\x80"}, // invalid UTF-8 on end StringTest{" x\x80", "x\x80"},
StringTest{" x\xc0", "x\xc0"}, // invalid UTF-8 on end StringTest{" x\xc0", "x\xc0"},
StringTest{"x \xc0\xc0 ", "x \xc0\xc0"},
StringTest{"x \xc0", "x \xc0"},
StringTest{"x \xc0 ", "x \xc0"},
StringTest{"x \xc0\xc0 ", "x \xc0\xc0"},
StringTest{"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
StringTest{"x ☺ ", "x ☺"},
} }
// Bytes returns a new slice containing the bytes in s. // Bytes returns a new slice containing the bytes in s.
...@@ -607,6 +614,7 @@ var trimTests = []TrimTest{ ...@@ -607,6 +614,7 @@ var trimTests = []TrimTest{
TrimTest{TrimRight, "abba", "", "abba"}, TrimTest{TrimRight, "abba", "", "abba"},
TrimTest{TrimRight, "", "123", ""}, TrimTest{TrimRight, "", "123", ""},
TrimTest{TrimRight, "", "", ""}, TrimTest{TrimRight, "", "", ""},
TrimTest{TrimRight, "☺\xc0", "☺", "☺\xc0"},
} }
func TestTrim(t *testing.T) { func TestTrim(t *testing.T) {
...@@ -629,22 +637,90 @@ func TestTrim(t *testing.T) { ...@@ -629,22 +637,90 @@ func TestTrim(t *testing.T) {
} }
} }
type TrimFuncTest struct { type predicate struct {
f func(r int) bool f func(r int) bool
name, in, out string name string
}
var isSpace = predicate{unicode.IsSpace, "IsSpace"}
var isDigit = predicate{unicode.IsDigit, "IsDigit"}
var isUpper = predicate{unicode.IsUpper, "IsUpper"}
var isValidRune = predicate{
func(r int) bool {
return r != utf8.RuneError
},
"IsValidRune",
}
type TrimFuncTest struct {
f predicate
in, out string
}
func not(p predicate) predicate {
return predicate{
func(r int) bool {
return !p.f(r)
},
"not " + p.name,
}
} }
var trimFuncTests = []TrimFuncTest{ var trimFuncTests = []TrimFuncTest{
TrimFuncTest{unicode.IsSpace, "IsSpace", space + " hello " + space, "hello"}, TrimFuncTest{isSpace, space + " hello " + space, "hello"},
TrimFuncTest{unicode.IsDigit, "IsDigit", "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"}, TrimFuncTest{isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
TrimFuncTest{unicode.IsUpper, "IsUpper", "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"}, TrimFuncTest{isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
TrimFuncTest{not(isSpace), "hello" + space + "hello", space},
TrimFuncTest{not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
TrimFuncTest{isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
TrimFuncTest{not(isValidRune), "\xc0a\xc0", "a"},
} }
func TestTrimFunc(t *testing.T) { func TestTrimFunc(t *testing.T) {
for _, tc := range trimFuncTests { for _, tc := range trimFuncTests {
actual := string(TrimFunc([]byte(tc.in), tc.f)) actual := string(TrimFunc([]byte(tc.in), tc.f.f))
if actual != tc.out { if actual != tc.out {
t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.name, actual, tc.out) t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
}
}
}
type IndexFuncTest struct {
in string
f predicate
first, last int
}
var indexFuncTests = []IndexFuncTest{
IndexFuncTest{"", isValidRune, -1, -1},
IndexFuncTest{"abc", isDigit, -1, -1},
IndexFuncTest{"0123", isDigit, 0, 3},
IndexFuncTest{"a1b", isDigit, 1, 1},
IndexFuncTest{space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
IndexFuncTest{"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
IndexFuncTest{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
IndexFuncTest{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
// tests of invalid UTF-8
IndexFuncTest{"\x801", isDigit, 1, 1},
IndexFuncTest{"\x80abc", isDigit, -1, -1},
IndexFuncTest{"\xc0a\xc0", isValidRune, 1, 1},
IndexFuncTest{"\xc0a\xc0", not(isValidRune), 0, 2},
IndexFuncTest{"\xc0\xc0", not(isValidRune), 0, 4},
IndexFuncTest{"\xc0\xc0\xc0", not(isValidRune), 0, 5},
IndexFuncTest{"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
IndexFuncTest{"a\xe0\x80cd", not(isValidRune), 1, 2},
}
func TestIndexFunc(t *testing.T) {
for _, tc := range indexFuncTests {
first := IndexFunc([]byte(tc.in), tc.f.f)
if first != tc.first {
t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
}
last := LastIndexFunc([]byte(tc.in), tc.f.f)
if last != tc.last {
t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
} }
} }
} }
......
...@@ -422,8 +422,7 @@ func LastIndexFunc(s string, f func(r int) bool) int { ...@@ -422,8 +422,7 @@ func LastIndexFunc(s string, f func(r int) bool) int {
// indexFunc is the same as IndexFunc except that if // indexFunc is the same as IndexFunc except that if
// truth==false, the sense of the predicate function is // truth==false, the sense of the predicate function is
// inverted. We could use IndexFunc directly, but this // inverted.
// way saves a closure allocation.
func indexFunc(s string, f func(r int) bool, truth bool) int { func indexFunc(s string, f func(r int) bool, truth bool) int {
start := 0 start := 0
for start < len(s) { for start < len(s) {
...@@ -442,8 +441,7 @@ func indexFunc(s string, f func(r int) bool, truth bool) int { ...@@ -442,8 +441,7 @@ func indexFunc(s string, f func(r int) bool, truth bool) int {
// lastIndexFunc is the same as LastIndexFunc except that if // lastIndexFunc is the same as LastIndexFunc except that if
// truth==false, the sense of the predicate function is // truth==false, the sense of the predicate function is
// inverted. We could use IndexFunc directly, but this // inverted.
// way saves a closure allocation.
func lastIndexFunc(s string, f func(r int) bool, truth bool) int { func lastIndexFunc(s string, f func(r int) bool, truth bool) int {
end := len(s) end := len(s)
for end > 0 { for end > 0 {
......
...@@ -420,25 +420,6 @@ var trimTests = []TrimTest{ ...@@ -420,25 +420,6 @@ var trimTests = []TrimTest{
TrimTest{TrimRight, "☺\xc0", "☺", "☺\xc0"}, 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) { func TestTrim(t *testing.T) {
for _, tc := range trimTests { for _, tc := range trimTests {
actual := tc.f(tc.in, tc.cutset) actual := tc.f(tc.in, tc.cutset)
...@@ -456,16 +437,14 @@ func TestTrim(t *testing.T) { ...@@ -456,16 +437,14 @@ func TestTrim(t *testing.T) {
if actual != tc.out { 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.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 isSpace = predicate{unicode.IsSpace, "IsSpace"}
var isDigit = predicate{unicode.IsDigit, "IsDigit"} var isDigit = predicate{unicode.IsDigit, "IsDigit"}
var isUpper = predicate{unicode.IsUpper, "IsUpper"} var isUpper = predicate{unicode.IsUpper, "IsUpper"}
...@@ -476,11 +455,6 @@ var isValidRune = predicate{ ...@@ -476,11 +455,6 @@ var isValidRune = predicate{
"IsValidRune", "IsValidRune",
} }
type predicate struct {
f func(r int) bool
name string
}
type TrimFuncTest struct { type TrimFuncTest struct {
f predicate f predicate
in, out string in, out string
...@@ -530,7 +504,7 @@ var indexFuncTests = []IndexFuncTest{ ...@@ -530,7 +504,7 @@ var indexFuncTests = []IndexFuncTest{
IndexFuncTest{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34}, IndexFuncTest{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
IndexFuncTest{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12}, IndexFuncTest{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
// broken unicode tests // tests of invalid UTF-8
IndexFuncTest{"\x801", isDigit, 1, 1}, IndexFuncTest{"\x801", isDigit, 1, 1},
IndexFuncTest{"\x80abc", isDigit, -1, -1}, IndexFuncTest{"\x80abc", isDigit, -1, -1},
IndexFuncTest{"\xc0a\xc0", isValidRune, 1, 1}, IndexFuncTest{"\xc0a\xc0", isValidRune, 1, 1},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment