Fix bug in patience

This commit is contained in:
Armin Ronacher 2021-01-24 03:01:40 +01:00
parent 89729de117
commit 5dca6ae665
3 changed files with 34 additions and 5 deletions

View file

@ -2,6 +2,10 @@
All notable changes to similar are documented here. All notable changes to similar are documented here.
## 0.2.0
* Fixed a bug in the patience algorithm causing it not not work.
## 0.1.0 ## 0.1.0
* Initial release. * Initial release.

View file

@ -6,11 +6,12 @@
[![Documentation](https://docs.rs/similar/badge.svg)](https://docs.rs/similar) [![Documentation](https://docs.rs/similar/badge.svg)](https://docs.rs/similar)
Similar is a dependency free crate for Rust that implements different diffing Similar is a dependency free crate for Rust that implements different diffing
algorithms and high level interfaces for it. algorithms and high level interfaces for it. It is based on the [pijul](https://pijul.org/)
implementation of the Myer's and Patience algorithms and inherits some ideas
from there.
It provides both low level implementations of Myer's and the Patience diff It's intended to be replacement for the popular but unmaintained
algorithm as well as high level text diffing utilities (such as the ability [difference] crate.
to generate unified diffs).
```rust ```rust
use similar::algorithms::Algorithm; use similar::algorithms::Algorithm;

View file

@ -30,7 +30,7 @@ where
D: DiffHook, D: DiffHook,
{ {
let old_indexes = unique(old, old_range.start, old_range.end); let old_indexes = unique(old, old_range.start, old_range.end);
let new_indexes = unique(new, old_range.start, old_range.end); let new_indexes = unique(new, new_range.start, new_range.end);
let mut d = Replace::new(Patience { let mut d = Replace::new(Patience {
d, d,
@ -227,3 +227,27 @@ fn test_patience() {
] ]
"###); "###);
} }
#[test]
fn test_patience_out_of_bounds_bug() {
let a: &[usize] = &[1, 2, 3, 4];
let b: &[usize] = &[1, 2, 3];
let mut d = Replace::new(crate::algorithms::Capture::new());
diff_slices(&mut d, a, b).unwrap();
insta::assert_debug_snapshot!(d.into_inner().ops(), @r###"
[
Equal {
old_index: 0,
new_index: 0,
len: 3,
},
Delete {
old_index: 3,
old_len: 1,
new_index: 3,
},
]
"###);
}