@@ -434,6 +434,7 @@ func (dmp *DiffMatchPatch) DiffCommonSuffix(text1, text2 string) int {
434
434
435
435
// commonPrefixLength returns the length of the common prefix of two rune slices.
436
436
func commonPrefixLength (text1 , text2 []rune ) int {
437
+ // Linear search. See comment in commonSuffixLength.
437
438
short , long := text1 , text2
438
439
if len (short ) > len (long ) {
439
440
short , long = long , short
@@ -448,34 +449,15 @@ func commonPrefixLength(text1, text2 []rune) int {
448
449
449
450
// commonSuffixLength returns the length of the common suffix of two rune slices.
450
451
func commonSuffixLength (text1 , text2 []rune ) int {
452
+ // Use linear search rather than the binary search discussed at https://neil.fraser.name/news/2007/10/09/.
453
+ // See discussion at https://github.com/sergi/go-diff/issues/54.
451
454
n := min (len (text1 ), len (text2 ))
452
455
for i := 0 ; i < n ; i ++ {
453
456
if text1 [len (text1 )- i - 1 ] != text2 [len (text2 )- i - 1 ] {
454
457
return i
455
458
}
456
459
}
457
460
return n
458
-
459
- // TODO research and benchmark this, why is it not activated? https://github.com/sergi/go-diff/issues/54
460
- // Binary search.
461
- // Performance analysis: http://neil.fraser.name/news/2007/10/09/
462
- /*
463
- pointermin := 0
464
- pointermax := math.Min(len(text1), len(text2))
465
- pointermid := pointermax
466
- pointerend := 0
467
- for pointermin < pointermid {
468
- if text1[len(text1)-pointermid:len(text1)-pointerend] ==
469
- text2[len(text2)-pointermid:len(text2)-pointerend] {
470
- pointermin = pointermid
471
- pointerend = pointermin
472
- } else {
473
- pointermax = pointermid
474
- }
475
- pointermid = math.Floor((pointermax-pointermin)/2 + pointermin)
476
- }
477
- return pointermid
478
- */
479
461
}
480
462
481
463
// DiffCommonOverlap determines if the suffix of one string is the prefix of another.
0 commit comments