]> Cypherpunks repositories - gostls13.git/commitdiff
strings: use bytealg.LastIndexRabinKarp on strings.LastIndex
authorjiahua wang <wjh180909@gmail.com>
Thu, 2 Nov 2023 03:51:46 +0000 (11:51 +0800)
committerGopher Robot <gobot@golang.org>
Thu, 22 Jan 2026 15:37:08 +0000 (07:37 -0800)
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>
src/strings/strings.go

index 80cea67d626d2e3445a29a4574fa3268de944763..56d4d07b014a4d666350293c468c927468e0929e 100644 (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.