Skip to content

Commit ce24a72

Browse files
committed
fixed bug due to rounding/float/int conversion
1 parent 2e246ae commit ce24a72

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

diff/dmp.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,16 @@ func (dmp *DiffMatchPatch) DiffMain(text1 string, text2 string, opt ...interface
234234
// Trim off common suffix (speedup).
235235
commonlength = dmp.DiffCommonSuffix(text1, text2)
236236
commonsuffix := text1[len(text1)-commonlength:]
237-
238237
text1 = text1[:len(text1)-commonlength]
239238
text2 = text2[:len(text2)-commonlength]
240239

241240
// Compute the diff on the middle block.
242241
diffs = dmp.diffCompute(text1, text2, checklines, deadline)
242+
243243
// Restore the prefix and suffix.
244244
if len(commonprefix) != 0 {
245245
diffs = append([]Diff{Diff{DiffEqual, commonprefix}}, diffs...)
246246
}
247-
248247
if len(commonsuffix) != 0 {
249248
diffs = append(diffs, Diff{DiffEqual, commonsuffix})
250249
}
@@ -259,15 +258,12 @@ func (dmp *DiffMatchPatch) diffCompute(text1 string, text2 string, checklines bo
259258
if len(text1) == 0 {
260259
// Just add some text (speedup).
261260
return append(diffs, Diff{DiffInsert, text2})
262-
}
263-
264-
if len(text2) == 0 {
261+
} else if len(text2) == 0 {
265262
// Just delete some text (speedup).
266263
return append(diffs, Diff{DiffDelete, text1})
267264
}
268265

269266
var longtext, shorttext string
270-
271267
if utf8.RuneCountInString(text1) > utf8.RuneCountInString(text2) {
272268
longtext = text1
273269
shorttext = text2
@@ -279,14 +275,14 @@ func (dmp *DiffMatchPatch) diffCompute(text1 string, text2 string, checklines bo
279275
if i := strings.Index(longtext, shorttext); i != -1 {
280276
var op int8 = DiffInsert
281277
// Swap insertions for deletions if diff is reversed.
282-
if utf8.RuneCountInString(text1) > utf8.RuneCountInString(text2) {
278+
if len(text1) > len(text2) {
283279
op = DiffDelete
284280
}
285281
// Shorter text is inside the longer text (speedup).
286282
diffs = []Diff{
287-
Diff{op, longtext[0:i]},
283+
Diff{op, longtext[:i]},
288284
Diff{DiffEqual, shorttext},
289-
Diff{op, longtext[i+utf8.RuneCountInString(shorttext):]},
285+
Diff{op, longtext[i+len(shorttext):]},
290286
}
291287

292288
return diffs
@@ -386,7 +382,7 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
386382
s1, s2 := []rune(text1), []rune(text2)
387383
s1_length, s2_length := len(s1), len(s2)
388384

389-
max_d := int(math.Ceil(float64(((s1_length + s2_length) / 2))))
385+
max_d := (s1_length + s2_length + 1) / 2
390386
v_offset := max_d
391387
v_length := 2 * max_d
392388
v1 := make([]int, v_length)
@@ -413,7 +409,7 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
413409

414410
// Walk the front path one step.
415411
for k1 := -d + k1start; k1 <= d-k1end; k1 += 2 {
416-
var k1_offset = v_offset + k1
412+
k1_offset := v_offset + k1
417413
var x1 int
418414

419415
if k1 == -d || (k1 != d && v1[k1_offset-1] < v1[k1_offset+1]) {
@@ -680,16 +676,15 @@ func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string {
680676
shorttext = text1
681677
}
682678

683-
//TODO
684679
if len(longtext) < 4 || len(shorttext)*2 < len(longtext) {
685680
return nil // Pointless.
686681
}
687682

688683
// First check if the second quarter is the seed for a half-match.
689-
hm1 := dmp.diffHalfMatchI(longtext, shorttext, int(math.Ceil(float64(len(longtext)/4))))
684+
hm1 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+3)/4))
690685

691686
// Check again based on the third quarter.
692-
hm2 := dmp.diffHalfMatchI(longtext, shorttext, int(math.Ceil(float64(len(longtext)/2))))
687+
hm2 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+1)/2))
693688

694689
hm := []string{}
695690
if hm1 == nil && hm2 == nil {

diff/dmp_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,17 +891,17 @@ func Test_diffMain(t *testing.T) {
891891
a = a + a
892892
b = b + b
893893
}
894-
startTime := time.Now().Unix()
895-
startTime *= 1000
894+
startTime := time.Now()
896895
dmp.DiffMain(a, b)
897-
endTime := time.Now().Unix()
898-
endTime *= 1000
896+
endTime := time.Now()
897+
delta := endTime.Sub(startTime)
898+
expected := time.Duration(dmp.DiffTimeout) * time.Second
899899
// Test that we took at least the timeout period.
900-
assert.True(t, dmp.DiffTimeout*1000 <= float64(endTime-startTime), "")
900+
assert.True(t, delta >= expected, "")
901901
// Test that we didn't take forever (be forgiving).
902902
// Theoretically this test could fail very occasionally if the
903903
// OS task swaps or locks up for a second at the wrong moment.
904-
assert.True(t, dmp.DiffTimeout*1000*2 > float64(endTime-startTime), "")
904+
assert.True(t, delta < (expected * 2), "")
905905
dmp.DiffTimeout = 0
906906

907907
// Test the linemode speedup.

0 commit comments

Comments
 (0)