fix(parser/diff): only run myers diff on shown part
This commit is contained in:
		
							parent
							
								
									63a4636341
								
							
						
					
					
						commit
						8e8c018f09
					
				|  | @ -52,6 +52,7 @@ func isWhitespace(b byte) bool { | ||||||
| 
 | 
 | ||||||
| // myersDiff computes the Myers' diff between two slices of strings.
 | // myersDiff computes the Myers' diff between two slices of strings.
 | ||||||
| // src: https://github.com/cj1128/myers-diff/blob/master/main.go
 | // 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 { | func myersDiff(src, dst []string, compareSpace bool) []operation { | ||||||
| 	n := len(src) | 	n := len(src) | ||||||
| 	m := len(dst) | 	m := len(dst) | ||||||
|  |  | ||||||
|  | @ -74,20 +74,26 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) ( | ||||||
| 					comment += fmt.Sprintf("Difference found in `%s`\n", | 					comment += fmt.Sprintf("Difference found in `%s`\n", | ||||||
| 						output.FileName) | 						output.FileName) | ||||||
| 					if !output.AlwaysHide { | 					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
 | 						if output.MaxDiffLength == 0 { // real default value
 | ||||||
| 							output.MaxDiffLength = 2048 | 							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
 | 						// Generate diff block with surrounding context
 | ||||||
| 						diffOutput := generateDiffWithContext( | 						diffOutput := generateDiffWithContext( | ||||||
| 							stdoutLines, | 							answerLines, | ||||||
| 							resultLines, | 							resultLines, | ||||||
| 							diffOps, | 							diffOps, | ||||||
| 							output.MaxDiffLength, | 							output.MaxDiffLength, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user