Merge consecutive Equal operations in the result.
(result: &mut DiffResult)
| 341 | |
| 342 | /// Merge consecutive Equal operations in the result. |
| 343 | fn merge_equal_ops(result: &mut DiffResult) { |
| 344 | // Take ownership of the ops by replacing with empty DiffResult |
| 345 | let old_result = std::mem::take(result); |
| 346 | let ops = old_result.into_ops(); |
| 347 | let mut merged = Vec::with_capacity(ops.len()); |
| 348 | |
| 349 | for op in ops { |
| 350 | if let DiffOp::Equal { |
| 351 | old_pos, |
| 352 | new_pos, |
| 353 | len, |
| 354 | } = op |
| 355 | { |
| 356 | // Check if we can merge with the previous operation |
| 357 | if let Some(DiffOp::Equal { |
| 358 | old_pos: prev_old, |
| 359 | new_pos: prev_new, |
| 360 | len: prev_len, |
| 361 | }) = merged.last_mut() |
| 362 | { |
| 363 | if *prev_old + *prev_len == old_pos && *prev_new + *prev_len == new_pos { |
| 364 | *prev_len += len; |
| 365 | continue; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | merged.push(op); |
| 370 | } |
| 371 | |
| 372 | *result = DiffResult::with_ops(merged); |
| 373 | } |
| 374 | |
| 375 | #[cfg(test)] |
| 376 | mod tests { |