// Copyright 2018 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package bytealgimport ()// Offsets into internal/cpu records for use in assembly.const ( offsetX86HasSSE42 = unsafe.Offsetof(cpu.X86.HasSSE42) offsetX86HasAVX2 = unsafe.Offsetof(cpu.X86.HasAVX2) offsetX86HasPOPCNT = unsafe.Offsetof(cpu.X86.HasPOPCNT) offsetS390xHasVX = unsafe.Offsetof(cpu.S390X.HasVX) offsetPPC64HasPOWER9 = unsafe.Offsetof(cpu.PPC64.IsPOWER9))// MaxLen is the maximum length of the string to be searched for (argument b) in Index.// If MaxLen is not 0, make sure MaxLen >= 4.varMaxLenint// PrimeRK is the prime base used in Rabin-Karp algorithm.constPrimeRK = 16777619// HashStr returns the hash and the appropriate multiplicative// factor for use in Rabin-Karp algorithm.func [ string | []byte]( ) (uint32, uint32) { := uint32(0)for := 0; < len(); ++ { = *PrimeRK + uint32([]) }var , uint32 = 1, PrimeRKfor := len(); > 0; >>= 1 {if &1 != 0 { *= } *= }return , }// HashStrRev returns the hash of the reverse of sep and the// appropriate multiplicative factor for use in Rabin-Karp algorithm.func [ string | []byte]( ) (uint32, uint32) { := uint32(0)for := len() - 1; >= 0; -- { = *PrimeRK + uint32([]) }var , uint32 = 1, PrimeRKfor := len(); > 0; >>= 1 {if &1 != 0 { *= } *= }return , }// IndexRabinKarp uses the Rabin-Karp search algorithm to return the index of the// first occurrence of sep in s, or -1 if not present.func [ string | []byte](, ) int {// Rabin-Karp search , := HashStr() := len()varuint32for := 0; < ; ++ { = *PrimeRK + uint32([]) }if == && string([:]) == string() {return0 }for := ; < len(); { *= PrimeRK += uint32([]) -= * uint32([-]) ++if == && string([-:]) == string() {return - } }return -1}// LastIndexRabinKarp uses the Rabin-Karp search algorithm to return the last index of the// occurrence of sep in s, or -1 if not present.func [ string | []byte](, ) int {// Rabin-Karp search from the end of the string , := HashStrRev() := len() := len() - varuint32for := len() - 1; >= ; -- { = *PrimeRK + uint32([]) }if == && string([:]) == string() {return }for := - 1; >= 0; -- { *= PrimeRK += uint32([]) -= * uint32([+])if == && string([:+]) == string() {return } }return -1}// MakeNoZero makes a slice of length n and capacity of at least n Bytes// without zeroing the bytes (including the bytes between len and cap).// It is the caller's responsibility to ensure uninitialized bytes// do not leak to the end user.func ( int) []byte
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.