mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
cat: fix for numbered lines w/ no trailing newline
Make at_line_start persist between printing each file. This fixes an issue when numbering lines in the output and one of the input files does not have a trailing newline.
This commit is contained in:
parent
4a16514d2c
commit
81996915df
3 changed files with 22 additions and 14 deletions
|
@ -304,9 +304,10 @@ fn write_fast(files: Vec<String>) -> CatResult<()> {
|
||||||
fn write_lines(files: Vec<String>, options: &OutputOptions) -> CatResult<()> {
|
fn write_lines(files: Vec<String>, options: &OutputOptions) -> CatResult<()> {
|
||||||
let mut line_counter: usize = 1;
|
let mut line_counter: usize = 1;
|
||||||
let mut error_count = 0;
|
let mut error_count = 0;
|
||||||
|
let mut at_line_start = true;
|
||||||
|
|
||||||
for file in files {
|
for file in files {
|
||||||
let written = write_file_lines(&file[..], options, line_counter);
|
let written = write_file_lines(&file[..], options, line_counter, &mut at_line_start);
|
||||||
line_counter += match written {
|
line_counter += match written {
|
||||||
Ok(lines) => lines,
|
Ok(lines) => lines,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
|
@ -326,16 +327,13 @@ fn write_lines(files: Vec<String>, options: &OutputOptions) -> CatResult<()> {
|
||||||
/// Outputs file and returns result with the number of lines to stdout
|
/// Outputs file and returns result with the number of lines to stdout
|
||||||
/// from `file`. If line numbering is enabled, then start output
|
/// from `file`. If line numbering is enabled, then start output
|
||||||
/// numbering at `line_number`.
|
/// numbering at `line_number`.
|
||||||
///
|
fn write_file_lines(file: &str,
|
||||||
/// # Arguments
|
options: &OutputOptions,
|
||||||
///
|
line_number: usize,
|
||||||
/// * `files` - There is no short circuit when encountiner an error
|
at_line_start: &mut bool) -> CatResult<usize> {
|
||||||
/// reading a file in this vector
|
|
||||||
fn write_file_lines(file: &str, options: &OutputOptions, line_number: usize) -> CatResult<usize> {
|
|
||||||
let mut handle = open(&file[..])?;
|
let mut handle = open(&file[..])?;
|
||||||
let mut in_buf = [0; 1024 * 31];
|
let mut in_buf = [0; 1024 * 31];
|
||||||
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
|
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
|
||||||
let mut at_line_start = true;
|
|
||||||
let mut one_blank_kept = false;
|
let mut one_blank_kept = false;
|
||||||
|
|
||||||
let mut lines = 0;
|
let mut lines = 0;
|
||||||
|
@ -349,9 +347,9 @@ fn write_file_lines(file: &str, options: &OutputOptions, line_number: usize) ->
|
||||||
while pos < n {
|
while pos < n {
|
||||||
// skip empty line_number enumerating them if needed
|
// skip empty line_number enumerating them if needed
|
||||||
if in_buf[pos] == '\n' as u8 {
|
if in_buf[pos] == '\n' as u8 {
|
||||||
if !at_line_start || ! options.squeeze_blank || !one_blank_kept {
|
if !*at_line_start || ! options.squeeze_blank || !one_blank_kept {
|
||||||
one_blank_kept = true;
|
one_blank_kept = true;
|
||||||
if at_line_start && options.number == NumberingMode::NumberAll {
|
if *at_line_start && options.number == NumberingMode::NumberAll {
|
||||||
write!(&mut writer, "{0:6}\t", line_number + lines)?;
|
write!(&mut writer, "{0:6}\t", line_number + lines)?;
|
||||||
lines += 1;
|
lines += 1;
|
||||||
}
|
}
|
||||||
|
@ -360,12 +358,12 @@ fn write_file_lines(file: &str, options: &OutputOptions, line_number: usize) ->
|
||||||
writer.flush().context(&file[..])?;
|
writer.flush().context(&file[..])?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
at_line_start = true;
|
*at_line_start = true;
|
||||||
pos += 1;
|
pos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
one_blank_kept = false;
|
one_blank_kept = false;
|
||||||
if at_line_start && options.number != NumberingMode::NumberNone {
|
if *at_line_start && options.number != NumberingMode::NumberNone {
|
||||||
write!(&mut writer, "{0:6}\t", line_number + lines)?;
|
write!(&mut writer, "{0:6}\t", line_number + lines)?;
|
||||||
lines += 1;
|
lines += 1;
|
||||||
}
|
}
|
||||||
|
@ -380,7 +378,7 @@ fn write_file_lines(file: &str, options: &OutputOptions, line_number: usize) ->
|
||||||
};
|
};
|
||||||
// end of buffer?
|
// end of buffer?
|
||||||
if offset == 0 {
|
if offset == 0 {
|
||||||
at_line_start = false;
|
*at_line_start = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// print suitable end of line
|
// print suitable end of line
|
||||||
|
@ -388,7 +386,7 @@ fn write_file_lines(file: &str, options: &OutputOptions, line_number: usize) ->
|
||||||
if handle.is_interactive {
|
if handle.is_interactive {
|
||||||
writer.flush()?;
|
writer.flush()?;
|
||||||
}
|
}
|
||||||
at_line_start = true;
|
*at_line_start = true;
|
||||||
pos += offset;
|
pos += offset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
tests/fixtures/cat/nonewline.txt
vendored
Normal file
1
tests/fixtures/cat/nonewline.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
text without a trailing newline
|
|
@ -22,6 +22,15 @@ fn test_output_multi_files_print_all_chars() {
|
||||||
pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?");
|
pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_numbered_lines_no_trailing_newline() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["nonewline.txt", "alpha.txt", "-n"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only(" 1\ttext without a trailing newlineabcde\n 2\tfghij\n \
|
||||||
|
3\tklmno\n 4\tpqrst\n 5\tuvwxyz\n");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_stdin_show_nonprinting() {
|
fn test_stdin_show_nonprinting() {
|
||||||
for same_param in vec!["-v", "--show-nonprinting"] {
|
for same_param in vec!["-v", "--show-nonprinting"] {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue