matchlen_generic.go 599 B

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