Compute word-level diff with custom configuration. # Arguments `old` - The original content `new` - The modified content `config` - Configuration options # Returns A `WordDiffResult` containing the diff operations. # Example ```rust use atomic_core::diff::word::{word_diff_with_config, WordDiffConfig}; let config = WordDiffConfig::ignoring_whitespace(); let result = word_diff_with_config(b"a
(
old: &'a [u8],
new: &'a [u8],
config: &WordDiffConfig,
)
| 576 | /// // With ignore_whitespace, these might be considered equal |
| 577 | /// ``` |
| 578 | pub fn word_diff_with_config<'a>( |
| 579 | old: &'a [u8], |
| 580 | new: &'a [u8], |
| 581 | config: &WordDiffConfig, |
| 582 | ) -> WordDiffResult<'a> { |
| 583 | // Tokenize both inputs |
| 584 | let old_tokens: Vec<Token<'a>> = |
| 585 | Tokenizer::with_config(old, config.tokenizer_config.clone()).collect(); |
| 586 | let new_tokens: Vec<Token<'a>> = |
| 587 | Tokenizer::with_config(new, config.tokenizer_config.clone()).collect(); |
| 588 | |
| 589 | // Optionally filter whitespace |
| 590 | let (old_filtered, new_filtered): (Vec<Token<'a>>, Vec<Token<'a>>) = if config.filter_whitespace |
| 591 | { |
| 592 | ( |
| 593 | old_tokens |
| 594 | .into_iter() |
| 595 | .filter(|t| t.is_significant()) |
| 596 | .collect(), |
| 597 | new_tokens |
| 598 | .into_iter() |
| 599 | .filter(|t| t.is_significant()) |
| 600 | .collect(), |
| 601 | ) |
| 602 | } else { |
| 603 | (old_tokens, new_tokens) |
| 604 | }; |
| 605 | |
| 606 | // Handle empty cases |
| 607 | if old_filtered.is_empty() && new_filtered.is_empty() { |
| 608 | return WordDiffResult::new(old_filtered, new_filtered); |
| 609 | } |
| 610 | |
| 611 | if old_filtered.is_empty() { |
| 612 | let len = new_filtered.len(); |
| 613 | let mut result = WordDiffResult::new(old_filtered, new_filtered); |
| 614 | result.push(WordDiffOp::Insert { |
| 615 | old_pos: 0, |
| 616 | new_range: 0..len, |
| 617 | }); |
| 618 | return result; |
| 619 | } |
| 620 | |
| 621 | if new_filtered.is_empty() { |
| 622 | let len = old_filtered.len(); |
| 623 | let mut result = WordDiffResult::new(old_filtered, new_filtered); |
| 624 | result.push(WordDiffOp::Delete { |
| 625 | old_range: 0..len, |
| 626 | new_pos: 0, |
| 627 | }); |
| 628 | return result; |
| 629 | } |
| 630 | |
| 631 | // Quick check: if sequences are identical, return early |
| 632 | if old_filtered.len() == new_filtered.len() |
| 633 | && old_filtered |
| 634 | .iter() |
| 635 | .zip(new_filtered.iter()) |