blob: 6149384aaf9fec52cd3dfd9f4162e6dd3e09ce85 [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001// Copyright 2019+ Klaus Post. All rights reserved.
2// License information can be found in the LICENSE file.
3
4package flate
5
6import (
7 "math/bits"
8
9 "github.com/klauspost/compress/internal/le"
10)
11
12// matchLen returns the maximum common prefix length of a and b.
13// a must be the shortest of the two.
14func matchLen(a, b []byte) (n int) {
15 left := len(a)
16 for left >= 8 {
17 diff := le.Load64(a, n) ^ le.Load64(b, n)
18 if diff != 0 {
19 return n + bits.TrailingZeros64(diff)>>3
20 }
21 n += 8
22 left -= 8
23 }
24
25 a = a[n:]
26 b = b[n:]
27 for i := range a {
28 if a[i] != b[i] {
29 break
30 }
31 n++
32 }
33 return n
34}