diff --git a/src/uu/wc/src/wc.rs b/src/uu/wc/src/wc.rs index c6d5d72d8..68e955f81 100644 --- a/src/uu/wc/src/wc.rs +++ b/src/uu/wc/src/wc.rs @@ -177,7 +177,7 @@ fn wc(files: Vec, 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, 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, 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); } } diff --git a/tests/by-util/test_wc.rs b/tests/by-util/test_wc.rs index 43dbbdb15..a0fe8889b 100644 --- a/tests/by-util/test_wc.rs +++ b/tests/by-util/test_wc.rs @@ -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!()