Skip to content

Implement -t/--expand-tabs option (fixes #26) and --tabsize=NUM option (fixes #27) #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2024
Merged
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
8 changes: 8 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ path = "src/main.rs"

[dependencies]
diff = "0.1.10"
regex = "1.10.3"
same-file = "1.0.6"
unicode-width = "0.1.11"

[dev-dependencies]
pretty_assertions = "1"
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_ed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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)?;
let mut output = ed_diff::diff(expected, actual, false, false, 8)?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>)| {
} else {
return
}*/
let diff = normal_diff::diff(&from, &to);
let diff = normal_diff::diff(&from, &to, false, false, 8);
File::create("target/fuzz.file.original")
.unwrap()
.write_all(&from)
Expand Down
3 changes: 3 additions & 0 deletions fuzz/fuzz_targets/fuzz_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ fuzz_target!(|x: (Vec<u8>, Vec<u8>, u8)| {
&to,
"target/fuzz.file",
context as usize,
false,
false,
8,
);
File::create("target/fuzz.file.original")
.unwrap()
Expand Down
39 changes: 33 additions & 6 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use std::collections::VecDeque;
use std::io::Write;

use crate::utils::do_write_line;

#[derive(Debug, PartialEq)]
pub enum DiffLine {
Context(Vec<u8>),
Expand Down Expand Up @@ -263,13 +265,16 @@ 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);
Expand Down Expand Up @@ -314,17 +319,20 @@ pub fn diff(
match line {
DiffLine::Context(e) => {
write!(output, " ").expect("write to Vec is infallible");
output.write_all(&e).expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Change(e) => {
write!(output, "! ").expect("write to Vec is infallible");
output.write_all(&e).expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Add(e) => {
write!(output, "- ").expect("write to Vec is infallible");
output.write_all(&e).expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
}
Expand All @@ -341,17 +349,20 @@ pub fn diff(
match line {
DiffLine::Context(e) => {
write!(output, " ").expect("write to Vec is infallible");
output.write_all(&e).expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Change(e) => {
write!(output, "! ").expect("write to Vec is infallible");
output.write_all(&e).expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
DiffLine::Add(e) => {
write!(output, "+ ").expect("write to Vec is infallible");
output.write_all(&e).expect("write to Vec is infallible");
do_write_line(&mut output, &e, expand_tabs, tabsize)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
}
Expand Down Expand Up @@ -424,6 +435,8 @@ mod tests {
&format!("{target}/alef"),
2,
false,
false,
8,
);
File::create(&format!("{target}/ab.diff"))
.unwrap()
Expand Down Expand Up @@ -503,6 +516,8 @@ mod tests {
&format!("{target}/alef_"),
2,
false,
false,
8,
);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
Expand Down Expand Up @@ -585,6 +600,8 @@ mod tests {
&format!("{target}/alefx"),
2,
false,
false,
8,
);
File::create(&format!("{target}/abx.diff"))
.unwrap()
Expand Down Expand Up @@ -670,6 +687,8 @@ mod tests {
&format!("{target}/alefr"),
2,
false,
false,
8,
);
File::create(&format!("{target}/abr.diff"))
.unwrap()
Expand Down Expand Up @@ -715,6 +734,8 @@ mod tests {
to_filename,
context_size,
false,
false,
8,
);
let expected_full = [
"*** foo\t",
Expand All @@ -740,6 +761,8 @@ mod tests {
to_filename,
context_size,
true,
false,
8,
);
let expected_brief = ["*** foo\t", "--- bar\t", ""].join("\n");
assert_eq!(diff_brief, expected_brief.as_bytes());
Expand All @@ -751,6 +774,8 @@ mod tests {
to_filename,
context_size,
false,
false,
8,
);
assert!(nodiff_full.is_empty());

Expand All @@ -761,6 +786,8 @@ mod tests {
to_filename,
context_size,
true,
false,
8,
);
assert!(nodiff_brief.is_empty());
}
Expand Down
24 changes: 16 additions & 8 deletions src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use std::io::Write;

use crate::utils::do_write_line;

#[derive(Debug, PartialEq)]
struct Mismatch {
pub line_number_expected: usize,
Expand Down Expand Up @@ -107,7 +109,13 @@ 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) -> Result<Vec<u8>, DiffError> {
pub fn diff(
expected: &[u8],
actual: &[u8],
stop_early: bool,
expand_tabs: bool,
tabsize: usize,
) -> 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() {
Expand Down Expand Up @@ -145,7 +153,7 @@ pub fn diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Result<Vec<u8>,
if actual == b"." {
writeln!(&mut output, "..\n.\ns/.//\na").unwrap();
} else {
output.write_all(actual).unwrap();
do_write_line(&mut output, actual, expand_tabs, tabsize).unwrap();
writeln!(&mut output).unwrap();
}
}
Expand All @@ -160,7 +168,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)?;
let mut output = diff(expected, actual, false, false, 8)?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand All @@ -169,7 +177,7 @@ mod tests {
fn test_basic() {
let from = b"a\n";
let to = b"b\n";
let diff = diff(from, to, false).unwrap();
let diff = diff(from, to, false, false, 8).unwrap();
let expected = ["1c", "b", ".", ""].join("\n");
assert_eq!(diff, expected.as_bytes());
}
Expand Down Expand Up @@ -404,18 +412,18 @@ 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).unwrap();
let diff_full = diff(from.as_bytes(), to.as_bytes(), false, false, 8).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).unwrap();
let diff_brief = diff(from.as_bytes(), to.as_bytes(), true, false, 8).unwrap();
let expected_brief = "\0".as_bytes();
assert_eq!(diff_brief, expected_brief);

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

let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true).unwrap();
let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true, false, 8).unwrap();
assert!(nodiff_brief.is_empty());
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod context_diff;
pub mod ed_diff;
pub mod normal_diff;
pub mod unified_diff;
pub mod utils;

// Re-export the public functions/types you need
pub use context_diff::diff as context_diff;
Expand Down
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod ed_diff;
mod normal_diff;
mod params;
mod unified_diff;
mod utils;

// Exit codes are documented at
// https://www.gnu.org/software/diffutils/manual/html_node/Invoking-diff.html.
Expand All @@ -30,6 +31,8 @@ fn main() -> ExitCode {
format,
report_identical_files,
brief,
expand_tabs,
tabsize,
} = parse_params(opts).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
Expand Down Expand Up @@ -65,14 +68,18 @@ fn main() -> ExitCode {
};
// run diff
let result: Vec<u8> = match format {
Format::Normal => normal_diff::diff(&from_content, &to_content, brief),
Format::Normal => {
normal_diff::diff(&from_content, &to_content, brief, expand_tabs, tabsize)
}
Format::Unified => unified_diff::diff(
&from_content,
&from.to_string_lossy(),
&to_content,
&to.to_string_lossy(),
context_count,
brief,
expand_tabs,
tabsize,
),
Format::Context => context_diff::diff(
&from_content,
Expand All @@ -81,11 +88,14 @@ fn main() -> ExitCode {
&to.to_string_lossy(),
context_count,
brief,
expand_tabs,
tabsize,
),
Format::Ed => ed_diff::diff(&from_content, &to_content, brief).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
}),
Format::Ed => ed_diff::diff(&from_content, &to_content, brief, expand_tabs, tabsize)
.unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
}),
};
if brief && !result.is_empty() {
println!(
Expand Down
Loading
Loading