Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use common lineHash to share indice between text1 and text2 (#5)
Use common cache of line contents between two texts in `DiffLinesToChars` to get line diffs correctly. In some cases, line diffs cannot be retrieved correctly in the standard way (https://github.com/google/diff-match-patch/wiki/Line-or-Word-Diffs#line-mode). In the below case, we failed to get line diffs correctly before this fix. ```go:main.go package main import ( "fmt" "github.com/sergi/go-diff/diffmatchpatch" ) const ( text1 = `hoge: step11: - arrayitem1 - arrayitem2 step12: step21: hoge step22: -93 fuga: flatitem ` text2 = `hoge: step11: - arrayitem4 - arrayitem2 - arrayitem3 step12: step21: hoge step22: -92 fuga: flatitem ` ) func main() { dmp := diffmatchpatch.New() a, b, c := dmp.DiffLinesToChars(text1, text2) diffs := dmp.DiffMain(a, b, false) diffs = dmp.DiffCharsToLines(diffs, c) // diffs = dmp.DiffCleanupSemantic(diffs) fmt.Println(diffs) } ``` ```text:output [{Insert hoge: step11: hoge: } {Equal hoge: } {Insert hoge: } {Equal step11: } {Insert hoge: } {Equal - arrayitem1 } {Insert hoge: } {Equal - arrayitem2 } {Insert hoge: } {Equal step12: } {Insert hoge: } {Equal step21: hoge } {Insert hoge: } {Equal step22: -93 } {Delete fuga: flatitem }] ``` Note: This fix corresponds to a javascript implementation. (ref: https://github.com/google/diff-match-patch/blob/62f2e689f498f9c92dbc588c58750addec9b1654/javascript/diff_match_patch_uncompressed.js#L466) Co-authored-by: nrnrk <[email protected]>
- Loading branch information