5 releases (3 breaking)

Uses old Rust 2015

0.4.0 Jul 22, 2018
0.3.1 Mar 24, 2018
0.3.0 Sep 9, 2017
0.2.0 Jan 16, 2017
0.1.0 Mar 1, 2016

#121 in Text processing

Download history 470929/week @ 2024-08-02 511940/week @ 2024-08-09 532315/week @ 2024-08-16 524924/week @ 2024-08-23 478845/week @ 2024-08-30 523581/week @ 2024-09-06 451704/week @ 2024-09-13 516109/week @ 2024-09-20 497579/week @ 2024-09-27 549067/week @ 2024-10-04 527191/week @ 2024-10-11 564199/week @ 2024-10-18 572195/week @ 2024-10-25 519815/week @ 2024-11-01 554361/week @ 2024-11-08 439989/week @ 2024-11-15

2,194,862 downloads per month
Used in 1,501 crates (13 directly)

MIT license

32KB
864 lines

Difflib Build Status

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.4.0"

Documentation

Documentation is available at https://github.com/DimaKudosh/difflib/wiki

Example

extern crate difflib;

use difflib::differ::Differ;
use difflib::sequencematcher::SequenceMatcher;

fn main() {
    // unified_diff
    let first_text = "one two three four".split(" ").collect::<Vec<&str>>();
    let second_text = "zero one tree four".split(" ").collect::<Vec<&str>>();
    let diff = difflib::unified_diff(
        &first_text,
        &second_text,
        "Original",
        "Current",
        "2005-01-26 23:30:50",
        "2010-04-02 10:20:52",
        3,
    );
    for line in &diff {
        println!("{:?}", line);
    }

    //context_diff
    let diff = difflib::context_diff(
        &first_text,
        &second_text,
        "Original",
        "Current",
        "2005-01-26 23:30:50",
        "2010-04-02 10:20:52",
        3,
    );
    for line in &diff {
        println!("{:?}", line);
    }

    //get_close_matches
    let words = vec!["ape", "apple", "peach", "puppy"];
    let result = difflib::get_close_matches("appel", words, 3, 0.6);
    println!("{:?}", result);

    //Differ examples
    let differ = Differ::new();
    let diff = differ.compare(&first_text, &second_text);
    for line in &diff {
        println!("{:?}", line);
    }

    //SequenceMatcher examples
    let mut matcher = SequenceMatcher::new("one two three four", "zero one tree four");
    let m = matcher.find_longest_match(0, 18, 0, 18);
    println!("{:?}", m);
    let all_matches = matcher.get_matching_blocks();
    println!("{:?}", all_matches);
    let opcode = matcher.get_opcodes();
    println!("{:?}", opcode);
    let grouped_opcodes = matcher.get_grouped_opcodes(2);
    println!("{:?}", grouped_opcodes);
    let ratio = matcher.ratio();
    println!("{:?}", ratio);
    matcher.set_seqs("aaaaa", "aaaab");
    println!("{:?}", matcher.ratio());
}

No runtime deps