Skip to content

Commit 987f9cb

Browse files
committed
added int min function. Improved pretty test fail output for assertDiffEqual. Cleaned up diffCompute func
1 parent f7e2296 commit 987f9cb

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

diff/dmp.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ func (dmp *DiffMatchPatch) diffCompute(text1 string, text2 string, checklines bo
290290
shorttext = text1
291291
}
292292

293-
var i = strings.Index(longtext, shorttext)
294-
if i != -1 {
293+
if i := strings.Index(longtext, shorttext); i != -1 {
295294
var op int8 = DiffInsert
296295
// Swap insertions for deletions if diff is reversed.
297296
if utf8.RuneCountInString(text1) > utf8.RuneCountInString(text2) {
@@ -305,18 +304,15 @@ func (dmp *DiffMatchPatch) diffCompute(text1 string, text2 string, checklines bo
305304
}
306305

307306
return diffs
308-
}
309-
if utf8.RuneCountInString(shorttext) == 1 {
307+
} else if utf8.RuneCountInString(shorttext) == 1 {
310308
// Single character string.
311309
// After the previous speedup, the character can't be an equality.
312310
return []Diff{
313311
Diff{DiffDelete, text1},
314312
Diff{DiffInsert, text2},
315313
}
316-
}
317314
// Check to see if the problem can be split in two.
318-
hm := dmp.DiffHalfMatch(text1, text2)
319-
if hm != nil {
315+
} else if hm := dmp.DiffHalfMatch(text1, text2); hm != nil {
320316
// A half-match was found, sort out the return data.
321317
text1_a := hm[0]
322318
text1_b := hm[1]
@@ -328,8 +324,7 @@ func (dmp *DiffMatchPatch) diffCompute(text1 string, text2 string, checklines bo
328324
diffs_b := dmp.DiffMain(text1_b, text2_b, checklines, deadline)
329325
// Merge the results.
330326
return append(diffs_a, append([]Diff{Diff{DiffEqual, mid_common}}, diffs_b...)...)
331-
}
332-
if checklines && utf8.RuneCountInString(text1) > 100 && utf8.RuneCountInString(text2) > 100 {
327+
} else if checklines && utf8.RuneCountInString(text1) > 100 && utf8.RuneCountInString(text2) > 100 {
333328
return dmp.diffLineMode(text1, text2, deadline)
334329
}
335330
return dmp.DiffBisect(text1, text2, deadline)
@@ -591,9 +586,16 @@ func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []
591586
return hydrated
592587
}
593588

589+
func min(x, y int) int {
590+
if x < y {
591+
return x
592+
}
593+
return y
594+
}
595+
594596
// DiffCommonPrefix determines the common prefix length of two strings.
595597
func (dmp *DiffMatchPatch) DiffCommonPrefix(text1 string, text2 string) int {
596-
n := int(math.Min(float64(len(text1)), float64(len(text2))))
598+
n := min(len(text1), len(text2))
597599
for i := 0; i < n; i++ {
598600
if text1[i] != text2[i] {
599601
return i
@@ -626,13 +628,13 @@ func (dmp *DiffMatchPatch) DiffCommonPrefix(text1 string, text2 string) int {
626628
func (dmp *DiffMatchPatch) DiffCommonSuffix(text1 string, text2 string) int {
627629
text1_length := len(text1)
628630
text2_length := len(text2)
629-
n := math.Min(float64(text1_length), float64(text2_length))
630-
for i := 1; i <= int(n); i++ {
631+
n := min(text1_length, text2_length)
632+
for i := 1; i <= n; i++ {
631633
if text1[text1_length-i] != text2[text2_length-i] {
632634
return i - 1
633635
}
634636
}
635-
return int(n)
637+
return n
636638
// Binary search.
637639
// Performance analysis: http://neil.fraser.name/news/2007/10/09/
638640
/*

diff/dmp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func assertMapEqual(t *testing.T, seq1, seq2 interface{}) {
7878

7979
func assertDiffEqual(t *testing.T, seq1, seq2 []Diff) {
8080
if a, b := len(seq1), len(seq2); a != b {
81+
t.Errorf("%v\nseq1:\n%v\nseq2:\n%v", caller(), pretty(seq1), pretty(seq2))
8182
t.Fatalf("%v Sequences of different length: %v != %v", caller(), a, b)
8283
}
8384

@@ -853,7 +854,6 @@ func Test_diffMain(t *testing.T) {
853854
Diff{DiffDelete, "\t"},
854855
Diff{DiffInsert, "\u0000"}}
855856

856-
assertDiffEqual(t, diffs, dmp.DiffMain("ax\t", "\u0680x\u0000", false))
857857
assertDiffEqual(t, diffs, dmp.DiffMain("ax\t", "\u0680x\u0000", false))
858858
diffs = []Diff{Diff{DiffDelete, "1"}, Diff{DiffEqual, "a"}, Diff{DiffDelete, "y"}, Diff{DiffEqual, "b"}, Diff{DiffDelete, "2"}, Diff{DiffInsert, "xab"}}
859859
assertDiffEqual(t, diffs, dmp.DiffMain("1ayb2", "abxab", false))

0 commit comments

Comments
 (0)