fix(parser/diff): only run myers diff on shown part
All checks were successful
submodules sync / sync (push) Successful in 46s
build / build (push) Successful in 2m6s
build / trigger-build-image (push) Successful in 13s

This commit is contained in:
张泊明518370910136 2025-03-25 07:30:17 -04:00
parent 63a4636341
commit 8e8c018f09
GPG Key ID: D47306D7062CDA9D
2 changed files with 16 additions and 9 deletions

View File

@ -52,6 +52,7 @@ func isWhitespace(b byte) bool {
// myersDiff computes the Myers' diff between two slices of strings.
// src: https://github.com/cj1128/myers-diff/blob/master/main.go
// TODO: it has O(n^2) time complexity
func myersDiff(src, dst []string, compareSpace bool) []operation {
n := len(src)
m := len(dst)

View File

@ -74,20 +74,26 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) (
comment += fmt.Sprintf("Difference found in `%s`\n",
output.FileName)
if !output.AlwaysHide {
// Convert answer to string and split by lines
stdoutLines := strings.Split(string(answer), "\n")
resultLines := strings.Split(
result.Files[output.FileName], "\n")
// Generate Myers diff
diffOps := myersDiff(stdoutLines, resultLines,
output.CompareSpace)
if output.MaxDiffLength == 0 { // real default value
output.MaxDiffLength = 2048
}
// Convert answer to string and split by lines
answerStr := string(answer)
resultStr := result.Files[output.FileName]
if len(answerStr) > output.MaxDiffLength {
answerStr = answerStr[:output.MaxDiffLength]
}
if len(resultStr) > output.MaxDiffLength {
resultStr = resultStr[:output.MaxDiffLength]
}
answerLines := strings.Split(answerStr, "\n")
resultLines := strings.Split(resultStr, "\n")
// Generate Myers diff
diffOps := myersDiff(answerLines, resultLines,
output.CompareSpace)
// Generate diff block with surrounding context
diffOutput := generateDiffWithContext(
stdoutLines,
answerLines,
resultLines,
diffOps,
output.MaxDiffLength,