Repository navigation
paste: preserve readable columns after input errors #17052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ use uucore::error::{FromIo, UResult, USimpleError}; | |
| use uucore::format_usage; | ||
| use uucore::i18n::charmap::mb_char_len; | ||
| use uucore::line_ending::LineEnding; | ||
| use uucore::translate; | ||
| use uucore::{show_if_err, translate}; | ||
|
|
||
| mod options { | ||
| pub const DELIMITER: &str = "delimiters"; | ||
|
|
@@ -158,32 +158,24 @@ fn paste( | |
| delimiter_state.reset_to_first_delimiter(); | ||
| } | ||
| } else { | ||
| let mut eof = vec![false; input_source_vec_len]; | ||
| let mut columns: Vec<_> = input_source_vec | ||
| .into_iter() | ||
| .map(|source| Column { | ||
| source: Some(source), | ||
| read_error: Ok(()), | ||
| }) | ||
| .collect(); | ||
|
|
||
| loop { | ||
| output.clear(); | ||
|
|
||
| let mut eof_count = 0; | ||
|
|
||
| for (i, input_source) in input_source_vec.iter_mut().enumerate() { | ||
| if eof[i] { | ||
| eof_count += 1; | ||
| } else { | ||
| match input_source.read_until(line_ending_byte, &mut output)? { | ||
| 0 => { | ||
| eof[i] = true; | ||
| eof_count += 1; | ||
| } | ||
| read => { | ||
| remove_trailing_line_ending_byte(line_ending_byte, read, &mut output); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut has_record = false; | ||
| for column in &mut columns { | ||
| has_record |= column.read_field(line_ending_byte, &mut output); | ||
| delimiter_state.write_delimiter(&mut output); | ||
| } | ||
|
|
||
| if eof_count == input_source_vec_len { | ||
| if !has_record { | ||
| break; | ||
| } | ||
|
|
||
|
|
@@ -204,27 +196,70 @@ fn paste( | |
| Ok(()) | ||
| } | ||
|
|
||
| struct Column { | ||
| source: Option<InputSource>, | ||
| read_error: UResult<()>, | ||
| } | ||
|
|
||
| impl Column { | ||
| /// Append this column's next field and return whether the row has input here. | ||
| fn read_field(&mut self, line_ending: u8, output: &mut Vec<u8>) -> bool { | ||
| let Some(source) = &mut self.source else { | ||
| return false; | ||
| }; | ||
|
|
||
| let start = output.len(); | ||
| let read = match source.read_until(line_ending, output) { | ||
| Ok(read) => read, | ||
| Err(err) => { | ||
| // Keep any bytes read before the error as this row's field. | ||
| // The next row can still get data if the error was transient. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why keep reading after an error? closing the column and showing the error right away would drop the |
||
| self.read_error = Err(err); | ||
| output.len() - start | ||
| } | ||
| }; | ||
| if read == 0 { | ||
| self.source = None; | ||
| show_if_err!(&self.read_error); | ||
| return false; | ||
| } | ||
| remove_trailing_line_ending_byte(line_ending, read, output); | ||
| true | ||
| } | ||
| } | ||
|
|
||
| fn write_single_input_source( | ||
| writer: &mut impl Write, | ||
| mut input_source: InputSource, | ||
| line_ending_byte: u8, | ||
| ) -> UResult<()> { | ||
| let mut buffer = [0_u8; 8 * 1024]; | ||
| let mut has_data = false; | ||
| let mut last_byte = line_ending_byte; | ||
|
|
||
| while let bytes_read @ 1.. = input_source.read(&mut buffer)? { | ||
| has_data = true; | ||
| last_byte = buffer[bytes_read - 1]; | ||
|
|
||
| writer.write_all(&buffer[..bytes_read])?; | ||
| let mut needs_terminator = false; | ||
| let mut read_error = Ok(()); | ||
|
|
||
| loop { | ||
| match input_source.read(&mut buffer) { | ||
| Ok(0) => break, | ||
| Ok(n) => { | ||
| writer.write_all(&buffer[..n])?; | ||
| needs_terminator = buffer[n - 1] != line_ending_byte; | ||
| } | ||
| Err(err) => { | ||
| read_error = Err(err); | ||
| if !needs_terminator { | ||
| break; | ||
| } | ||
| // Finish the partial record and try reading the next one. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, a |
||
| writer.write_all(&[line_ending_byte])?; | ||
| needs_terminator = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if has_data && last_byte != line_ending_byte { | ||
| if needs_terminator { | ||
| writer.write_all(&[line_ending_byte])?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| read_error | ||
| } | ||
|
|
||
| fn parse_delimiters(delimiters: &OsString) -> UResult<Box<[Box<[u8]>]>> { | ||
|
|
@@ -394,7 +429,8 @@ impl InputSource { | |
| USimpleError::new(1, translate!("paste-error-stdin-borrow", "error" => bo)) | ||
| })? | ||
| .lock() | ||
| .read(buf)?, | ||
| .read(buf) | ||
| .map_err_context(|| "-".to_string())?, | ||
| }; | ||
|
|
||
| Ok(us) | ||
|
|
@@ -411,7 +447,8 @@ impl InputSource { | |
| USimpleError::new(1, translate!("paste-error-stdin-borrow", "error" => bo)) | ||
| })? | ||
| .lock() | ||
| .read_until(byte, buf)?, | ||
| .read_until(byte, buf) | ||
| .map_err_context(|| "-".to_string())?, | ||
| }; | ||
|
|
||
| Ok(us) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
-s?paste -s left dir1 rightstill stops at dir1