1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

cat: remove all per-file state

cat cannot keep per-file state, so move all remaining state (one_blank_kept)
to the global state.
This commit is contained in:
Michael Debertol 2021-08-07 21:31:05 +02:00
parent 5be4c48546
commit 7229360217
2 changed files with 19 additions and 4 deletions

View file

@ -126,6 +126,9 @@ struct OutputState {
/// Whether we skipped a \r, which still needs to be printed /// Whether we skipped a \r, which still needs to be printed
skipped_carriage_return: bool, skipped_carriage_return: bool,
/// Whether we have already printed a blank line
one_blank_kept: bool,
} }
/// Represents an open file handle, stream, or other device /// Represents an open file handle, stream, or other device
@ -343,6 +346,7 @@ fn cat_files(files: Vec<String>, options: &OutputOptions) -> UResult<()> {
line_number: 1, line_number: 1,
at_line_start: true, at_line_start: true,
skipped_carriage_return: false, skipped_carriage_return: false,
one_blank_kept: false,
}; };
let mut error_messages: Vec<String> = Vec::new(); let mut error_messages: Vec<String> = Vec::new();
@ -431,7 +435,6 @@ fn write_lines<R: Read>(
let mut in_buf = [0; 1024 * 31]; let mut in_buf = [0; 1024 * 31];
let stdout = io::stdout(); let stdout = io::stdout();
let mut writer = stdout.lock(); let mut writer = stdout.lock();
let mut one_blank_kept = false;
while let Ok(n) = handle.reader.read(&mut in_buf) { while let Ok(n) = handle.reader.read(&mut in_buf) {
if n == 0 { if n == 0 {
@ -447,8 +450,8 @@ fn write_lines<R: Read>(
writer.write_all(b"^M")?; writer.write_all(b"^M")?;
state.skipped_carriage_return = false; state.skipped_carriage_return = false;
} }
if !state.at_line_start || !options.squeeze_blank || !one_blank_kept { if !state.at_line_start || !options.squeeze_blank || !state.one_blank_kept {
one_blank_kept = true; state.one_blank_kept = true;
if state.at_line_start && options.number == NumberingMode::All { if state.at_line_start && options.number == NumberingMode::All {
write!(&mut writer, "{0:6}\t", state.line_number)?; write!(&mut writer, "{0:6}\t", state.line_number)?;
state.line_number += 1; state.line_number += 1;
@ -467,7 +470,7 @@ fn write_lines<R: Read>(
state.skipped_carriage_return = false; state.skipped_carriage_return = false;
state.at_line_start = false; state.at_line_start = false;
} }
one_blank_kept = false; state.one_blank_kept = false;
if state.at_line_start && options.number != NumberingMode::None { if state.at_line_start && options.number != NumberingMode::None {
write!(&mut writer, "{0:6}\t", state.line_number)?; write!(&mut writer, "{0:6}\t", state.line_number)?;
state.line_number += 1; state.line_number += 1;

View file

@ -273,6 +273,18 @@ fn test_stdin_show_ends() {
.stdout_only("\t\0$\n\t"); .stdout_only("\t\0$\n\t");
} }
} }
#[test]
fn squeeze_all_files() {
// empty lines at the end of a file are "squeezed" together with empty lines at the beginning
let (at, mut ucmd) = at_and_ucmd!();
at.write("input1", "a\n\n");
at.write("input2", "\n\nb");
ucmd.args(&["input1", "input2", "-s"])
.succeeds()
.stdout_only("a\n\nb");
}
#[test] #[test]
fn test_show_ends_crlf() { fn test_show_ends_crlf() {
new_ucmd!() new_ucmd!()