mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
add bytes.IndexByte; common case we can make fast later.
also pick off the special case in strings.Index. don't want strings.IndexByte because the call site will very rarely need to allocate and we can handle the test in the code itself. bytes.IndexByte can avoid a common allocation. R=rsc CC=golang-dev https://golang.org/cl/156091
This commit is contained in:
parent
1a8ebcc4b8
commit
8c1a627e5c
4 changed files with 96 additions and 17 deletions
|
|
@ -98,6 +98,16 @@ func Index(s, sep []byte) int {
|
|||
return -1;
|
||||
}
|
||||
|
||||
// IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
|
||||
func IndexByte(s []byte, c byte) int {
|
||||
for i, b := range s {
|
||||
if b == c {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
|
||||
func LastIndex(s, sep []byte) int {
|
||||
n := len(sep);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue