Skip to content

Commit

Permalink
Fix overlap bug in LCS
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Dec 29, 2023
1 parent 1871278 commit 2ef5562
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to similar are documented here.
## 2.4.0

* Fixed a bug where the LCS diff algorithm didn't always call `D::finish`. (#58)
* Fixed a bug in LCS that caused a panic if the common prefix and the
common suffix overlapped.

## 2.3.0

Expand Down
30 changes: 29 additions & 1 deletion src/algorithms/lcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ where
}

let common_prefix_len = common_prefix_len(old, old_range.clone(), new, new_range.clone());
let common_suffix_len = common_suffix_len(old, old_range.clone(), new, new_range.clone());
let common_suffix_len = common_suffix_len(
old,
old_range.start + common_prefix_len..old_range.end,
new,
new_range.start + common_prefix_len..new_range.end,
);

// If the sequences are not different then we're done
if common_prefix_len == old_range.len() && (old_range.len() == new_range.len()) {
Expand Down Expand Up @@ -266,3 +271,26 @@ fn test_finish_called() {
diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);
}

#[test]
fn test_bad_range_regression() {
use crate::algorithms::Capture;
use crate::DiffOp;
let mut d = Capture::new();
diff(&mut d, &[0], 0..1, &[0, 0], 0..2).unwrap();
assert_eq!(
d.into_ops(),
vec![
DiffOp::Equal {
old_index: 0,
new_index: 0,
len: 1
},
DiffOp::Insert {
old_index: 1,
new_index: 1,
new_len: 1
}
]
);
}

0 comments on commit 2ef5562

Please sign in to comment.