@@ -225,17 +225,18 @@ func (dmp *DiffMatchPatch) DiffMain(text1 string, text2 string, opt ...interface
225
225
return diffs
226
226
}
227
227
228
+ // Trim off common prefix (speedup).
228
229
commonlength := dmp .DiffCommonPrefix (text1 , text2 )
229
- commonprefix := text1 [0 :commonlength ]
230
+ commonprefix := text1 [:commonlength ]
230
231
text1 = text1 [commonlength :]
231
232
text2 = text2 [commonlength :]
232
233
233
234
// Trim off common suffix (speedup).
234
235
commonlength = dmp .DiffCommonSuffix (text1 , text2 )
235
236
commonsuffix := text1 [len (text1 )- commonlength :]
236
237
237
- text1 = text1 [0 : len (text1 )- commonlength ]
238
- text2 = text2 [0 : len (text2 )- commonlength ]
238
+ text1 = text1 [: len (text1 )- commonlength ]
239
+ text2 = text2 [: len (text2 )- commonlength ]
239
240
240
241
// Compute the diff on the middle block.
241
242
diffs = dmp .diffCompute (text1 , text2 , checklines , deadline )
@@ -382,10 +383,10 @@ func (dmp *DiffMatchPatch) diffLineMode(text1 string, text2 string, deadline int
382
383
// See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
383
384
func (dmp * DiffMatchPatch ) DiffBisect (text1 string , text2 string , deadline int64 ) []Diff {
384
385
// Cache the text lengths to prevent multiple calls.
385
- text1_length := len (text1 )
386
- text2_length := len (text2 )
386
+ s1 , s2 := [] rune (text1 ), [] rune ( text2 )
387
+ s1_length , s2_length := len (s1 ), len ( s2 )
387
388
388
- max_d := int (math .Ceil (float64 (((text1_length + text2_length ) / 2 ))))
389
+ max_d := int (math .Ceil (float64 (((s1_length + s2_length ) / 2 ))))
389
390
v_offset := max_d
390
391
v_length := 2 * max_d
391
392
v1 := make ([]int , v_length )
@@ -394,7 +395,7 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
394
395
v1 [v_offset + 1 ] = 0
395
396
v2 [v_offset + 1 ] = 0
396
397
397
- delta := text1_length - text2_length
398
+ delta := s1_length - s2_length
398
399
// If the total number of characters is odd, then the front path will collide
399
400
// with the reverse path.
400
401
front := (delta % 2 != 0 )
@@ -422,26 +423,26 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
422
423
}
423
424
424
425
y1 := x1 - k1
425
- for x1 < text1_length && y1 < text2_length &&
426
- text1 [x1 ] == text2 [y1 ] {
426
+ for x1 < s1_length && y1 < s2_length &&
427
+ s1 [x1 ] == s2 [y1 ] {
427
428
x1 ++
428
429
y1 ++
429
430
}
430
431
v1 [k1_offset ] = x1
431
- if x1 > text1_length {
432
+ if x1 > s1_length {
432
433
// Ran off the right of the graph.
433
434
k1end += 2
434
- } else if y1 > text2_length {
435
+ } else if y1 > s2_length {
435
436
// Ran off the bottom of the graph.
436
437
k1start += 2
437
438
} else if front {
438
439
k2_offset := v_offset + delta - k1
439
440
if k2_offset >= 0 && k2_offset < v_length && v2 [k2_offset ] != - 1 {
440
441
// Mirror x2 onto top-left coordinate system.
441
- x2 := text1_length - v2 [k2_offset ]
442
+ x2 := s1_length - v2 [k2_offset ]
442
443
if x1 >= x2 {
443
444
// Overlap detected.
444
- return dmp .diffBisectSplit_ (text1 , text2 , x1 , y1 , deadline )
445
+ return dmp .diffBisectSplit_ (s1 , s2 , x1 , y1 , deadline )
445
446
}
446
447
}
447
448
}
@@ -456,17 +457,17 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
456
457
x2 = v2 [k2_offset - 1 ] + 1
457
458
}
458
459
var y2 = x2 - k2
459
- for x2 < text1_length &&
460
- y2 < text2_length &&
461
- (text1 [ text1_length - x2 - 1 ] == text2 [ text2_length - y2 - 1 ]) {
460
+ for x2 < s1_length &&
461
+ y2 < s2_length &&
462
+ (s1 [ s1_length - x2 - 1 ] == s2 [ s2_length - y2 - 1 ]) {
462
463
x2 ++
463
464
y2 ++
464
465
}
465
466
v2 [k2_offset ] = x2
466
- if x2 > text1_length {
467
+ if x2 > s1_length {
467
468
// Ran off the left of the graph.
468
469
k2end += 2
469
- } else if y2 > text2_length {
470
+ } else if y2 > s2_length {
470
471
// Ran off the top of the graph.
471
472
k2start += 2
472
473
} else if ! front {
@@ -475,10 +476,10 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
475
476
x1 := v1 [k1_offset ]
476
477
y1 := v_offset + x1 - k1_offset
477
478
// Mirror x2 onto top-left coordinate system.
478
- x2 = text1_length - x2
479
+ x2 = s1_length - x2
479
480
if x1 >= x2 {
480
481
// Overlap detected.
481
- return dmp .diffBisectSplit_ (text1 , text2 , x1 , y1 , deadline )
482
+ return dmp .diffBisectSplit_ (s1 , s2 , x1 , y1 , deadline )
482
483
}
483
484
}
484
485
}
@@ -492,11 +493,11 @@ func (dmp *DiffMatchPatch) DiffBisect(text1 string, text2 string, deadline int64
492
493
}
493
494
}
494
495
495
- func (dmp * DiffMatchPatch ) diffBisectSplit_ (text1 string , text2 string , x int , y int , deadline int64 ) []Diff {
496
- text1a := text1 [0 :x ]
497
- text2a := text2 [0 :y ]
498
- text1b := text1 [x :]
499
- text2b := text2 [y :]
496
+ func (dmp * DiffMatchPatch ) diffBisectSplit_ (text1 , text2 [] rune , x , y int , deadline int64 ) []Diff {
497
+ text1a := string ( text1 [:x ])
498
+ text2a := string ( text2 [:y ])
499
+ text1b := string ( text1 [x :])
500
+ text2b := string ( text2 [y :])
500
501
501
502
// Compute both diffs serially.
502
503
diffs := dmp .DiffMain (text1a , text2a , false , deadline )
@@ -587,26 +588,6 @@ func (dmp *DiffMatchPatch) DiffCommonPrefix(text1 string, text2 string) int {
587
588
}
588
589
}
589
590
return n
590
-
591
- // Binary search.
592
- // Performance analysis: http://neil.fraser.name/news/2007/10/09/
593
- /*
594
- pointermin := 0
595
- pointermax := math.Min(len(text1), len(text2))
596
- pointermid := pointermax
597
- pointerstart := 0
598
- for pointermin < pointermid {
599
- if text1[pointerstart:pointermid] ==
600
- text2[pointerstart:pointermid] {
601
- pointermin = pointermid
602
- pointerstart = pointermin
603
- } else {
604
- pointermax = pointermid
605
- }
606
- pointermid = math.Floor((pointermax-pointermin)/2 + pointermin)
607
- }
608
- return pointermid
609
- */
610
591
}
611
592
612
593
// DiffCommonSuffix determines the common suffix length of two strings.
@@ -2147,3 +2128,4 @@ func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
2147
2128
}
2148
2129
return patches , nil
2149
2130
}
2131
+
0 commit comments