From fde15bbfc1ed51c9ec8c0f8d3d610851d1a690d0 Mon Sep 17 00:00:00 2001 From: jiahua wang Date: Thu, 2 Nov 2023 11:51:46 +0800 Subject: [PATCH] strings: use bytealg.LastIndexRabinKarp on strings.LastIndex Change-Id: I7eae15bf0b4d556763e1754e17031c880035d69c Reviewed-on: https://go-review.googlesource.com/c/go/+/538737 Auto-Submit: Sean Liao Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Cherry Mui Reviewed-by: Sean Liao --- src/strings/strings.go | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/strings/strings.go b/src/strings/strings.go index 80cea67d62..56d4d07b01 100644 --- a/src/strings/strings.go +++ b/src/strings/strings.go @@ -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. -- 2.52.0