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:
parent
f50b004860
commit
1358aeecdd
1 changed files with 34 additions and 26 deletions
|
@ -235,34 +235,42 @@ 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 ch.is_whitespace() {
|
if settings.show_words {
|
||||||
in_word = false;
|
if ch.is_whitespace() {
|
||||||
} else if ch.is_ascii_control() {
|
in_word = false;
|
||||||
// These count as characters but do not affect the word state
|
} else if ch.is_ascii_control() {
|
||||||
} else if !in_word {
|
// These count as characters but do not affect the word state
|
||||||
in_word = true;
|
} else if !in_word {
|
||||||
total.words += 1;
|
in_word = true;
|
||||||
}
|
total.words += 1;
|
||||||
match ch {
|
|
||||||
'\n' => {
|
|
||||||
total.max_line_length = max(current_len, total.max_line_length);
|
|
||||||
current_len = 0;
|
|
||||||
total.lines += 1;
|
|
||||||
}
|
|
||||||
// '\x0c' = '\f'
|
|
||||||
'\r' | '\x0c' => {
|
|
||||||
total.max_line_length = max(current_len, total.max_line_length);
|
|
||||||
current_len = 0;
|
|
||||||
}
|
|
||||||
'\t' => {
|
|
||||||
current_len -= current_len % 8;
|
|
||||||
current_len += 8;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
current_len += ch.width().unwrap_or(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
total.chars += 1;
|
if settings.show_max_line_length {
|
||||||
|
match ch {
|
||||||
|
'\n' => {
|
||||||
|
total.max_line_length = max(current_len, total.max_line_length);
|
||||||
|
current_len = 0;
|
||||||
|
}
|
||||||
|
// '\x0c' = '\f'
|
||||||
|
'\r' | '\x0c' => {
|
||||||
|
total.max_line_length = max(current_len, total.max_line_length);
|
||||||
|
current_len = 0;
|
||||||
|
}
|
||||||
|
'\t' => {
|
||||||
|
current_len -= current_len % 8;
|
||||||
|
current_len += 8;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
current_len += ch.width().unwrap_or(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if settings.show_lines && ch == '\n' {
|
||||||
|
total.lines += 1;
|
||||||
|
}
|
||||||
|
if settings.show_chars {
|
||||||
|
total.chars += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
total.bytes += text.len();
|
total.bytes += text.len();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue