Skip to content
Dima Kudosh edited this page Mar 1, 2016 · 10 revisions

Difflib

Port of Python's difflib library to Rust. It's provide all necessary tools for comparing word sequences.

Installation

Simply add difflib to your dependencies block in Cargo.toml

[dependencies]
difflib = "0.1"

Usage

Sequence trait implements for str and Vec of str, so all parameterized functions(structs) accepts only this types.

  • fn context_diff<T: Sequence>(first_sequence: &T, second_sequence: &T, from_file: &str, to_file: &str, from_file_date: &str, to_file_date: &str, n: usize) -> Vec<String>

Compare first_sequence and second_sequence (vector of strings) and return vector of strings delta in context diff format. Context diffs are a compact way of showing just the lines that have changed plus a few lines of context. The changes are shown in a before/after style. The number of context lines is set by n which defaults to three. Context diff format has a header for filenames and modification times. Any or all of these may be specified using strings for from_file, to_file, from_file_date, and to_file_date.

  • fn unified_diff<T: Sequence>(first_sequence: &T, second_sequence: &T, from_file: &str, to_file: &str, from_file_date: &str, to_file_date: &str, n: usize) -> Vec<String>

Compare first_sequence and second_sequence (vector of strings) and return vector of strings delta in unified diff format. Unified diffs are a compact way of showing just the lines that have changed plus a few lines of context. The changes are shown in a before/after style. The number of context lines is set by n which defaults to three. Unified diff format has a header for filenames and modification times. Any or all of these may be specified using strings for from_file, to_file, from_file_date, and to_file_date.

  • fn get_close_matches<'a>(word: &str, possibilities: Vec<&'a str>, n: usize, cutoff: f32) -> Vec<&'a str>

Return a list of the n best matches to word from possibilities vector. cutoff is float in the range [0..1]. All possibilities smaller than cutoff will ignored. If cutoff not in [0..1] programm will panic.

Differ struct

  • fn new() -> Differ

Differ constructor. Differ struct has 2 optional arguments: line_junk: Option<fn(&str) -> bool> and char_junk: Option<fn(&str) -> bool>. It's public fields, by default it's None, but you can set your function. line_junk used for junking string, when it return true it's mean that passed string is junk. char_junk used for junking 1 element string(char), when it return true it's mean that passed 1 element string is junk.

  • fn compare<T: ?Sized + Sequence>(&self, first_sequence: &T, second_sequence: &T) -> Vec

Compare two sequences of lines, and generate the delta (a sequence of lines).

  • fn restore(delta: &Vec, which: usize) -> Vec

Return one of the two sequences that generated a delta. delta is a result of compare method, which is number that tell which sequence you want to restore. which must be 1 or 2. If you pass another number it will panic.

Clone this wiki locally