Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement -t/--expand-tabs option
  • Loading branch information
oSoMoN committed Mar 28, 2024
commit 8d65c2baddac1226cc6d172acf3998b24d47e722
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/main.rs"
[dependencies]
diff = "0.1.10"
same-file = "1.0.6"
unicode-width = "0.1.11"

[dev-dependencies]
pretty_assertions = "1"
Expand Down
29 changes: 23 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 @@ -270,6 +272,7 @@ pub fn diff(
actual_filename: &str,
context_size: usize,
stop_early: bool,
expand_tabs: bool,
) -> 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 +317,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)
.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)
.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)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
}
Expand All @@ -341,17 +347,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)
.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)
.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)
.expect("write to Vec is infallible");
writeln!(output).unwrap();
}
}
Expand Down Expand Up @@ -424,6 +433,7 @@ mod tests {
&format!("{target}/alef"),
2,
false,
false,
);
File::create(&format!("{target}/ab.diff"))
.unwrap()
Expand Down Expand Up @@ -503,6 +513,7 @@ mod tests {
&format!("{target}/alef_"),
2,
false,
false,
);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
Expand Down Expand Up @@ -585,6 +596,7 @@ mod tests {
&format!("{target}/alefx"),
2,
false,
false,
);
File::create(&format!("{target}/abx.diff"))
.unwrap()
Expand Down Expand Up @@ -670,6 +682,7 @@ mod tests {
&format!("{target}/alefr"),
2,
false,
false,
);
File::create(&format!("{target}/abr.diff"))
.unwrap()
Expand Down Expand Up @@ -715,6 +728,7 @@ mod tests {
to_filename,
context_size,
false,
false,
);
let expected_full = [
"*** foo\t",
Expand All @@ -740,6 +754,7 @@ mod tests {
to_filename,
context_size,
true,
false,
);
let expected_brief = ["*** foo\t", "--- bar\t", ""].join("\n");
assert_eq!(diff_brief, expected_brief.as_bytes());
Expand All @@ -751,6 +766,7 @@ mod tests {
to_filename,
context_size,
false,
false,
);
assert!(nodiff_full.is_empty());

Expand All @@ -761,6 +777,7 @@ mod tests {
to_filename,
context_size,
true,
false,
);
assert!(nodiff_brief.is_empty());
}
Expand Down
23 changes: 15 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,12 @@ 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,
) -> 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 +152,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).unwrap();
writeln!(&mut output).unwrap();
}
}
Expand All @@ -160,7 +167,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)?;
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}
Expand All @@ -169,7 +176,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).unwrap();
let expected = ["1c", "b", ".", ""].join("\n");
assert_eq!(diff, expected.as_bytes());
}
Expand Down Expand Up @@ -404,18 +411,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).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).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).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).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
16 changes: 11 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,7 @@ fn main() -> ExitCode {
format,
report_identical_files,
brief,
expand_tabs,
} = parse_params(opts).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
Expand Down Expand Up @@ -65,14 +67,15 @@ 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),
Format::Unified => unified_diff::diff(
&from_content,
&from.to_string_lossy(),
&to_content,
&to.to_string_lossy(),
context_count,
brief,
expand_tabs,
),
Format::Context => context_diff::diff(
&from_content,
Expand All @@ -81,11 +84,14 @@ fn main() -> ExitCode {
&to.to_string_lossy(),
context_count,
brief,
expand_tabs,
),
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).unwrap_or_else(|error| {
eprintln!("{error}");
exit(2);
})
}
};
if brief && !result.is_empty() {
println!(
Expand Down
26 changes: 14 additions & 12 deletions src/normal_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 @@ -114,7 +116,7 @@ fn make_diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec<Mismatch>
}

#[must_use]
pub fn diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec<u8> {
pub fn diff(expected: &[u8], actual: &[u8], stop_early: bool, expand_tabs: bool) -> 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();
Expand Down Expand Up @@ -188,7 +190,7 @@ pub fn diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec<u8> {
}
for expected in &result.expected {
write!(&mut output, "< ").unwrap();
output.write_all(expected).unwrap();
do_write_line(&mut output, expected, expand_tabs).unwrap();
writeln!(&mut output).unwrap();
}
if result.expected_missing_nl {
Expand All @@ -199,7 +201,7 @@ pub fn diff(expected: &[u8], actual: &[u8], stop_early: bool) -> Vec<u8> {
}
for actual in &result.actual {
write!(&mut output, "> ").unwrap();
output.write_all(actual).unwrap();
do_write_line(&mut output, actual, expand_tabs).unwrap();
writeln!(&mut output).unwrap();
}
if result.actual_missing_nl {
Expand All @@ -220,7 +222,7 @@ mod tests {
a.write_all(b"a\n").unwrap();
let mut b = Vec::new();
b.write_all(b"b\n").unwrap();
let diff = diff(&a, &b, false);
let diff = diff(&a, &b, false, false);
let expected = b"1c1\n< a\n---\n> b\n".to_vec();
assert_eq!(diff, expected);
}
Expand Down Expand Up @@ -273,7 +275,7 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(&alef, &bet, false);
let diff = diff(&alef, &bet, false, false);
File::create(&format!("{target}/ab.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -365,7 +367,7 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(&alef, &bet, false);
let diff = diff(&alef, &bet, false, false);
File::create(&format!("{target}/abn.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -439,7 +441,7 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(&alef, &bet, false);
let diff = diff(&alef, &bet, false, false);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -517,7 +519,7 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff = diff(&alef, &bet, false);
let diff = diff(&alef, &bet, false, false);
File::create(&format!("{target}/abr.diff"))
.unwrap()
.write_all(&diff)
Expand Down Expand Up @@ -552,18 +554,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);
let diff_full = diff(from.as_bytes(), to.as_bytes(), false, false);
let expected_full = ["2c2", "< b", "---", "> d", ""].join("\n");
assert_eq!(diff_full, expected_full.as_bytes());

let diff_brief = diff(from.as_bytes(), to.as_bytes(), true);
let diff_brief = diff(from.as_bytes(), to.as_bytes(), true, false);
let expected_brief = "\0".as_bytes();
assert_eq!(diff_brief, expected_brief);

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

let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true);
let nodiff_brief = diff(from.as_bytes(), from.as_bytes(), true, false);
assert!(nodiff_brief.is_empty());
}
}
Loading