Repository navigation
diff: add -D/--ifdef format support - #422
yunusarda-sudo wants to merge 4 commits into
Conversation
|
GNU diffutils testsuite comparison: |
| &mut differs, | ||
| ); | ||
| do_write_line(&mut output, str1, params.expand_tabs, params.tabsize).unwrap(); | ||
| output.push(b'\n'); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
I checked GNU diff 3.10 on this. diff -D FOO on identical files whose last line has no newline (a\nb) writes a\nb\n and exits 0 — it does not keep the raw missing-newline bytes. The same extra newline is used when both sides lack a newline but the last lines differ. When only one side has a newline, GNU wraps that last line in #ifndef / #else, and ifdef_diff_missing_newline covers all three cases.
| let (result, differs) = match params.format { | ||
| Format::Normal => { | ||
| let res = normal_diff::diff(&from_content, &to_content, params); | ||
| let differs = !res.is_empty(); |
There was a problem hiding this comment.
almost the same code 5 times :)
could the match keep returning a Vec<u8>, with differs only special-cased for Ifdef?
There was a problem hiding this comment.
Done — compare_files now keeps the match returning a Vec<u8>, and differs is only special-cased for Ifdef.
| 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() { |
There was a problem hiding this comment.
Done — the side-by-side header path now uses if differs.
| match (!current_expected.is_empty(), !current_actual.is_empty()) { | ||
| (true, false) => { | ||
| writeln!(output, "#ifndef {ifdef_name}").unwrap(); | ||
| for line in current_expected.drain(..) { |
There was a problem hiding this comment.
the same loop 4 times, could you please move it into a small helper?
There was a problem hiding this comment.
Done — the four directive/line loops now go through flush_hunk, write_directive and write_lines.
| let mut line_number_actual = 1; | ||
| let mut differs = false; | ||
|
|
||
| let mut current_expected: Vec<Vec<u8>> = Vec::new(); |
There was a problem hiding this comment.
why copy each line? Vec<&[u8]> should be enough
There was a problem hiding this comment.
Done — the hunk buffers are Vec<&[u8]> now.
| } | ||
| format = Some(Format::Ifdef); | ||
| let val = argument_value(argument, ¶m, &mut opts)?; | ||
| params.ifdef_name = Some(val.to_string_lossy().into_owned()); |
There was a problem hiding this comment.
please keep it as OsString (or bytes), a non-UTF-8 name gets mangled here
There was a problem hiding this comment.
Done — ifdef_name is an OsString, and the output uses OsStr::as_encoded_bytes. ifdef_diff_non_utf8_macro covers a non-UTF-8 -D value.
|
Could you please verify the output of your LLM ? |
654600a to
4831b43
Compare
|
Updated and rebased onto latest |
31cca27 to
998758b
Compare
|
Thanks for the review — the current patch takes the inline comments:
|
| } | ||
| } | ||
| if result.is_empty() { | ||
| let differs = if params.format == Format::Ifdef { |
There was a problem hiding this comment.
differs is already computed above, could you reuse it?
There was a problem hiding this comment.
Done — the exit path now reuses the differs computed after the format match.
| write_lines(output, current_actual, expand_tabs, tabsize); | ||
| write_directive(output, b"#endif /* ", ifdef_name, Some(b" */")); | ||
| } | ||
| (false, false) => unreachable!(), |
There was a problem hiding this comment.
with the early return above, could (false, false) => return false replace both?
There was a problem hiding this comment.
Done — the empty case is handled by (false, false) => return false, and the early return is gone.
013e8b2 to
0dcc6bf
Compare
|
|
||
| fn write_lines(output: &mut Vec<u8>, lines: &[&[u8]], expand_tabs: bool, tabsize: usize) { | ||
| for &line in lines { | ||
| do_write_line(output, line, expand_tabs, tabsize, b"").unwrap(); |
There was a problem hiding this comment.
please avoid unwrap(), unified_diff uses .expect("write to Vec is infallible")
There was a problem hiding this comment.
Done — writes to the Vec now use .expect("write to Vec is infallible").
| continue; | ||
| } | ||
| // -D NAME, -DNAME, --ifdef NAME or --ifdef=NAME | ||
| let ifdef = if bytes == b"-D" || bytes == b"--ifdef" { |
There was a problem hiding this comment.
almost the same code as the -L block above, could it be a small helper?
There was a problem hiding this comment.
Done — -L/--label and -D/--ifdef now share option_os_value.
Reuse the already-computed differs flag for the exit outcome. In flush_hunk, return false from the (false, false) arm and drop the matching early return.
Parse -L/--label and -D/--ifdef through a shared helper, and use
expect("write to Vec is infallible") for ifdef output writes.
0dcc6bf to
b985cb4
Compare
Implement
-D NAMEand--ifdef=NAMEto output merged files with C preprocessor conditional directives (#ifndef,#ifdef,#else, and#endif), matching GNU diff.Closes #410