lib/scanner, vendor: Update github.com/chmduquesne/rollinghash (fixes #5334) (#5335)

Updates the package and fixes a test that depended on the old behavior
of Write() being equivalent to Reset()+Write() which is no longer the
case. The scanner already did resets after each block write, so this is
fine.
This commit is contained in:
Jakob Borg
2018-11-22 08:50:06 +01:00
committed by GitHub
parent d1704d5304
commit c0a26c918a
8 changed files with 172 additions and 107 deletions

View File

@@ -65,7 +65,7 @@ func New() *Buzhash64 {
func NewFromUint64Array(b [256]uint64) *Buzhash64 {
return &Buzhash64{
sum: 0,
window: make([]byte, 1, rollinghash.DefaultWindowCap),
window: make([]byte, 0, rollinghash.DefaultWindowCap),
oldest: 0,
bytehash: b,
}
@@ -77,30 +77,32 @@ func (d *Buzhash64) Size() int { return Size }
// BlockSize is 1 byte
func (d *Buzhash64) BlockSize() int { return 1 }
// Write (re)initializes the rolling window with the input byte slice and
// adds its data to the digest.
// Write appends data to the rolling window and updates the digest. It
// never returns an error.
func (d *Buzhash64) Write(data []byte) (int, error) {
// Copy the window, avoiding allocations where possible
l := len(data)
if l == 0 {
l = 1
return 0, nil
}
if len(d.window) != l {
if cap(d.window) >= l {
d.window = d.window[:l]
} else {
d.window = make([]byte, l)
}
// Re-arrange the window so that the leftmost element is at index 0
n := len(d.window)
if d.oldest != 0 {
tmp := make([]byte, d.oldest)
copy(tmp, d.window[:d.oldest])
copy(d.window, d.window[d.oldest:])
copy(d.window[n-d.oldest:], tmp)
d.oldest = 0
}
copy(d.window, data)
d.window = append(d.window, data...)
d.sum = 0
for _, c := range d.window {
d.sum = d.sum<<1 | d.sum>>63
d.sum ^= d.bytehash[int(c)]
}
d.nRotate = uint(len(d.window)) % 64
d.nRotateComplement = 64 - d.nRotate
return len(d.window), nil
return len(data), nil
}
// Sum64 returns the hash as a uint64
@@ -117,6 +119,13 @@ func (d *Buzhash64) Sum(b []byte) []byte {
// Roll updates the checksum of the window from the entering byte. You
// MUST initialize a window with Write() before calling this method.
func (d *Buzhash64) Roll(c byte) {
// This check costs 10-15% performance. If we disable it, we crash
// when the window is empty. If we enable it, we are always correct
// (an empty window never changes no matter how much you roll it).
//if len(d.window) == 0 {
// return
//}
// extract the entering/leaving bytes and update the circular buffer.
hn := d.bytehash[int(c)]
h0 := d.bytehash[int(d.window[d.oldest])]