diff options
author | Dan Davison <dandavison7@gmail.com> | 2019-07-16 11:55:33 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2019-07-16 12:05:53 -0400 |
commit | 4e8f9d6a394417116f6980484cc8a8cd27ca6be5 (patch) | |
tree | aeb62caaee8d97279a40a204281a4d793417230d | |
parent | 6939c2867d113df60fe34526c2da52fe446b09ef (diff) |
Fix string pair edits algorithm0.0.1
-rw-r--r-- | src/edits.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/edits.rs b/src/edits.rs index d573049e..01b2f3ad 100644 --- a/src/edits.rs +++ b/src/edits.rs @@ -1,4 +1,4 @@ -use std::cmp::max; +use std::cmp::{max, min}; use syntect::highlighting::StyleModifier; @@ -48,7 +48,6 @@ pub fn get_diff_style_sections( for (minus, plus) in minus_lines.iter().zip(plus_lines.iter()) { let string_pair = StringPair::new(minus, plus); - let change_begin = string_pair.common_prefix_length; // We require that (right-trimmed length) >= (common prefix length). Consider: // minus = "a " @@ -58,17 +57,18 @@ pub fn get_diff_style_sections( let minus_length = max(string_pair.lengths[0], string_pair.common_prefix_length); let plus_length = max(string_pair.lengths[1], string_pair.common_prefix_length); - // We require that change_begin <= change_end. Consider: - // minus = "a c" - // plus = "a b c" - // Here, the common prefix length is 2, and the common suffix length is 2, yet the - // length of minus is 3. This overlap between prefix and suffix leads to a violation of - // the requirement. We resolve this by taking the following maxima: - let minus_change_end = max( - minus_length - string_pair.common_suffix_length, - change_begin, + // Work backwards from the end of the strings. The end of the + // change region is equal to the start of their common + // suffix. To find the start of the change region, start with + // the end of their common prefix, and then move leftwards + // until it is before the start of the common suffix in both + // strings. + let minus_change_end = minus_length - string_pair.common_suffix_length; + let plus_change_end = plus_length - string_pair.common_suffix_length; + let change_begin = min( + string_pair.common_prefix_length, + min(minus_change_end, plus_change_end), ); - let plus_change_end = max(plus_length - string_pair.common_suffix_length, change_begin); minus_line_sections.push(vec![ (minus_style_modifier, minus[0..change_begin].to_string()), |