mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
Fixed wc -L no end of line LF bug (#1714)
This commit is contained in:
parent
749c794bf6
commit
6c2bca110d
2 changed files with 14 additions and 8 deletions
|
@ -177,7 +177,7 @@ fn wc(files: Vec<String>, settings: &Settings) -> StdResult<(), i32> {
|
|||
let mut char_count: usize = 0;
|
||||
let mut longest_line_length: usize = 0;
|
||||
let mut raw_line = Vec::new();
|
||||
|
||||
let mut ends_lf: bool;
|
||||
// reading from a TTY seems to raise a condition on, rather than return Some(0) like a file.
|
||||
// hence the option wrapped in a result here
|
||||
while match reader.read_until(LF, &mut raw_line) {
|
||||
|
@ -189,9 +189,8 @@ fn wc(files: Vec<String>, settings: &Settings) -> StdResult<(), i32> {
|
|||
_ => false,
|
||||
} {
|
||||
// GNU 'wc' only counts lines that end in LF as lines
|
||||
if *raw_line.last().unwrap() == LF {
|
||||
line_count += 1;
|
||||
}
|
||||
ends_lf = *raw_line.last().unwrap() == LF;
|
||||
line_count += ends_lf as usize;
|
||||
|
||||
byte_count += raw_line.len();
|
||||
|
||||
|
@ -209,11 +208,9 @@ fn wc(files: Vec<String>, settings: &Settings) -> StdResult<(), i32> {
|
|||
}
|
||||
}
|
||||
char_count += current_char_count;
|
||||
|
||||
if current_char_count > longest_line_length {
|
||||
// we subtract one here because `line.len()` includes the LF
|
||||
// matches GNU 'wc' behavior
|
||||
longest_line_length = current_char_count - 1;
|
||||
// -L is a GNU 'wc' extension so same behavior on LF
|
||||
longest_line_length = current_char_count - (ends_lf as usize);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,15 @@ fn test_stdin_default() {
|
|||
.stdout_is(" 13 109 772\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stdin_line_len_regression() {
|
||||
new_ucmd!()
|
||||
.args(&["-L"])
|
||||
.pipe_in("\n123456")
|
||||
.run()
|
||||
.stdout_is(" 6\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_stdin_only_bytes() {
|
||||
new_ucmd!()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue