MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / diff

Function diff

atomic-core/src/diff/myers.rs:37–61  ·  view source on GitHub ↗

Compute the diff between two sequences using the LCS-based algorithm. This finds the operations needed to transform `old` into `new`. # Arguments `old` - The original sequence `new` - The modified sequence # Returns A [`DiffResult`] containing the operations to transform old → new.

(old: &[Line<'a>], new: &[Line<'a>])

Source from the content-addressed store, hash-verified

35///
36/// A [`DiffResult`] containing the operations to transform old → new.
37pub fn diff<'a>(old: &[Line<'a>], new: &[Line<'a>]) -> DiffResult {
38 let n = old.len();
39 let m = new.len();
40
41 // Special cases for empty sequences
42 if n == 0 && m == 0 {
43 return DiffResult::new();
44 }
45 if n == 0 {
46 let mut result = DiffResult::new();
47 result.push(DiffOp::insert(0, 0, m));
48 return result;
49 }
50 if m == 0 {
51 let mut result = DiffResult::new();
52 result.push(DiffOp::delete(0, 0, n));
53 return result;
54 }
55
56 // Compute LCS using dynamic programming
57 let lcs = compute_lcs(old, new);
58
59 // Convert LCS to diff operations
60 lcs_to_diff(&lcs, n, m)
61}
62
63/// Represents an element in the LCS.
64#[derive(Debug, Clone, Copy)]

Callers 15

test_empty_sequencesFunction · 0.70
test_insert_allFunction · 0.70
test_delete_allFunction · 0.70
test_identicalFunction · 0.70
test_single_insertFunction · 0.70
test_single_deleteFunction · 0.70
test_replaceFunction · 0.70
test_multiple_changesFunction · 0.70
test_insert_at_startFunction · 0.70
test_insert_at_endFunction · 0.70
test_delete_at_startFunction · 0.70
test_delete_at_endFunction · 0.70

Calls 4

compute_lcsFunction · 0.85
lcs_to_diffFunction · 0.85
lenMethod · 0.45
pushMethod · 0.45

Tested by 15

test_empty_sequencesFunction · 0.56
test_insert_allFunction · 0.56
test_delete_allFunction · 0.56
test_identicalFunction · 0.56
test_single_insertFunction · 0.56
test_single_deleteFunction · 0.56
test_replaceFunction · 0.56
test_multiple_changesFunction · 0.56
test_insert_at_startFunction · 0.56
test_insert_at_endFunction · 0.56
test_delete_at_startFunction · 0.56
test_delete_at_endFunction · 0.56