Function rdiff::string_diff::find_diff [] [src]

pub fn find_diff<S: OperationScore>(old: &str, new: &str, scorer: &S) -> Diff

Finds the difference on a character by character level between two strings

Uses the Hirschberg algorithm (doi: 10.1145/360825.360861) which operates in O(x * y) time and O(y) space. The algorithm finds the minimal set of operations that will transform 'old' into 'new'. The 'weight' of each operation is determined by the scorer. For more details about weighting, see the OperationScore documentation.

The operations in the returned Diffare presented in file order, with offsets assuming the previous operations have already been performed. Furthermore, the inserts are assumed to be performed prior to the deletes.

Example

use rdiff::string_diff::{find_diff, EditDistance};
// Find the difference between meadow and yellowing using the edit distance as the weighting.
let diff = find_diff("meadow", "yellowing", &EditDistance{});
// prints (0, 'y'), (3, 'll') and (9, 'ing')
for insert in diff.inserts() {
    println!("{:?}", insert);
}
// prints (1, 1) and (4, 2)
for delete in diff.deletes() {
    println!("{:?}", delete);
}
assert_eq!("yellowing", diff.apply_to_string("meadow").unwrap());