mitsuhiko / similar

A high level diffing library for rust based on diffs
https://insta.rs/similar
Apache License 2.0
949 stars 31 forks source link

`finish` is not always called in LCS algorithm #55

Closed madsmtm closed 8 months ago

madsmtm commented 8 months ago

The LCS implementation does not always call finish at the end of its execution. This can cause issues when used with diff hooks like Replace.

You can use the code below as a regression test.

use similar::algorithms::{lcs, DiffHook};

struct HasRunFinish(bool);

impl DiffHook for HasRunFinish {
    type Error = ();
    fn finish(&mut self) -> Result<(), Self::Error> {
        self.0 = true;
        Ok(())
    }
}

let mut d = HasRunFinish(false);
let slice = &[1, 2];
lcs::diff(&mut d, slice, 0..slice.len(), slice, 0..slice.len()).unwrap();
assert!(d.0);