1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

wc: Avoid unnecessary work in general loop

This gives a big speedup if e.g. only characters are being counted.
This commit is contained in:
Jan Verbeek 2021-08-25 18:11:54 +02:00 committed by Michael Debertol
parent f50b004860
commit 1358aeecdd

View file

@ -235,6 +235,7 @@ fn word_count_from_reader<T: WordCountable>(
match chunk { match chunk {
Ok(text) => { Ok(text) => {
for ch in text.chars() { for ch in text.chars() {
if settings.show_words {
if ch.is_whitespace() { if ch.is_whitespace() {
in_word = false; in_word = false;
} else if ch.is_ascii_control() { } else if ch.is_ascii_control() {
@ -243,11 +244,12 @@ fn word_count_from_reader<T: WordCountable>(
in_word = true; in_word = true;
total.words += 1; total.words += 1;
} }
}
if settings.show_max_line_length {
match ch { match ch {
'\n' => { '\n' => {
total.max_line_length = max(current_len, total.max_line_length); total.max_line_length = max(current_len, total.max_line_length);
current_len = 0; current_len = 0;
total.lines += 1;
} }
// '\x0c' = '\f' // '\x0c' = '\f'
'\r' | '\x0c' => { '\r' | '\x0c' => {
@ -262,8 +264,14 @@ fn word_count_from_reader<T: WordCountable>(
current_len += ch.width().unwrap_or(0); current_len += ch.width().unwrap_or(0);
} }
} }
}
if settings.show_lines && ch == '\n' {
total.lines += 1;
}
if settings.show_chars {
total.chars += 1; total.chars += 1;
} }
}
total.bytes += text.len(); total.bytes += text.len();
} }
Err(BufReadDecoderError::InvalidByteSequence(bytes)) => { Err(BufReadDecoderError::InvalidByteSequence(bytes)) => {