Skip to content

Commit

Permalink
matchfinder.M4: factor out extendMatch2
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalholm committed Dec 31, 2023
1 parent 63f3f43 commit 0d2aef3
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions matchfinder/m4.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,13 @@ func (q *M4) FindMatches(dst []Match, src []byte) []Match {
continue
}

// We have a 4-byte match now.

start := i
match := candidate
end := extendMatch(src, match+4, start+4)
for start > e.NextEmit && match > 0 && src[start-1] == src[match-1] {
start--
match--
}
if end-start <= matches[0].End-matches[0].Start {
m := extendMatch2(src, i, candidate, e.NextEmit)
if m.End-m.Start <= matches[0].End-matches[0].Start {
continue
}

matches = [3]absoluteMatch{
absoluteMatch{
Start: start,
End: end,
Match: match,
},
m,
matches[0],
matches[1],
}
Expand Down Expand Up @@ -225,3 +213,18 @@ func extendMatch(src []byte, i, j int) int {
}
return j
}

// Given a 4-byte match at src[start] and src[candidate], extendMatch2 extends it
// upward as far as possible, and downward no farther than to min.
func extendMatch2(src []byte, start, candidate, min int) absoluteMatch {
end := extendMatch(src, candidate+4, start+4)
for start > min && candidate > 0 && src[start-1] == src[candidate-1] {
start--
candidate--
}
return absoluteMatch{
Start: start,
End: end,
Match: candidate,
}
}

0 comments on commit 0d2aef3

Please sign in to comment.