Skip to content

Commit

Permalink
Merge pull request #37 from oSoMoN/diff-arguments
Browse files Browse the repository at this point in the history
Pass a Params reference to the various diff() functions, instead of a long list of arguments
  • Loading branch information
sylvestre authored Mar 31, 2024
2 parents 314e3a7 + e9f0630 commit b135b6f
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 234 deletions.
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/fuzz_ed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
extern crate libfuzzer_sys;
use diffutilslib::ed_diff;
use diffutilslib::ed_diff::DiffError;
use diffutilslib::params::Params;
use std::fs::{self, File};
use std::io::Write;
use std::process::Command;

fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
let mut output = ed_diff::diff(expected, actual, false, false, 8)?;
let mut output = ed_diff::diff(expected, actual, &Params::default())?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand Down
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/fuzz_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[macro_use]
extern crate libfuzzer_sys;
use diffutilslib::normal_diff;
use diffutilslib::params::Params;

use std::fs::{self, File};
use std::io::Write;
Expand All @@ -21,7 +22,7 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>)| {
} else {
return
}*/
let diff = normal_diff::diff(&from, &to, false, false, 8);
let diff = normal_diff::diff(&from, &to, &Params::default());
File::create("target/fuzz.file.original")
.unwrap()
.write_all(&from)
Expand Down
13 changes: 7 additions & 6 deletions fuzz/fuzz_targets/fuzz_patch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_main]
#[macro_use]
extern crate libfuzzer_sys;
use diffutilslib::params::Params;
use diffutilslib::unified_diff;
use std::fs::{self, File};
use std::io::Write;
Expand All @@ -22,13 +23,13 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>, u8)| {
}*/
let diff = unified_diff::diff(
&from,
"a/fuzz.file",
&to,
"target/fuzz.file",
context as usize,
false,
false,
8,
&Params {
from: "a/fuzz.file".into(),
to: "target/fuzz.file".into(),
context_count: context as usize,
..Default::default()
}
);
File::create("target/fuzz.file.original")
.unwrap()
Expand Down
131 changes: 62 additions & 69 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::collections::VecDeque;
use std::io::Write;

use crate::params::Params;
use crate::utils::do_write_line;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -265,23 +266,18 @@ fn make_diff(
}

#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn diff(
expected: &[u8],
expected_filename: &str,
actual: &[u8],
actual_filename: &str,
context_size: usize,
stop_early: bool,
expand_tabs: bool,
tabsize: usize,
) -> Vec<u8> {
let mut output = format!("*** {expected_filename}\t\n--- {actual_filename}\t\n").into_bytes();
let diff_results = make_diff(expected, actual, context_size, stop_early);
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Vec<u8> {
let mut output = format!(
"*** {0}\t\n--- {1}\t\n",
params.from.to_string_lossy(),
params.to.to_string_lossy()
)
.into_bytes();
let diff_results = make_diff(expected, actual, params.context_count, params.brief);
if diff_results.is_empty() {
return Vec::new();
}
if stop_early {
if params.brief {
return output;
}
for result in diff_results {
Expand Down Expand Up @@ -319,19 +315,19 @@ pub fn diff(
match line {
DiffLine::Context(e) => {
write!(output, " ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Change(e) => {
write!(output, "! ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Add(e) => {
write!(output, "- ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
Expand All @@ -349,19 +345,19 @@ pub fn diff(
match line {
DiffLine::Context(e) => {
write!(output, " ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Change(e) => {
write!(output, "! ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Add(e) => {
write!(output, "+ ").expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
do_write_line(&mut output, &e, params.expand_tabs, params.tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
Expand Down Expand Up @@ -430,13 +426,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alef",
&bet,
&format!("{target}/alef"),
2,
false,
false,
8,
&Params {
from: "a/alef".into(),
to: (&format!("{target}/alef")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/ab.diff"))
.unwrap()
Expand Down Expand Up @@ -511,13 +507,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alef_",
&bet,
&format!("{target}/alef_"),
2,
false,
false,
8,
&Params {
from: "a/alef_".into(),
to: (&format!("{target}/alef_")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
Expand Down Expand Up @@ -595,13 +591,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alefx",
&bet,
&format!("{target}/alefx"),
2,
false,
false,
8,
&Params {
from: "a/alefx".into(),
to: (&format!("{target}/alefx")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/abx.diff"))
.unwrap()
Expand Down Expand Up @@ -682,13 +678,13 @@ mod tests {
// We want it to turn the alef into bet.
let diff = diff(
&alef,
"a/alefr",
&bet,
&format!("{target}/alefr"),
2,
false,
false,
8,
&Params {
from: "a/alefr".into(),
to: (&format!("{target}/alefr")).into(),
context_count: 2,
..Default::default()
},
);
File::create(&format!("{target}/abr.diff"))
.unwrap()
Expand Down Expand Up @@ -725,17 +721,15 @@ mod tests {
let from = ["a", "b", "c", ""].join("\n");
let to_filename = "bar";
let to = ["a", "d", "c", ""].join("\n");
let context_size: usize = 3;

let diff_full = diff(
from.as_bytes(),
from_filename,
to.as_bytes(),
to_filename,
context_size,
false,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
..Default::default()
},
);
let expected_full = [
"*** foo\t",
Expand All @@ -756,38 +750,37 @@ mod tests {

let diff_brief = diff(
from.as_bytes(),
from_filename,
to.as_bytes(),
to_filename,
context_size,
true,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
brief: true,
..Default::default()
},
);
let expected_brief = ["*** foo\t", "--- bar\t", ""].join("\n");
assert_eq!(diff_brief, expected_brief.as_bytes());

let nodiff_full = diff(
from.as_bytes(),
from_filename,
from.as_bytes(),
to_filename,
context_size,
false,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
..Default::default()
},
);
assert!(nodiff_full.is_empty());

let nodiff_brief = diff(
from.as_bytes(),
from_filename,
from.as_bytes(),
to_filename,
context_size,
true,
false,
8,
&Params {
from: from_filename.into(),
to: to_filename.into(),
brief: true,
..Default::default()
},
);
assert!(nodiff_brief.is_empty());
}
Expand Down
43 changes: 27 additions & 16 deletions src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::io::Write;

use crate::params::Params;
use crate::utils::do_write_line;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -109,16 +110,10 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result<Vec<Mis
Ok(results)
}

pub fn diff(
expected: &[u8],
actual: &[u8],
stop_early: bool,
expand_tabs: bool,
tabsize: usize,
) -> Result<Vec<u8>, DiffError> {
pub fn diff(expected: &[u8], actual: &[u8], params: &Params) -> Result<Vec<u8>, DiffError> {
let mut output = Vec::new();
let diff_results = make_diff(expected, actual, stop_early)?;
if stop_early && !diff_results.is_empty() {
let diff_results = make_diff(expected, actual, params.brief)?;
if params.brief && !diff_results.is_empty() {
write!(&mut output, "\0").unwrap();
return Ok(output);
}
Expand Down Expand Up @@ -153,7 +148,7 @@ pub fn diff(
if actual == b"." {
writeln!(&mut output, "..\n.\ns/.//\na").unwrap();
} else {
do_write_line(&mut output, actual, expand_tabs, tabsize).unwrap();
do_write_line(&mut output, actual, params.expand_tabs, params.tabsize).unwrap();
writeln!(&mut output).unwrap();
}
}
Expand All @@ -168,7 +163,7 @@ mod tests {
use super::*;
use pretty_assertions::assert_eq;
pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
let mut output = diff(expected, actual, false, false, 8)?;
let mut output = diff(expected, actual, &Params::default())?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand All @@ -177,7 +172,7 @@ mod tests {
fn test_basic() {
let from = b"a\n";
let to = b"b\n";
let diff = diff(from, to, false, false, 8).unwrap();
let diff = diff(from, to, &Params::default()).unwrap();
let expected = ["1c", "b", ".", ""].join("\n");
assert_eq!(diff, expected.as_bytes());
}
Expand Down Expand Up @@ -412,18 +407,34 @@ mod tests {
let from = ["a", "b", "c", ""].join("\n");
let to = ["a", "d", "c", ""].join("\n");

let diff_full = diff(from.as_bytes(), to.as_bytes(), false, false, 8).unwrap();
let diff_full = diff(from.as_bytes(), to.as_bytes(), &Params::default()).unwrap();
let expected_full = ["2c", "d", ".", ""].join("\n");
assert_eq!(diff_full, expected_full.as_bytes());

let diff_brief = diff(from.as_bytes(), to.as_bytes(), true, false, 8).unwrap();
let diff_brief = diff(
from.as_bytes(),
to.as_bytes(),
&Params {
brief: true,
..Default::default()
},
)
.unwrap();
let expected_brief = "\0".as_bytes();
assert_eq!(diff_brief, expected_brief);

let nodiff_full = diff(from.as_bytes(), from.as_bytes(), false, false, 8).unwrap();
let nodiff_full = diff(from.as_bytes(), from.as_bytes(), &Params::default()).unwrap();
assert!(nodiff_full.is_empty());

let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true, false, 8).unwrap();
let nodiff_brief = diff(
from.as_bytes(),
from.as_bytes(),
&Params {
brief: true,
..Default::default()
},
)
.unwrap();
assert!(nodiff_brief.is_empty());
}
}
Loading

0 comments on commit b135b6f

Please sign in to comment.