1
- use std:: cmp:: max;
1
+ use std:: cmp:: { max, min } ;
2
2
3
3
use syntect:: highlighting:: StyleModifier ;
4
4
@@ -48,7 +48,6 @@ pub fn get_diff_style_sections(
48
48
49
49
for ( minus, plus) in minus_lines. iter ( ) . zip ( plus_lines. iter ( ) ) {
50
50
let string_pair = StringPair :: new ( minus, plus) ;
51
- let change_begin = string_pair. common_prefix_length ;
52
51
53
52
// We require that (right-trimmed length) >= (common prefix length). Consider:
54
53
// minus = "a "
@@ -58,17 +57,18 @@ pub fn get_diff_style_sections(
58
57
let minus_length = max ( string_pair. lengths [ 0 ] , string_pair. common_prefix_length ) ;
59
58
let plus_length = max ( string_pair. lengths [ 1 ] , string_pair. common_prefix_length ) ;
60
59
61
- // We require that change_begin <= change_end. Consider:
62
- // minus = "a c"
63
- // plus = "a b c"
64
- // Here, the common prefix length is 2, and the common suffix length is 2, yet the
65
- // length of minus is 3. This overlap between prefix and suffix leads to a violation of
66
- // the requirement. We resolve this by taking the following maxima:
67
- let minus_change_end = max (
68
- minus_length - string_pair. common_suffix_length ,
69
- change_begin,
60
+ // Work backwards from the end of the strings. The end of the
61
+ // change region is equal to the start of their common
62
+ // suffix. To find the start of the change region, start with
63
+ // the end of their common prefix, and then move leftwards
64
+ // until it is before the start of the common suffix in both
65
+ // strings.
66
+ let minus_change_end = minus_length - string_pair. common_suffix_length ;
67
+ let plus_change_end = plus_length - string_pair. common_suffix_length ;
68
+ let change_begin = min (
69
+ string_pair. common_prefix_length ,
70
+ min ( minus_change_end, plus_change_end) ,
70
71
) ;
71
- let plus_change_end = max ( plus_length - string_pair. common_suffix_length , change_begin) ;
72
72
73
73
minus_line_sections. push ( vec ! [
74
74
( minus_style_modifier, minus[ 0 ..change_begin] . to_string( ) ) ,
0 commit comments