Expand description
A small, dependency-free, generic sequence diff library (see docs/SPEC.md §5).
It knows nothing about animation: it turns two ordered sequences into an edit script of
Ops. Items are compared by a caller-supplied key rather than PartialEq, so the
caller decides what “the same” means.
The pipeline is:
- Keys are interned to dense integers and the common prefix/suffix is trimmed.
- A shortest edit script is computed with Myers’ greedy algorithm, its linear-space
variant (chosen automatically for large inputs), or patience diff. For inputs of
moderate size, ties between equally short scripts are broken by
TieBreak::Stable. - Hunks are normalized so deletions come before insertions (determinism).
- Optional post-passes: move detection (§5.4), semantic cleanup, and replacement pairing, both adjacent and cross-hunk (§5.5).
use fastanim_diff::{diff, DiffOptions, Op};
let a: Vec<char> = "a+b=c".chars().collect();
let b: Vec<char> = "b+a=c".chars().collect();
let ops = diff(&a, &b, |c| *c, &DiffOptions::default());
assert!(ops.iter().any(|op| matches!(op, Op::Move { .. })));Structs§
- Diff
Options - Options for
diffandDiffer. - Differ
- A configurable diff, for when
diffisn’t enough.
Enums§
- Algorithm
- The core shortest-edit-script algorithm.
- Cleanup
- Folding of short equal runs into the surrounding edits.
- Op
- One step of an edit script.
- Script
Error - Why an edit script is not a valid transformation of
aintob. - TieBreak
- How to choose between edit scripts of equal (minimal) cost.
Constants§
- LINEAR_
SPACE_ THRESHOLD - Above this many items (
N + M, after trimming),Algorithm::Myersswitches to the linear-space variant. - STABLE_
LIMIT TieBreak::Stableapplies whileN · M(after trimming, if needed) is at most this.
Functions§
- apply
- Applies a script to
a, producing the new sequence. Kept and moved items are taken froma; inserted and replacement items fromb. Fails if the script is invalid or pairs items whose keys differ, so on success the result’s keys equalb’s. - diff
- Diffs
aagainstb, comparing items bykey. - edit_
cost - Number of items not kept in place: every
aandbitem outside anEqualcounts once. For a core script (Equal/Delete/Insertonly) this is the edit distanceD. - expand
- Expands a script over groups (e.g. lines) into one over their items (e.g. tokens), for
coarse-to-fine diffing (SPEC §5.6).
aandbgive each group’s contiguous range of items. - validate
- Checks the structure of a script for inputs of length
nandm: every index of each input is used exactly once,bindices appear in increasing order, andEqualops keepaorder.