gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
package brotli

import (
	"encoding/binary"
	"math/bits"
	"runtime"
)

/* Copyright 2010 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

/* Function to find maximal matching prefixes of strings. */
func findMatchLengthWithLimit(s1 []byte, s2 []byte, limit uint) uint {
	var matched uint = 0
	_, _ = s1[limit-1], s2[limit-1] // bounds check
	switch runtime.GOARCH {
	case "amd64":
		// Compare 8 bytes at at time.
		for matched+8 <= limit {
			w1 := binary.LittleEndian.Uint64(s1[matched:])
			w2 := binary.LittleEndian.Uint64(s2[matched:])
			if w1 != w2 {
				return matched + uint(bits.TrailingZeros64(w1^w2)>>3)
			}
			matched += 8
		}
	case "386":
		// Compare 4 bytes at at time.
		for matched+4 <= limit {
			w1 := binary.LittleEndian.Uint32(s1[matched:])
			w2 := binary.LittleEndian.Uint32(s2[matched:])
			if w1 != w2 {
				return matched + uint(bits.TrailingZeros32(w1^w2)>>3)
			}
			matched += 4
		}
	}
	for matched < limit && s1[matched] == s2[matched] {
		matched++
	}
	return matched
}