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

Function diff_with_anchors

atomic-core/src/diff/patience.rs:278–340  ·  view source on GitHub ↗

Build the diff result using LIS anchors. Process regions between consecutive anchors recursively.

(
    old: &[Line<'a>],
    new: &[Line<'a>],
    old_offset: usize,
    new_offset: usize,
    anchors: &[UniqueMatch],
)

Source from the content-addressed store, hash-verified

276///
277/// Process regions between consecutive anchors recursively.
278fn diff_with_anchors<'a>(
279 old: &[Line<'a>],
280 new: &[Line<'a>],
281 old_offset: usize,
282 new_offset: usize,
283 anchors: &[UniqueMatch],
284) -> DiffResult {
285 let mut result = DiffResult::new();
286
287 let mut old_pos = 0;
288 let mut new_pos = 0;
289
290 for anchor in anchors {
291 // Diff the region before this anchor
292 if old_pos < anchor.old_idx || new_pos < anchor.new_idx {
293 let old_region = &old[old_pos..anchor.old_idx];
294 let new_region = &new[new_pos..anchor.new_idx];
295
296 let region_result = patience_diff_recursive(
297 old_region,
298 new_region,
299 old_offset + old_pos,
300 new_offset + new_pos,
301 );
302
303 for op in region_result {
304 result.push(op);
305 }
306 }
307
308 // The anchor itself is an equal line
309 result.push(DiffOp::equal(
310 old_offset + anchor.old_idx,
311 new_offset + anchor.new_idx,
312 1,
313 ));
314
315 old_pos = anchor.old_idx + 1;
316 new_pos = anchor.new_idx + 1;
317 }
318
319 // Diff the region after the last anchor
320 if old_pos < old.len() || new_pos < new.len() {
321 let old_region = &old[old_pos..];
322 let new_region = &new[new_pos..];
323
324 let region_result = patience_diff_recursive(
325 old_region,
326 new_region,
327 old_offset + old_pos,
328 new_offset + new_pos,
329 );
330
331 for op in region_result {
332 result.push(op);
333 }
334 }
335

Callers 1

patience_diff_recursiveFunction · 0.85

Calls 4

patience_diff_recursiveFunction · 0.85
merge_equal_opsFunction · 0.85
pushMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected