2023-12-29 04:01:08 +03:00
|
|
|
package matchfinder
|
|
|
|
|
2024-01-09 16:40:40 +03:00
|
|
|
// An absoluteMatch is like a Match, but it stores indexes into the byte
|
2023-12-29 04:01:08 +03:00
|
|
|
// stream instead of lengths.
|
2024-01-09 16:40:40 +03:00
|
|
|
type absoluteMatch struct {
|
2023-12-29 04:01:08 +03:00
|
|
|
// Start is the index of the first byte.
|
|
|
|
Start int
|
|
|
|
|
|
|
|
// End is the index of the byte after the last byte
|
|
|
|
// (so that End - Start = Length).
|
|
|
|
End int
|
|
|
|
|
|
|
|
// Match is the index of the previous data that matches
|
|
|
|
// (Start - Match = Distance).
|
|
|
|
Match int
|
|
|
|
}
|
|
|
|
|
|
|
|
// A matchEmitter manages the output of matches for a MatchFinder.
|
|
|
|
type matchEmitter struct {
|
|
|
|
// Dst is the destination slice that Matches are added to.
|
|
|
|
Dst []Match
|
|
|
|
|
|
|
|
// NextEmit is the index of the next byte to emit.
|
|
|
|
NextEmit int
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:40:40 +03:00
|
|
|
func (e *matchEmitter) emit(m absoluteMatch) {
|
2023-12-29 04:01:08 +03:00
|
|
|
e.Dst = append(e.Dst, Match{
|
|
|
|
Unmatched: m.Start - e.NextEmit,
|
|
|
|
Length: m.End - m.Start,
|
|
|
|
Distance: m.Start - m.Match,
|
|
|
|
})
|
|
|
|
e.NextEmit = m.End
|
|
|
|
}
|
2023-12-29 04:21:34 +03:00
|
|
|
|
|
|
|
// trim shortens m if it extends past maxEnd. Then if the length is at least
|
|
|
|
// minLength, the match is emitted.
|
2024-01-09 16:40:40 +03:00
|
|
|
func (e *matchEmitter) trim(m absoluteMatch, maxEnd int, minLength int) {
|
2023-12-29 04:21:34 +03:00
|
|
|
if m.End > maxEnd {
|
|
|
|
m.End = maxEnd
|
|
|
|
}
|
|
|
|
if m.End-m.Start >= minLength {
|
|
|
|
e.emit(m)
|
|
|
|
}
|
|
|
|
}
|