bytes, strings: IndexOfAny

+ first use in go/doc

R=r
CC=golang-dev
https://golang.org/cl/781041
This commit is contained in:
Robert Griesemer 2010-03-26 13:05:04 -07:00
parent 9e481e2905
commit d0ffee8abf
5 changed files with 102 additions and 28 deletions

View file

@ -106,6 +106,21 @@ func LastIndex(s, sep string) int {
return -1
}
// IndexAny returns the index of the first instance of any Unicode code point
// from chars in s, or -1 if no Unicode code point from chars is present in s.
func IndexAny(s, chars string) int {
if len(chars) > 0 {
for i, c := range s {
for _, m := range chars {
if c == m {
return i
}
}
}
}
return -1
}
// Generic split: splits after each instance of sep,
// including sepSave bytes of sep in the subarrays.
func genSplit(s, sep string, sepSave, n int) []string {