emitter.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package matchfinder
  2. // An absoluteMatch is like a Match, but it stores indexes into the byte
  3. // stream instead of lengths.
  4. type absoluteMatch struct {
  5. // Start is the index of the first byte.
  6. Start int
  7. // End is the index of the byte after the last byte
  8. // (so that End - Start = Length).
  9. End int
  10. // Match is the index of the previous data that matches
  11. // (Start - Match = Distance).
  12. Match int
  13. }
  14. // A matchEmitter manages the output of matches for a MatchFinder.
  15. type matchEmitter struct {
  16. // Dst is the destination slice that Matches are added to.
  17. Dst []Match
  18. // NextEmit is the index of the next byte to emit.
  19. NextEmit int
  20. }
  21. func (e *matchEmitter) emit(m absoluteMatch) {
  22. e.Dst = append(e.Dst, Match{
  23. Unmatched: m.Start - e.NextEmit,
  24. Length: m.End - m.Start,
  25. Distance: m.Start - m.Match,
  26. })
  27. e.NextEmit = m.End
  28. }
  29. // trim shortens m if it extends past maxEnd. Then if the length is at least
  30. // minLength, the match is emitted.
  31. func (e *matchEmitter) trim(m absoluteMatch, maxEnd int, minLength int) {
  32. if m.End > maxEnd {
  33. m.End = maxEnd
  34. }
  35. if m.End-m.Start >= minLength {
  36. e.emit(m)
  37. }
  38. }