summaryrefslogtreecommitdiff
path: root/vendor/github.com/klauspost/compress/flate/matchlen_generic.go
blob: 6149384aaf9fec52cd3dfd9f4162e6dd3e09ce85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2019+ Klaus Post. All rights reserved.
// License information can be found in the LICENSE file.

package flate

import (
	"math/bits"

	"github.com/klauspost/compress/internal/le"
)

// matchLen returns the maximum common prefix length of a and b.
// a must be the shortest of the two.
func matchLen(a, b []byte) (n int) {
	left := len(a)
	for left >= 8 {
		diff := le.Load64(a, n) ^ le.Load64(b, n)
		if diff != 0 {
			return n + bits.TrailingZeros64(diff)>>3
		}
		n += 8
		left -= 8
	}

	a = a[n:]
	b = b[n:]
	for i := range a {
		if a[i] != b[i] {
			break
		}
		n++
	}
	return n
}