Skip to content

Commit f7fcaad

Browse files
authored
Merge pull request sergi#42 from sergi/28-handle-zero-deadlines-differently
Handle zero deadlines differently
2 parents 159219e + dc65385 commit f7fcaad

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

diffmatchpatch/dmp.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,13 @@ func New() *DiffMatchPatch {
250250

251251
// DiffMain finds the differences between two texts.
252252
func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff {
253-
var deadline time.Time
254-
if dmp.DiffTimeout <= 0 {
255-
deadline = time.Now().Add(24 * 365 * time.Hour)
256-
} else {
257-
deadline = time.Now().Add(dmp.DiffTimeout)
258-
}
259-
return dmp.diffMain(text1, text2, checklines, deadline)
260-
}
261-
262-
func (dmp *DiffMatchPatch) diffMain(text1, text2 string, checklines bool, deadline time.Time) []Diff {
263-
return dmp.diffMainRunes([]rune(text1), []rune(text2), checklines, deadline)
253+
return dmp.DiffMainRunes([]rune(text1), []rune(text2), checklines)
264254
}
265255

266256
// DiffMainRunes finds the differences between two rune sequences.
267257
func (dmp *DiffMatchPatch) DiffMainRunes(text1, text2 []rune, checklines bool) []Diff {
268258
var deadline time.Time
269-
if dmp.DiffTimeout <= 0 {
270-
deadline = time.Now().Add(24 * 365 * time.Hour)
271-
} else {
259+
if dmp.DiffTimeout > 0 {
272260
deadline = time.Now().Add(dmp.DiffTimeout)
273261
}
274262
return dmp.diffMainRunes(text1, text2, checklines, deadline)
@@ -387,6 +375,8 @@ func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time)
387375
pointer := 0
388376
countDelete := 0
389377
countInsert := 0
378+
379+
// NOTE: Rune slices are slower than using strings in this case.
390380
textDelete := ""
391381
textInsert := ""
392382

@@ -406,7 +396,7 @@ func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time)
406396
countDelete+countInsert)
407397

408398
pointer = pointer - countDelete - countInsert
409-
a := dmp.diffMain(textDelete, textInsert, false, deadline)
399+
a := dmp.diffMainRunes([]rune(textDelete), []rune(textInsert), false, deadline)
410400
for j := len(a) - 1; j >= 0; j-- {
411401
diffs = splice(diffs, pointer, 0, a[j])
412402
}
@@ -464,7 +454,7 @@ func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time)
464454
k2end := 0
465455
for d := 0; d < maxD; d++ {
466456
// Bail out if deadline is reached.
467-
if time.Now().After(deadline) {
457+
if !deadline.IsZero() && time.Now().After(deadline) {
468458
break
469459
}
470460

diffmatchpatch/dmp_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,18 +907,21 @@ func Test_diffBisect(t *testing.T) {
907907
// Since the resulting diff hasn't been normalized, it would be ok if
908908
// the insertion and deletion pairs are swapped.
909909
// If the order changes, tweak this test as required.
910-
diffs := []Diff{
910+
correctDiffs := []Diff{
911911
Diff{DiffDelete, "c"},
912912
Diff{DiffInsert, "m"},
913913
Diff{DiffEqual, "a"},
914914
Diff{DiffDelete, "t"},
915915
Diff{DiffInsert, "p"}}
916916

917-
assertDiffEqual(t, diffs, dmp.DiffBisect(a, b, time.Date(9999, time.December, 31, 23, 59, 59, 59, time.UTC)))
917+
assertDiffEqual(t, correctDiffs, dmp.DiffBisect(a, b, time.Date(9999, time.December, 31, 23, 59, 59, 59, time.UTC)))
918918

919919
// Timeout.
920-
diffs = []Diff{Diff{DiffDelete, "cat"}, Diff{DiffInsert, "map"}}
921-
assertDiffEqual(t, diffs, dmp.DiffBisect(a, b, time.Date(0001, time.January, 01, 00, 00, 00, 00, time.UTC)))
920+
diffs := []Diff{Diff{DiffDelete, "cat"}, Diff{DiffInsert, "map"}}
921+
assertDiffEqual(t, diffs, dmp.DiffBisect(a, b, time.Now().Add(time.Nanosecond)))
922+
923+
// Negative deadlines count as having infinite time.
924+
assertDiffEqual(t, correctDiffs, dmp.DiffBisect(a, b, time.Date(0001, time.January, 01, 00, 00, 00, 00, time.UTC)))
922925
}
923926

924927
func Test_diffMain(t *testing.T) {
@@ -1259,6 +1262,14 @@ func Test_patch_make(t *testing.T) {
12591262

12601263
patches = dmp.PatchMake("2016-09-01T03:07:14.807830741Z", "2016-09-01T03:07:15.154800781Z")
12611264
assert.Equal(t, "@@ -15,16 +15,16 @@\n 07:1\n+5.15\n 4\n-.\n 80\n+0\n 78\n-3074\n 1Z\n", dmp.PatchToText(patches), "patch_make: Corner case of #31 fixed by #32")
1265+
1266+
text1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut risus et enim consectetur convallis a non ipsum. Sed nec nibh cursus, interdum libero vel."
1267+
text2 = "Lorem a ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ut risus et enim consectetur convallis a non ipsum. Sed nec nibh cursus, interdum liberovel."
1268+
dmp2 := New()
1269+
dmp2.DiffTimeout = 0
1270+
diffs = dmp2.DiffMain(text1, text2, true)
1271+
patches = dmp2.PatchMake(text1, diffs)
1272+
assert.Equal(t, "@@ -1,14 +1,16 @@\n Lorem \n+a \n ipsum do\n@@ -148,13 +148,12 @@\n m libero\n- \n vel.\n", dmp2.PatchToText(patches), "patch_make: Corner case of #28 wrong patch with timeout of 0")
12621273
}
12631274

12641275
func Test_PatchSplitMax(t *testing.T) {

0 commit comments

Comments
 (0)