Skip to content

diff: add -D/--ifdef format support - #422

Open
yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:diff-ifdef
Open

yunusarda-sudo wants to merge 2 commits into
uutils:mainfrom
yunusarda-sudo:diff-ifdef

Conversation

@yunusarda-sudo

Copy link
Copy Markdown

Implement -D NAME and --ifdef=NAME to output merged files with C preprocessor conditional directives (#ifndef, #ifdef, #else, and #endif), matching GNU diff.

Closes #410

@github-actions

Copy link
Copy Markdown

GNU diffutils testsuite comparison:

Test results comparison:
  Current:   TOTAL: 33 / PASSED: 20 / FAILED: 9 / SKIPPED: 4
  Reference: TOTAL: 33 / PASSED: 19 / FAILED: 10 / SKIPPED: 4

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + ifdef

Comment thread src/ifdef_diff.rs
&mut differs,
);
do_write_line(&mut output, str1, params.expand_tabs, params.tabsize).unwrap();
output.push(b'\n');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this always adds a '\n', so identical files a\nb (no final newline) give a\nb\n.
GNU keeps the file as is, no?
please add a test with a missing newline at EOF, on one side and on both

Comment thread src/diff.rs Outdated
let (result, differs) = match params.format {
Format::Normal => {
let res = normal_diff::diff(&from_content, &to_content, params);
let differs = !res.is_empty();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same code 5 times :)
could the match keep returning a Vec<u8>, with differs only special-cased for Ifdef?

Comment thread src/diff.rs Outdated
if !result.is_empty() {
let res = side_diff::diff(&from_content, &to_content, &mut output, params);
let differs = !res.is_empty();
if !res.is_empty() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if differs?

Comment thread src/ifdef_diff.rs Outdated
match (!current_expected.is_empty(), !current_actual.is_empty()) {
(true, false) => {
writeln!(output, "#ifndef {ifdef_name}").unwrap();
for line in current_expected.drain(..) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same loop 4 times, could you please move it into a small helper?

Comment thread src/ifdef_diff.rs Outdated
let mut line_number_actual = 1;
let mut differs = false;

let mut current_expected: Vec<Vec<u8>> = Vec::new();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why copy each line? Vec<&[u8]> should be enough

Comment thread src/params.rs Outdated
}
format = Some(Format::Ifdef);
let val = argument_value(argument, &param, &mut opts)?;
params.ifdef_name = Some(val.to_string_lossy().into_owned());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep it as OsString (or bytes), a non-UTF-8 name gets mangled here

@sylvestre

Copy link
Copy Markdown
Collaborator

Could you please verify the output of your LLM ?
it isn't great here :(

- preserve non-UTF-8 macro names using OsString and raw byte parsing
- deduplicate diff format matching in compare_files
- eliminate line-copy allocations in ifdef diff by storing &[u8] slices
- extract helper functions for ifdef directive formatting and line output
- add regression tests for non-UTF-8 macro names and missing newlines at EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diff - -D / --ifdef output format is not implemented

2 participants