19次发布

0.7.0 2024年5月7日
0.6.4 2023年4月6日
0.6.2 2022年12月28日
0.6.1 2022年2月21日
0.2.4 2019年2月3日

#28 in 文本处理

Download history 20544/week @ 2024-05-01 20613/week @ 2024-05-08 24227/week @ 2024-05-15 23677/week @ 2024-05-22 22669/week @ 2024-05-29 21877/week @ 2024-06-05 23707/week @ 2024-06-12 22082/week @ 2024-06-19 17504/week @ 2024-06-26 17773/week @ 2024-07-03 18623/week @ 2024-07-10 20457/week @ 2024-07-17 26126/week @ 2024-07-24 26836/week @ 2024-07-31 27176/week @ 2024-08-07 21967/week @ 2024-08-14

105,526 每月下载量
用于 161 个crates (20 直接)

MIT 许可证

63KB
1K SLoC

prettydiff

Crate docs.rs

Rust中两个文件的并排差异。应用程序和库。

示例

切片差异

use prettydiff::diff_slice;

println!("Diff: {}", diff_slice(&[1, 2, 3, 4, 5, 6], &[2, 3, 5, 7]));
println!(
    "Diff: {}",
    diff_slice(&["q", "a", "b", "x", "c", "d"], &["a", "b", "y", "c", "d", "f"])
);
println!(
    "Diff: {}",
    diff_slice(&["a", "c", "d", "b"], &["a", "e", "b"])
);

diff_slice

获取更改向量

use prettydiff::diff_slice;

assert_eq!(
    diff_slice(&["q", "a", "b", "x", "c", "d"], &["a", "b", "y", "c", "d", "f"]).diff,
    vec![
        DiffOp::Remove(&["q"]),
        DiffOp::Equal(&["a", "b"]),
        DiffOp::Replace(&["x"], &["y"]),
        DiffOp::Equal(&["c", "d"]),
        DiffOp::Insert(&["f"]),
    ]
);

按字符或单词比较差异

diff_chars

use prettydiff::{diff_chars, diff_words};

println!("diff_chars: {}", diff_chars("abefcd", "zadqwc"));
println!(
    "diff_chars: {}",
    diff_chars(
        "The quick brown fox jumps over the lazy dog",
        "The quick brown dog leaps over the lazy cat"
    )
);
println!(
    "diff_chars: {}",
    diff_chars(
        "The red brown fox jumped over the rolling log",
        "The brown spotted fox leaped over the rolling log"
    )
);
println!(
    "diff_chars: {}",
    diff_chars(
        "The red    brown fox jumped over the rolling log",
        "The brown spotted fox leaped over the rolling log"
    )
    .set_highlight_whitespace(true)
);
println!(
    "diff_words: {}",
    diff_words(
        "The red brown fox jumped over the rolling log",
        "The brown spotted fox leaped over the rolling log"
    )
);
println!(
    "diff_words: {}",
    diff_words(
        "The quick brown fox jumps over the lazy dog",
        "The quick, brown dog leaps over the lazy cat"
    )
);

比较行

diff_lines

use prettydiff::diff_lines;

let code1_a = r#"
void func1() {
    x += 1
}

void func2() {
    x += 2
}
    "#;
let code1_b = r#"
void func1(a: u32) {
    x += 1
}

void functhreehalves() {
    x += 1.5
}

void func2() {
    x += 2
}

void func3(){}
"#;
println!("diff_lines:");
println!("{}", diff_lines(code1_a, code1_b));

应用程序

此crate还提供并排差异应用程序。

cargo install prettydiff
prettydiff left_file.txt right_file.txt

App

依赖关系

~0.4–8MB
~49K SLoC