5 releases

0.0.4 Jul 23, 2023
0.0.3 Feb 17, 2023
0.0.2 Feb 17, 2023
0.0.1 Feb 16, 2023
0.0.0 Feb 14, 2023

#239 in Algorithms

Download history 68/week @ 2024-02-19 80/week @ 2024-02-26 43/week @ 2024-03-04 46/week @ 2024-03-11 10/week @ 2024-03-18 18/week @ 2024-03-25

120 downloads per month

MIT/Apache

93KB
2K SLoC

DiffMatchPatch - diffmatchpatch

The Diff Match and Patch libraries offer robust algorithms to perform the operations required for synchronising plain text. This repository contains a Rust version of the original diff-match-patch library, using up-to-date crate packages.

Modification Compared to the Upstream

  • Use Chars(Vec<char>) to represent text instead of String to avoid unnecessary traversing of the string
  • Extend diff_lines_to_chars to diff_any_to_chars to support words, lines, text blocks, and any sequence of comparable items
  • Test cases from google/diff-match-patch and distill-io/diff-match-patch.rs
  • Implement the patch part
  • Helper prelude mod

Demo

The same example is copied from https://neil.fraser.name/software/diff_match_patch/demos/diff.html.

use diffmatchpatch::prelude::*;

fn main() {
    let text1 = r#"I am the very model of a modern Major-General,
I've information vegetable, animal, and mineral,
I know the kings of England, and I quote the fights historical,
From Marathon to Waterloo, in order categorical."#;
    let text2 = r#"I am the very model of a cartoon individual,
My animation's comical, unusual, and whimsical,
I'm quite adept at funny gags, comedic theory I have read,
From wicked puns and stupid jokes to anvils that drop on your head."#;

    // No Cleanup
    //let diffs = diff_main(text2, text1);
    //println!("diffs {:#?}", diffs);

    // Semantic Cleanup
    let diffs = diff_semantic(text2, text1);
    println!("Semantic diffs {:#?}", diffs);

    // Word mode
    let diffs = diff_word_mode(text2, text1);
    println!("Word diffs {:#?}", diffs);

    // diff_word_mode is also avaliable, but is meaningless to this example
}

Original

This code is forked originally from dmp, licensed under the MIT license.

Features

  • Diffing and patching library for plain text
  • Retrieve differences between two blocks of text
  • Create a set of patches for converting a block of text into another
  • Apply a set of patches onto a block of text to convert it to another block of text
  • Uses best-effort to apply patch even when the underlying text doesn't fully match.

speedtest

On an M1 Pro MacBook Pro:

Python3                         8.695004s
JS(Chrome)                      0.469s
speedtest                       147.20 ms

Dependencies