https://github.com/dimakudosh/difflib
Port of Python's difflib library to Rust
https://github.com/dimakudosh/difflib
difflib python-difflib-library rust
Last synced: 8 months ago
JSON representation
Port of Python's difflib library to Rust
- Host: GitHub
- URL: https://github.com/dimakudosh/difflib
- Owner: DimaKudosh
- License: mit
- Created: 2016-02-12T16:14:45.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-07-28T21:05:38.000Z (over 4 years ago)
- Last Synced: 2025-04-07T18:14:33.053Z (11 months ago)
- Topics: difflib, python-difflib-library, rust
- Language: Rust
- Homepage:
- Size: 52.7 KB
- Stars: 48
- Watchers: 3
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Difflib [](https://travis-ci.org/DimaKudosh/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
```rust
[dependencies]
difflib = "0.4.0"
```
## Documentation
Documentation is available at https://github.com/DimaKudosh/difflib/wiki
## Example
```rust
extern crate difflib;
use difflib::differ::Differ;
use difflib::sequencematcher::SequenceMatcher;
fn main() {
// unified_diff
let first_text = "one two three four".split(" ").collect::>();
let second_text = "zero one tree four".split(" ").collect::>();
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());
}
```