]> Cypherpunks repositories - gostls13.git/commitdiff
internal/bytealg: unexport {Last,}IndexRabinKarp helpers
authorTobias Klauser <tklauser@distanz.ch>
Wed, 4 Feb 2026 15:30:04 +0000 (16:30 +0100)
committerGopher Robot <gobot@golang.org>
Fri, 6 Feb 2026 23:30:48 +0000 (15:30 -0800)
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 <khr@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
src/internal/bytealg/bytealg.go

index 319ea54ba3c77f0dba127c9fb909b82551fba5da..57a1e4ded5aa553bdb590f994747b1bdbb49a8e2 100644 (file)
@@ -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) {