github.com/searKing/golang/go@v1.2.74/bytes/count.go (about) 1 package bytes 2 3 import "bytes" 4 5 // CountIndex counts the number of non-overlapping instances of sep in s. 6 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s. 7 func CountIndex(s, sep []byte) (c, index int) { 8 n := 0 9 lastIndex := -1 10 for { 11 i := bytes.Index(s, sep) 12 if i == -1 { 13 return n, lastIndex 14 } 15 n++ 16 lastIndex = i 17 s = s[i+len(sep):] 18 } 19 }