Module similar::text[][src]

Text diffing utilities.

This provides helpful utilities for text (and more specifically line) diff operations. The main type you want to work with is TextDiff which uses the underlying diff algorithms to expose a convenient API to work with texts.

It can produce a unified diff and also let you iterate over the changeset directly if you want.

Text diffing is available by default but can be disabled by turning off the default features. The feature to enable to get it back is text.

Examples

A super simple example for how to generate a unified diff with three lines off context around the changes:

let diff = TextDiff::from_lines(old_text, new_text);
let unified_diff = diff.unified_diff().header("old_file", "new_file").to_string();

This is another example that iterates over the actual changes:

let diff = TextDiff::from_lines(old_text, new_text);
for op in diff.ops() {
    for change in diff.iter_changes(op) {
        println!("{:?}", change);
    }
}

Ops vs Changes

Because very commonly two compared sequences will largely match this module splits it's functionality into two layers. The first is inherited from the general algorithms module: changes are encoded as diff operations. These are ranges of the differences by index in the source sequence. Because this can be cumbersome to work with a separate method TextDiff::iter_changes is provided which expands all the changes on an item by item level encoded in an operation.

Because the TextDiff::grouped_ops method can isolate clusters of changes this even works for very long files if paired with this method.

Structs

Change

Represents the expanded textual change.

TextDiff

Captures diff op codes for textual diffs

TextDiffConfig

A builder type config for more complex uses of TextDiff.

UnifiedDiff

Unified diff formatter.

UnifiedDiffHunk

Unified diff hunk formatter.

UnifiedHunkHeader

Unified diff hunk header formatter.

Enums

ChangeTag

The tag of a change.

Functions

get_close_matches

Use the text differ to find n close matches.

unified_diff

Quick way to get a unified diff as string.