Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/uu/wc/src/wc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,18 @@ fn process_chunk<
total.max_line_length = max(*current_len, total.max_line_length);
}

fn handle_error(error: BufReadDecoderError<'_>, total: &mut WordCount) -> Option<io::Error> {
fn handle_error(
error: BufReadDecoderError<'_>,
total: &mut WordCount,
in_word: &mut bool,
) -> Option<io::Error> {
match error {
BufReadDecoderError::InvalidByteSequence(bytes) => {
total.bytes += bytes.len();
if !(*in_word) {
*in_word = true;
total.words += 1;
}
}
BufReadDecoderError::Io(e) => return Some(e),
}
Expand Down Expand Up @@ -650,7 +658,7 @@ fn word_count_from_reader_specialized<
);
}
Err(e) => {
if let Some(e) = handle_error(e, &mut total) {
if let Some(e) = handle_error(e, &mut total, &mut in_word) {
return (total, Some(e));
}
}
Expand Down
12 changes: 11 additions & 1 deletion tests/by-util/test_wc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn test_utf8() {
.args(&["-lwmcL"])
.pipe_in_fixture("UTF_8_test.txt")
.succeeds()
.stdout_is(" 303 2119 22457 23025 79\n");
.stdout_is(" 303 2178 22457 23025 79\n");
}

#[test]
Expand Down Expand Up @@ -826,6 +826,16 @@ fn wc_w_words_with_emoji_separator() {
.stdout_contains("3");
}

#[test]
fn test_invalid_byte_sequence_word_count() {
// wc should count invalid byte sequences as words
// Input: "a \xff b\n" should produce: 1 line, 3 words, 6 bytes
new_ucmd!()
.pipe_in([b'a', b' ', 0xff, b' ', b'b', b'\n'])
.succeeds()
.stdout_is(" 1 3 6\n");
}

#[cfg(unix)]
#[test]
fn test_simd_respects_glibc_tunables() {
Expand Down
Loading