summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-06-20 20:14:29 +0300
committerA Farzat <a@farzat.xyz>2026-06-21 11:19:03 +0300
commitea5c36fff4922c06181e1942d7db36ebb96cfd1b (patch)
treed7e6bce57154bd57570da12c8a2aefedb07df5da
parent43454f5035be9b07e71596dfddad271db5061495 (diff)
downloadrepo2markdown-ea5c36fff4922c06181e1942d7db36ebb96cfd1b.tar.gz
repo2markdown-ea5c36fff4922c06181e1942d7db36ebb96cfd1b.zip
Make append_paths preserve stream if no additions
-rw-r--r--src/path_list_editor.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/path_list_editor.rs b/src/path_list_editor.rs
index 1ae4816..d605d25 100644
--- a/src/path_list_editor.rs
+++ b/src/path_list_editor.rs
@@ -25,8 +25,7 @@ const NUL: &[u8] = b"\0";
///
/// This function is defensive about unterminated input: if `input` is non-empty and does not end
/// with a NUL byte, a NUL byte is inserted before appended paths are written. If `new_paths` is
-/// empty, this still has the effect of repairing a non-empty unterminated input stream into a
-/// NUL-terminated one.
+/// empty, the input stream is passed through unchanged.
///
/// This function does not normalize, deduplicate, or validate paths.
pub fn append_paths<R: Read, W: Write>(
@@ -36,7 +35,7 @@ pub fn append_paths<R: Read, W: Write>(
) -> std::io::Result<()> {
let last_byte_in_input = copy_while_tracking_last_byte(input, &mut output)?;
// Defensively ensure non-empty input is NUL-terminated.
- if matches!(last_byte_in_input, Some(byte) if byte != 0) {
+ if !new_paths.is_empty() && matches!(last_byte_in_input, Some(byte) if byte != 0) {
output.write_all(NUL)?;
}
for path in new_paths {
@@ -219,6 +218,16 @@ mod tests {
}
#[test]
+ fn append_paths_does_not_insert_nul_when_no_new_paths_and_input_not_terminated() {
+ let input = b"a.rs";
+ let mut output = Vec::new();
+
+ append_paths(&input[..], &mut output, &[]).unwrap();
+
+ assert_eq!(output, b"a.rs");
+ }
+
+ #[test]
fn prepend_paths_works_with_empty_stdin_and_args() {
let input = b"";
let mut output = Vec::new();