Skip to content

Commit 25bc765

Browse files
committed
added type-safe wrapper DiffMain around private diffMain - no more interface{} args. max int from math package.
1 parent 24c1575 commit 25bc765

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

diff/dmp.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const (
3737
DiffDelete = -1
3838
DiffInsert = 1
3939
DiffEqual = 0
40-
max64 = int64(uint64(1<<63) - 1)
4140
)
4241

4342
// unescaper unescapes selected chars for compatability with JavaScript's encodeURI.
@@ -199,24 +198,18 @@ func New() *DiffMatchPatch {
199198
}
200199

201200
// DiffMain finds the differences between two texts.
202-
func (dmp *DiffMatchPatch) DiffMain(text1 string, text2 string, opt ...interface{}) []Diff {
203-
checklines := true
201+
func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff {
204202
var deadline time.Time
205-
206-
if opt != nil && len(opt) > 0 {
207-
checklines = opt[0].(bool)
208-
209-
if len(opt) > 1 {
210-
deadline = opt[1].(time.Time)
211-
} else {
212-
if dmp.DiffTimeout <= 0 {
213-
deadline = time.Now().Add(24*365*time.Hour)
214-
} else {
215-
deadline = time.Now().Add(dmp.DiffTimeout)
216-
}
217-
}
203+
if dmp.DiffTimeout <= 0 {
204+
deadline = time.Now().Add(24*365*time.Hour)
205+
} else {
206+
deadline = time.Now().Add(dmp.DiffTimeout)
218207
}
208+
return dmp.diffMain(text1, text2, checklines, deadline)
209+
}
219210

211+
// DiffMain finds the differences between two texts.
212+
func (dmp *DiffMatchPatch) diffMain(text1, text2 string, checklines bool, deadline time.Time) []Diff {
220213
diffs := []Diff{}
221214
if text1 == text2 {
222215
if len(text1) > 0 {
@@ -303,8 +296,8 @@ checklines bool, deadline time.Time) []Diff {
303296
text2_b := hm[3]
304297
mid_common := hm[4]
305298
// Send both pairs off for separate processing.
306-
diffs_a := dmp.DiffMain(text1_a, text2_a, checklines, deadline)
307-
diffs_b := dmp.DiffMain(text1_b, text2_b, checklines, deadline)
299+
diffs_a := dmp.diffMain(text1_a, text2_a, checklines, deadline)
300+
diffs_b := dmp.diffMain(text1_b, text2_b, checklines, deadline)
308301
// Merge the results.
309302
return append(diffs_a, append([]Diff{Diff{DiffEqual, mid_common}}, diffs_b...)...)
310303
} else if checklines && utf8.RuneCountInString(text1) > 100 && utf8.RuneCountInString(text2) > 100 {
@@ -320,7 +313,7 @@ deadline time.Time) []Diff {
320313
// Scan the text on a line-by-line basis first.
321314
text1, text2, linearray := dmp.DiffLinesToChars(text1, text2)
322315

323-
diffs := dmp.DiffMain(text1, text2, false, deadline)
316+
diffs := dmp.diffMain(text1, text2, false, deadline)
324317

325318
// Convert the diff back to original text.
326319
diffs = dmp.DiffCharsToLines(diffs, linearray)
@@ -353,7 +346,7 @@ deadline time.Time) []Diff {
353346
count_delete+count_insert)
354347

355348
pointer = pointer - count_delete - count_insert
356-
a := dmp.DiffMain(text_delete, text_insert, false, deadline)
349+
a := dmp.diffMain(text_delete, text_insert, false, deadline)
357350
for j := len(a) - 1; j >= 0; j-- {
358351
diffs = splice(diffs, pointer, 0, a[j])
359352
}
@@ -494,8 +487,8 @@ deadline time.Time) []Diff {
494487
text2b := string(text2[y:])
495488

496489
// Compute both diffs serially.
497-
diffs := dmp.DiffMain(text1a, text2a, false, deadline)
498-
diffsb := dmp.DiffMain(text1b, text2b, false, deadline)
490+
diffs := dmp.diffMain(text1a, text2a, false, deadline)
491+
diffsb := dmp.diffMain(text1b, text2b, false, deadline)
499492

500493
return append(diffs, diffsb...)
501494
}

diff/dmp_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,9 @@ func Test_diffMain(t *testing.T) {
891891
a = a + a
892892
b = b + b
893893
}
894+
894895
startTime := time.Now()
895-
dmp.DiffMain(a, b)
896+
dmp.DiffMain(a, b, true)
896897
endTime := time.Now()
897898
delta := endTime.Sub(startTime)
898899
// Test that we took at least the timeout period.

0 commit comments

Comments
 (0)