1
0
mirror of https://github.com/golang/go synced 2024-09-29 19:14:28 -06:00

bytes: narrow the search of IndexByte in Index

This commit is contained in:
Andy Pan 2020-03-22 10:51:21 +08:00
parent 4d5bb9c609
commit 8576f1931d

View File

@ -1019,11 +1019,11 @@ func Index(s, sep []byte) int {
if s[i] != c0 { if s[i] != c0 {
// IndexByte is faster than bytealg.Index, so use it as long as // IndexByte is faster than bytealg.Index, so use it as long as
// we're not getting lots of false positives. // we're not getting lots of false positives.
o := IndexByte(s[i:t], c0) o := IndexByte(s[i+1:t], c0)
if o < 0 { if o < 0 {
return -1 return -1
} }
i += o i += o + 1
} }
if s[i+1] == c1 && Equal(s[i:i+n], sep) { if s[i+1] == c1 && Equal(s[i:i+n], sep) {
return i return i
@ -1048,11 +1048,11 @@ func Index(s, sep []byte) int {
t := len(s) - n + 1 t := len(s) - n + 1
for i < t { for i < t {
if s[i] != c0 { if s[i] != c0 {
o := IndexByte(s[i:t], c0) o := IndexByte(s[i+1:t], c0)
if o < 0 { if o < 0 {
break break
} }
i += o i += o + 1
} }
if s[i+1] == c1 && Equal(s[i:i+n], sep) { if s[i+1] == c1 && Equal(s[i:i+n], sep) {
return i return i