mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes, strings: add LastIndexByte
Currently the packages have the following index functions: func Index(s, sep []byte) int func IndexAny(s []byte, chars string) int func IndexByte(s []byte, c byte) int func IndexFunc(s []byte, f func(r rune) bool) int func IndexRune(s []byte, r rune) int func LastIndex(s, sep []byte) int func LastIndexAny(s []byte, chars string) int func LastIndexFunc(s []byte, f func(r rune) bool) int Searching for the last occurrence of a byte is quite common for string parsing algorithms (e.g. find the last paren on a line). Also addition of LastIndexByte makes the set more orthogonal. Change-Id: Ida168849acacf8e78dd70c1354bef9eac5effafe Reviewed-on: https://go-review.googlesource.com/9500 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
89454b1c39
commit
0fb5475bdf
4 changed files with 54 additions and 0 deletions
|
|
@ -120,6 +120,23 @@ func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", l
|
|||
func TestIndexAny(t *testing.T) { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
|
||||
func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
|
||||
|
||||
func TestLastIndexByte(t *testing.T) {
|
||||
testCases := []IndexTest{
|
||||
{"", "q", -1},
|
||||
{"abcdef", "q", -1},
|
||||
{"abcdefabcdef", "a", len("abcdef")}, // something in the middle
|
||||
{"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
|
||||
{"zabcdefabcdef", "z", 0}, // first byte
|
||||
{"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
|
||||
}
|
||||
for _, test := range testCases {
|
||||
actual := LastIndexByte(test.s, test.sep[0])
|
||||
if actual != test.out {
|
||||
t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.s, test.sep[0], actual, test.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var indexRuneTests = []struct {
|
||||
s string
|
||||
rune rune
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue