github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/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 diffs := diffMatcher.DiffMain(textFrom, textTo, false) 15 curToLinePos := 0 16 textDestPosToLine := map[int]int{} 17 for iLine, line := range strings.Split(textTo, "\n") { 18 textDestPosToLine[curToLinePos] = iLine 19 curToLinePos += len(line) + len("\n") 20 } 21 22 toLines := strings.Split(textTo, "\n") 23 curFromLinePos := 0 24 lineToLine := []int{} 25 for _, line := range strings.Split(textFrom, "\n") { 26 toLinePos := diffMatcher.DiffXIndex(diffs, curFromLinePos) 27 toLine := -1 28 if bestMatchDestLine, ok := textDestPosToLine[toLinePos]; ok { 29 if toLines[bestMatchDestLine] == line { 30 toLine = bestMatchDestLine 31 } 32 } 33 lineToLine = append(lineToLine, toLine) 34 curFromLinePos += len(line) + len("\n") 35 } 36 return &LineToLineMatcher{ 37 lineToLine: lineToLine, 38 } 39 } 40 41 type LineToLineMatcher struct { 42 lineToLine []int 43 } 44 45 func (lm *LineToLineMatcher) SameLinePos(line int) int { 46 return lm.lineToLine[line] 47 }