diff options
| author | Jes Cok <xigua67damn@gmail.com> | 2023-10-31 18:34:07 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-11-01 19:02:57 +0000 |
| commit | a05a25cb19d1ea222e37e7172fe489d972c0ec67 (patch) | |
| tree | 34d80bea46507629359a36e3fbe20c6d716620bd /src/bytes | |
| parent | 0330aad03839fe24d15b1f4b012e908ae3b4614d (diff) | |
| download | go-a05a25cb19d1ea222e37e7172fe489d972c0ec67.tar.xz | |
bytes,internal/bytealg: add func bytealg.LastIndexRabinKarp
Also rename 'substr' to 'sep' in IndexRabinKarp for consistency.
Change-Id: Icc2ad1116aecaf002c8264daa2fa608306c9a88a
GitHub-Last-Rev: 1784b93f53d569991f86585f9011120ea26f193f
GitHub-Pull-Request: golang/go#63854
Reviewed-on: https://go-review.googlesource.com/c/go/+/538716
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/bytes')
| -rw-r--r-- | src/bytes/bytes.go | 20 |
1 files changed, 1 insertions, 19 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index c84accd8f5..0679b43a20 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -121,25 +121,7 @@ func LastIndex(s, sep []byte) int { case n > len(s): return -1 } - // Rabin-Karp search from the end of the string - hashss, pow := bytealg.HashStrRev(sep) - 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 && Equal(s[last:], sep) { - 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 && Equal(s[i:i+n], sep) { - return i - } - } - return -1 + return bytealg.LastIndexRabinKarp(s, sep) } // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s. |
