strings: use bytealg.LastIndexRabinKarp on strings.LastIndex

Change-Id: I7eae15bf0b4d556763e1754e17031c880035d69c
Reviewed-on: https://go-review.googlesource.com/c/go/+/538737
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
This commit is contained in:
jiahua wang 2023-11-02 11:51:46 +08:00 committed by Gopher Robot
parent 0f9cdc2fbc
commit fde15bbfc1

View file

@ -94,25 +94,7 @@ func LastIndex(s, substr string) int {
case n > len(s):
return -1
}
// Rabin-Karp search from the end of the string
hashss, pow := bytealg.HashStrRev(substr)
last := len(s) - n
var h uint32
for i := len(s) - 1; i >= last; i-- {
h = h*bytealg.PrimeRK + uint32(s[i])
}
if h == hashss && s[last:] == substr {
return last
}
for i := last - 1; i >= 0; i-- {
h *= bytealg.PrimeRK
h += uint32(s[i])
h -= pow * uint32(s[i+n])
if h == hashss && s[i:i+n] == substr {
return i
}
}
return -1
return bytealg.LastIndexRabinKarp(s, substr)
}
// IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.