Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ path = "src/main.rs"

[dependencies]
chrono = "0.4.38"
diff = "0.1.13"
itoa = "1.0.11"
regex = "1.10.4"
same-file = "1.0.6"
ses = "0.0.4"
unicode-width = "0.2.0"

[dev-dependencies]
Expand Down
77 changes: 56 additions & 21 deletions benches/bench-diffutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,43 @@ mod diffutils_cmp {

#[cfg(not(feature = "feat_bench_not_diff"))]
mod diffutils_diff {
// use std::hint::black_box;
use std::hint::black_box;

use crate::{binary, prepare::*, FILE_SIZE_KILO_BYTES};
// use diffutilslib::params;
use diffutilslib::{normal_diff, params};
use divan::Bencher;

// bench the actual compare
// TODO diff does not have a diff function
// #[divan::bench(args = [100_000,10_000])]
// fn diff_compare_files(bencher: Bencher, bytes: u64) {
// let (from, to) = gen_testfiles(lines, 0, "id");
// let cmd = format!("cmp {from} {to}");
// let opts = str_to_options(&cmd).into_iter().peekable();
// let params = params::parse_params(opts).unwrap();
//
// bencher
// // .with_inputs(|| prepare::cmp_params_identical_testfiles(lines))
// .with_inputs(|| params.clone())
// .bench_refs(|params| diff::diff(&params).unwrap());
// }

// bench original GNU diff
// bench the actual diff comparison in-process on identical files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn diff_compare_files_equal(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_equal(bytes);
let from_content = std::fs::read(from).expect("Failed to read from file");
let to_content = std::fs::read(to).expect("Failed to read to file");
let cmd = format!("diff {from} {to}");
let opts = str_to_options(&cmd).into_iter().peekable();
let (params, _) = params::parse_params(opts).unwrap();

bencher
.with_inputs(|| (&from_content, &to_content, &params))
.bench_refs(|(from, to, params)| black_box(normal_diff(from, to, params)));
}

// bench the actual diff comparison in-process on differing files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn diff_compare_files_different(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_different(bytes);
let from_content = std::fs::read(from).expect("Failed to read from file");
let to_content = std::fs::read(to).expect("Failed to read to file");
let cmd = format!("diff {from} {to}");
let opts = str_to_options(&cmd).into_iter().peekable();
let (params, _) = params::parse_params(opts).unwrap();

bencher
.with_inputs(|| (&from_content, &to_content, &params))
.bench_refs(|(from, to, params)| black_box(normal_diff(from, to, params)));
}

// bench original GNU diff on equal files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_gnu_equal(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_equal(bytes);
Expand All @@ -113,7 +128,7 @@ mod diffutils_diff {
.bench_refs(|cmd_args| binary::bench_binary("diff", cmd_args));
}

// bench the compiled release version
// bench the compiled release version on equal files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_release_equal(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_equal(bytes);
Expand All @@ -124,6 +139,27 @@ mod diffutils_diff {
.with_inputs(|| args_str.clone())
.bench_refs(|cmd_args| binary::bench_binary("target/release/diffutils", cmd_args));
}

// bench original GNU diff on differing files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_gnu_different(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_different(bytes);
let args_str = format!("{from} {to}");
bencher
.with_inputs(|| args_str.clone())
.bench_refs(|cmd_args| binary::bench_binary("diff", cmd_args));
}

// bench the compiled release version on differing files
#[divan::bench(args = FILE_SIZE_KILO_BYTES)]
fn cmd_diff_release_different(bencher: Bencher, bytes: u64) {
let (from, to) = get_context().get_test_files_different(bytes);
let args_str = format!("diff {from} {to}");

bencher
.with_inputs(|| args_str.clone())
.bench_refs(|cmd_args| binary::bench_binary("target/release/diffutils", cmd_args));
}
}

mod parser {
Expand Down Expand Up @@ -204,7 +240,6 @@ mod prepare {
&self.files_equal[p]
}

#[allow(unused)]
pub fn get_test_files_different(&self, kb: u64) -> &(String, String) {
let p = FILE_SIZE_KILO_BYTES.iter().position(|f| *f == kb).unwrap();
&self.files_different[p]
Expand Down Expand Up @@ -358,7 +393,7 @@ mod prepare {
}

mod binary {
use std::process::Command;
use std::process::{Command, Stdio};

use crate::prepare::str_to_options;

Expand Down
9 changes: 5 additions & 4 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {

let mut block: Option<ChangeBlock> = None;

for result in diff::slice(&expected_lines, &actual_lines) {
let differences = ses::diff_lines(&expected_lines, &actual_lines);
for result in differences.iter_changes() {
match result {
diff::Result::Left(str) => {
ses::Change::Delete(str) => {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
Expand All @@ -150,7 +151,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
line_number_expected += 1;
lines_since_mismatch = 0;
}
diff::Result::Right(str) => {
ses::Change::Insert(str) => {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
Expand All @@ -172,7 +173,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
line_number_actual += 1;
lines_since_mismatch = 0;
}
diff::Result::Both(str, _) => {
ses::Change::Equal(str, _) => {
// if one of them is missing a newline and the other isn't, then they don't actually match
if (line_number_actual > actual_lines_count)
&& (line_number_expected > expected_lines_count)
Expand Down
9 changes: 5 additions & 4 deletions src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
actual_lines.pop();
}

for result in diff::slice(&expected_lines, &actual_lines) {
let differences = ses::diff_lines(&expected_lines, &actual_lines);
for result in differences.iter_changes() {
match result {
diff::Result::Left(str) => {
ses::Change::Delete(str) => {
if !mismatch.actual.is_empty() {
results.push(mismatch);
mismatch = Mismatch::new(line_number_expected, line_number_actual);
}
mismatch.expected.push(str.to_vec());
line_number_expected += 1;
}
diff::Result::Right(str) => {
ses::Change::Insert(str) => {
mismatch.actual.push(str.to_vec());
line_number_actual += 1;
}
diff::Result::Both(_str, _) => {
ses::Change::Equal(_str, _) => {
line_number_expected += 1;
line_number_actual += 1;
if !mismatch.actual.is_empty() || !mismatch.expected.is_empty() {
Expand Down
18 changes: 13 additions & 5 deletions src/normal_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

use std::io::Write;

use ses::Change;

use crate::params::Params;
use crate::utils::{comparison_lines, do_write_line, line_prefix};
use crate::utils::{comparison_lines, do_write_line, is_identical, line_prefix};

#[derive(Debug, PartialEq)]
struct Mismatch {
Expand Down Expand Up @@ -55,9 +57,10 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
actual_lines.pop();
}

for result in diff::slice(&expected_lines, &actual_lines) {
let differences = ses::diff_lines(&expected_lines, &actual_lines);
for result in differences.iter_changes() {
match result {
diff::Result::Left(str) => {
Change::Delete(str) => {
if !mismatch.actual.is_empty() && !mismatch.actual_missing_nl {
results.push(mismatch);
mismatch = Mismatch::new(line_number_expected, line_number_actual);
Expand All @@ -66,12 +69,12 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
mismatch.expected_missing_nl = line_number_expected > expected_lines_count;
line_number_expected += 1;
}
diff::Result::Right(str) => {
Change::Insert(str) => {
mismatch.actual.push(str.to_vec());
mismatch.actual_missing_nl = line_number_actual > actual_lines_count;
line_number_actual += 1;
}
diff::Result::Both(str, _) => {
Change::Equal(str, _) => {
match (
line_number_expected > expected_lines_count,
line_number_actual > actual_lines_count,
Expand Down Expand Up @@ -122,6 +125,11 @@ pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
// See https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Normal.html
// for details on the syntax of the normal format.
let mut output = Vec::new();

if is_identical(expected, actual) {
return output;
}

let diff_results = make_diff(expected, actual, params);
if params.brief && !diff_results.is_empty() {
write!(&mut output, "\0").unwrap();
Expand Down
23 changes: 13 additions & 10 deletions src/side_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// files that was distributed with this source code.

use core::cmp::{max, min};
use diff::Result;
use std::{io::Write, vec};
use unicode_width::UnicodeWidthStr;

Expand Down Expand Up @@ -351,14 +350,18 @@ pub fn diff<T: Write>(
More studies are needed to cover GNU diff side by side with 100% accuracy, which is one of
the goals of this project : )
*/
for result in diff::slice(&left_lines, &right_lines) {
match result {
Result::Left(left_ln) => push_output(left_ln.raw, b"", b'<', output, &config).unwrap(),
Result::Right(right_ln) => {
push_output(b"", right_ln.raw, b'>', output, &config).unwrap()
let differences = ses::diff_lines(&left_lines, &right_lines);

for change in differences.iter_changes() {
match change {
ses::Change::Delete(left_ln) => {
push_output(left_ln, b"", b'<', output, &config).unwrap()
}
ses::Change::Insert(right_ln) => {
push_output(b"", right_ln, b'>', output, &config).unwrap()
}
Result::Both(left_ln, right_ln) => {
push_output(left_ln.raw, right_ln.raw, b' ', output, &config).unwrap()
ses::Change::Equal(left_ln, right_ln) => {
push_output(left_ln, right_ln, b' ', output, &config).unwrap()
}
}
}
Expand Down Expand Up @@ -1115,10 +1118,10 @@ mod tests {
let params = generate_params();
let from_file = b"";
let to_file = b"";
let mut output = vec![];
let mut output: Vec<u8> = vec![];
diff(from_file, to_file, &mut output, &params);

assert_eq!(output, vec![]);
assert_eq!(output, Vec::<u8>::new());
}

#[test]
Expand Down
18 changes: 12 additions & 6 deletions src/unified_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use std::collections::VecDeque;
use std::io::Write;

use crate::params::Params;
use crate::utils::{comparison_lines, do_write_line, line_prefix};
use crate::utils::{get_modification_time, write_file_header, FunctionLineFinder, TimestampStyle};
use crate::utils::{
comparison_lines, do_write_line, get_modification_time, is_identical, line_prefix,
write_file_header, FunctionLineFinder, TimestampStyle,
};

#[derive(Debug, PartialEq)]
pub enum DiffLine {
Expand Down Expand Up @@ -65,9 +67,10 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
actual_lines.pop();
}

for result in diff::slice(&expected_lines, &actual_lines) {
let differences = ses::diff_lines(&expected_lines, &actual_lines);
for result in differences.iter_changes() {
match result {
diff::Result::Left(str) => {
ses::Change::Delete(str) => {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
Expand Down Expand Up @@ -104,7 +107,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
line_number_expected += 1;
lines_since_mismatch = 0;
}
diff::Result::Right(str) => {
ses::Change::Insert(str) => {
if lines_since_mismatch > max_gap {
results.push(mismatch);
mismatch = Mismatch::new(
Expand All @@ -125,7 +128,7 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {
line_number_actual += 1;
lines_since_mismatch = 0;
}
diff::Result::Both(str, _) => {
ses::Change::Equal(str, _) => {
// if one of them is missing a newline and the other isn't, then they don't actually match
if (line_number_actual > actual_lines_count)
&& (line_number_expected > expected_lines_count)
Expand Down Expand Up @@ -234,6 +237,9 @@ fn make_diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<Mismatch> {

#[must_use]
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
if is_identical(expected, actual) {
return Vec::new();
}
let from_modified_time = get_modification_time(
&params.from.to_string_lossy(),
TimestampStyle::Iso,
Expand Down
Loading
Loading