Skip to content

Commit 26f1870

Browse files
committed
fixed bug in reverse overlap elimination - added pretty printing for diff sets in tests
1 parent 5dbd4a7 commit 26f1870

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

diff/dmp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,9 +917,10 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
917917
float64(overlap_length2) >= float64(len(insertion))/2 {
918918
// Reverse overlap found.
919919
// Insert an equality and swap and trim the surrounding edits.
920+
overlap := Diff{DiffEqual, insertion[overlap_length2:]}
920921
diffs = append(
921922
diffs[:pointer],
922-
append([]Diff{Diff{DiffEqual, insertion[0:overlap_length2]}}, diffs[pointer:]...)...)
923+
append([]Diff{overlap}, diffs[pointer:]...)...)
923924
// diffs.splice(pointer, 0,
924925
// [DiffEqual, deletion[0 : overlap_length2)]]
925926
diffs[pointer-1].Type = DiffInsert

diff/dmp_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99
"runtime"
10+
"bytes"
1011

1112
"github.com/bmizerany/assert"
1213
)
@@ -25,6 +26,25 @@ func caller() string {
2526
return ""
2627
}
2728

29+
func pretty(diffs []Diff) string {
30+
var w bytes.Buffer
31+
for i, diff := range diffs {
32+
w.WriteString(fmt.Sprintf("%v. ",i))
33+
switch diff.Type {
34+
case DiffInsert:
35+
w.WriteString("DiffIns")
36+
case DiffDelete:
37+
w.WriteString("DiffDel")
38+
case DiffEqual:
39+
w.WriteString("DiffEql")
40+
default:
41+
w.WriteString("Unknown")
42+
}
43+
w.WriteString(fmt.Sprintf(": %v\n", diff.Text))
44+
}
45+
return w.String()
46+
}
47+
2848
func assertMapEqual(t *testing.T, seq1, seq2 interface{}) {
2949
v1 := reflect.ValueOf(seq1)
3050
k1 := v1.Kind()
@@ -63,6 +83,7 @@ func assertDiffEqual(t *testing.T, seq1, seq2 []Diff) {
6383

6484
for i := range seq1 {
6585
if a, b := seq1[i], seq2[i]; a != b {
86+
t.Errorf("%v\nseq1:\n%v\nseq2:\n%v", caller(), pretty(seq1), pretty(seq2))
6687
t.Fatalf("%v %v != %v", caller(), a, b)
6788
}
6889
}

0 commit comments

Comments
 (0)