Add utility methods to diff that capture
This commit is contained in:
parent
c9b0aa2eef
commit
8692e89b0a
2 changed files with 38 additions and 3 deletions
|
|
@ -123,8 +123,7 @@ impl Capture {
|
||||||
|
|
||||||
/// Isolate change clusters by eliminating ranges with no changes.
|
/// Isolate change clusters by eliminating ranges with no changes.
|
||||||
///
|
///
|
||||||
/// This will leave holes behind in long periods of equal ranges so that
|
/// This is equivalent to calling [`group_diff_ops`] on [`Capture::into_ops`].
|
||||||
/// you can build things like unified diffs.
|
|
||||||
pub fn into_grouped_ops(self, n: usize) -> Vec<Vec<DiffOp>> {
|
pub fn into_grouped_ops(self, n: usize) -> Vec<Vec<DiffOp>> {
|
||||||
group_diff_ops(self.into_ops(), n)
|
group_diff_ops(self.into_ops(), n)
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +134,11 @@ impl Capture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn group_diff_ops(mut ops: Vec<DiffOp>, n: usize) -> Vec<Vec<DiffOp>> {
|
/// Isolate change clusters by eliminating ranges with no changes.
|
||||||
|
///
|
||||||
|
/// This will leave holes behind in long periods of equal ranges so that
|
||||||
|
/// you can build things like unified diffs.
|
||||||
|
pub fn group_diff_ops(mut ops: Vec<DiffOp>, n: usize) -> Vec<Vec<DiffOp>> {
|
||||||
if ops.is_empty() {
|
if ops.is_empty() {
|
||||||
return vec![];
|
return vec![];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,3 +77,35 @@ where
|
||||||
{
|
{
|
||||||
diff(alg, d, old, 0..old.len(), new, 0..new.len())
|
diff(alg, d, old, 0..old.len(), new, 0..new.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a diff between old and new with the given algorithm capturing the ops.
|
||||||
|
///
|
||||||
|
/// This is like [`diff`] but instead of using an arbitrary hook this will
|
||||||
|
/// always use [`Replace`] + [`Capture`] and return the captured [`DiffOp`]s.
|
||||||
|
pub fn capture_diff<Old, New>(
|
||||||
|
alg: Algorithm,
|
||||||
|
old: &Old,
|
||||||
|
old_range: Range<usize>,
|
||||||
|
new: &New,
|
||||||
|
new_range: Range<usize>,
|
||||||
|
) -> Vec<DiffOp>
|
||||||
|
where
|
||||||
|
Old: Index<usize> + ?Sized,
|
||||||
|
New: Index<usize> + ?Sized,
|
||||||
|
Old::Output: Hash + Eq + Ord,
|
||||||
|
New::Output: PartialEq<Old::Output> + Hash + Eq + Ord,
|
||||||
|
{
|
||||||
|
let mut d = Replace::new(Capture::new());
|
||||||
|
diff(alg, &mut d, old, old_range, new, new_range).unwrap();
|
||||||
|
d.into_inner().into_ops()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a diff between old and new with the given algorithm capturing the ops.
|
||||||
|
pub fn capture_diff_slices<D, T>(alg: Algorithm, old: &[T], new: &[T]) -> Vec<DiffOp>
|
||||||
|
where
|
||||||
|
T: Eq + Hash + Ord,
|
||||||
|
{
|
||||||
|
let mut d = Replace::new(Capture::new());
|
||||||
|
diff_slices(alg, &mut d, old, new).unwrap();
|
||||||
|
d.into_inner().into_ops()
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue