From: Tobias Klauser Date: Wed, 4 Feb 2026 15:30:04 +0000 (+0100) Subject: internal/bytealg: unexport {Last,}IndexRabinKarp helpers X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f1e016bb3a98857f66140952279b088292a6021b;p=gostls13.git internal/bytealg: unexport {Last,}IndexRabinKarp helpers After CL 538737 these no longer need to be exported. They are only used in internal/bytealg and can be unexported. Change-Id: Idd405f397c7ec9f96425d2b7e0e74de61daa7a6e Reviewed-on: https://go-review.googlesource.com/c/go/+/741920 Reviewed-by: Keith Randall Reviewed-by: Michael Pratt Auto-Submit: Tobias Klauser LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall --- diff --git a/src/internal/bytealg/bytealg.go b/src/internal/bytealg/bytealg.go index 319ea54ba3..57a1e4ded5 100644 --- a/src/internal/bytealg/bytealg.go +++ b/src/internal/bytealg/bytealg.go @@ -29,17 +29,17 @@ const ( // If MaxLen is not 0, make sure MaxLen >= 4. var MaxLen int -// PrimeRK is the prime base used in Rabin-Karp algorithm. -const PrimeRK = 16777619 +// primeRK is the prime base used in Rabin-Karp algorithm. +const primeRK = 16777619 -// HashStr returns the hash and the appropriate multiplicative +// hashStr returns the hash and the appropriate multiplicative // factor for use in Rabin-Karp algorithm. -func HashStr[T string | []byte](sep T) (uint32, uint32) { +func hashStr[T string | []byte](sep T) (uint32, uint32) { hash := uint32(0) for i := 0; i < len(sep); i++ { - hash = hash*PrimeRK + uint32(sep[i]) + hash = hash*primeRK + uint32(sep[i]) } - var pow, sq uint32 = 1, PrimeRK + var pow, sq uint32 = 1, primeRK for i := len(sep); i > 0; i >>= 1 { if i&1 != 0 { pow *= sq @@ -49,14 +49,14 @@ func HashStr[T string | []byte](sep T) (uint32, uint32) { return hash, pow } -// HashStrRev returns the hash of the reverse of sep and the +// hashStrRev returns the hash of the reverse of sep and the // appropriate multiplicative factor for use in Rabin-Karp algorithm. -func HashStrRev[T string | []byte](sep T) (uint32, uint32) { +func hashStrRev[T string | []byte](sep T) (uint32, uint32) { hash := uint32(0) for i := len(sep) - 1; i >= 0; i-- { - hash = hash*PrimeRK + uint32(sep[i]) + hash = hash*primeRK + uint32(sep[i]) } - var pow, sq uint32 = 1, PrimeRK + var pow, sq uint32 = 1, primeRK for i := len(sep); i > 0; i >>= 1 { if i&1 != 0 { pow *= sq @@ -70,17 +70,17 @@ func HashStrRev[T string | []byte](sep T) (uint32, uint32) { // first occurrence of sep in s, or -1 if not present. func IndexRabinKarp[T string | []byte](s, sep T) int { // Rabin-Karp search - hashss, pow := HashStr(sep) + hashss, pow := hashStr(sep) n := len(sep) var h uint32 for i := 0; i < n; i++ { - h = h*PrimeRK + uint32(s[i]) + h = h*primeRK + uint32(s[i]) } if h == hashss && string(s[:n]) == string(sep) { return 0 } for i := n; i < len(s); { - h *= PrimeRK + h *= primeRK h += uint32(s[i]) h -= pow * uint32(s[i-n]) i++ @@ -95,18 +95,18 @@ func IndexRabinKarp[T string | []byte](s, sep T) int { // occurrence of sep in s, or -1 if not present. func LastIndexRabinKarp[T string | []byte](s, sep T) int { // Rabin-Karp search from the end of the string - hashss, pow := HashStrRev(sep) + hashss, pow := hashStrRev(sep) n := len(sep) last := len(s) - n var h uint32 for i := len(s) - 1; i >= last; i-- { - h = h*PrimeRK + uint32(s[i]) + h = h*primeRK + uint32(s[i]) } if h == hashss && string(s[last:]) == string(sep) { return last } for i := last - 1; i >= 0; i-- { - h *= PrimeRK + h *= primeRK h += uint32(s[i]) h -= pow * uint32(s[i+n]) if h == hashss && string(s[i:i+n]) == string(sep) {