diff options
| author | Micha White <botahamec@outlook.com> | 2025-10-04 19:07:22 -0400 |
|---|---|---|
| committer | Micha White <botahamec@outlook.com> | 2025-10-04 19:07:22 -0400 |
| commit | 84105e10d0ce93052cdce677e8472bdcc3c388db (patch) | |
| tree | 4fff3ee866aff26c79c9cf3ec1bacba9cd78535f /src/diff.rs | |
| parent | 1dd18cdb4c9e537993cef42dd6bb7c040b9ca232 (diff) | |
Basic diffing
Diffstat (limited to 'src/diff.rs')
| -rw-r--r-- | src/diff.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/diff.rs b/src/diff.rs new file mode 100644 index 0000000..0d11400 --- /dev/null +++ b/src/diff.rs @@ -0,0 +1,26 @@ +use std::ops::Range; + +use imara_diff::{Algorithm, Diff, Hunk, InternedInput}; + +pub enum DiffSpan { + Insertion(Range<u32>), + Deletion(Range<u32>), +} + +pub fn diff(before: &[u8], after: &[u8]) -> Vec<DiffSpan> { + let input = InternedInput::new(before, after); + let diff = Diff::compute(Algorithm::Histogram, &input); + + let hunks: Vec<Hunk> = diff.hunks().collect(); + let mut spans = Vec::with_capacity(hunks.len()); + for hunk in hunks { + if !hunk.before.is_empty() { + spans.push(DiffSpan::Deletion(hunk.before)); + } + if !hunk.after.is_empty() { + spans.push(DiffSpan::Insertion(hunk.after)); + } + } + + spans +} |
