Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overlap bug in LCS #59

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. (#59)

## 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
}
]
);
}