github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/covermerger/lines_matcher.go (about) 1 // Copyright 2024 syzkaller project authors. All rights reserved. 2 // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. 3 4 package covermerger 5 6 import ( 7 "strings" 8 9 dmp "github.com/sergi/go-diff/diffmatchpatch" 10 ) 11 12 func makeLineToLineMatcher(textFrom, textTo string) *LineToLineMatcher { 13 diffMatcher := dmp.New() 14 diffMatcher.DiffTimeout = 0 15 diffs := diffMatcher.DiffMain(textFrom, textTo, false) 16 curToLinePos := 0 17 textDestPosToLine := map[int]int{} 18 for iLine, line := range strings.Split(textTo, "\n") { 19 textDestPosToLine[curToLinePos] = iLine 20 curToLinePos += len(line) + len("\n") 21 } 22 23 toLines := strings.Split(textTo, "\n") 24 curFromLinePos := 0 25 lineToLine := []int{} 26 for _, line := range strings.Split(textFrom, "\n") { 27 toLinePos := diffMatcher.DiffXIndex(diffs, curFromLinePos) 28 toLine := -1 29 if bestMatchDestLine, ok := textDestPosToLine[toLinePos]; ok { 30 if toLines[bestMatchDestLine] == line { 31 toLine = bestMatchDestLine 32 } 33 } 34 lineToLine = append(lineToLine, toLine) 35 curFromLinePos += len(line) + len("\n") 36 } 37 return &LineToLineMatcher{ 38 lineToLine: lineToLine, 39 } 40 } 41 42 type LineToLineMatcher struct { 43 lineToLine []int 44 } 45 46 func (lm *LineToLineMatcher) SameLinePos(line int) int { 47 return lm.lineToLine[line] 48 }