mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
wc: respect POSIXLY_CORRECT for word counting (#10344)
* wc: respect POSIXLY_CORRECT for word counting * wc: Add test for POSIXLY_CORRECT word counting
This commit is contained in:
+11
-1
@@ -13,6 +13,7 @@ mod word_count;
|
||||
use std::{
|
||||
borrow::{Borrow, Cow},
|
||||
cmp::max,
|
||||
env,
|
||||
ffi::{OsStr, OsString},
|
||||
fs::{self, File},
|
||||
io::{self, Write},
|
||||
@@ -578,10 +579,17 @@ fn process_chunk<
|
||||
text: &str,
|
||||
current_len: &mut usize,
|
||||
in_word: &mut bool,
|
||||
posixly_correct: bool,
|
||||
) {
|
||||
for ch in text.chars() {
|
||||
if SHOW_WORDS {
|
||||
if ch.is_whitespace() {
|
||||
let is_space = if posixly_correct {
|
||||
matches!(ch, '\t'..='\r' | ' ')
|
||||
} else {
|
||||
ch.is_whitespace()
|
||||
};
|
||||
|
||||
if is_space {
|
||||
*in_word = false;
|
||||
} else if !(*in_word) {
|
||||
// This also counts control characters! (As of GNU coreutils 9.5)
|
||||
@@ -639,6 +647,7 @@ fn word_count_from_reader_specialized<
|
||||
let mut reader = BufReadDecoder::new(reader.buffered());
|
||||
let mut in_word = false;
|
||||
let mut current_len = 0;
|
||||
let posixly_correct = env::var_os("POSIXLY_CORRECT").is_some();
|
||||
while let Some(chunk) = reader.next_strict() {
|
||||
match chunk {
|
||||
Ok(text) => {
|
||||
@@ -647,6 +656,7 @@ fn word_count_from_reader_specialized<
|
||||
text,
|
||||
&mut current_len,
|
||||
&mut in_word,
|
||||
posixly_correct,
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
@@ -891,3 +891,23 @@ fn test_simd_respects_glibc_tunables() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_posixly_correct_whitespace() {
|
||||
let input = "word\u{00A0}word"; // Non-breaking space
|
||||
|
||||
// Default: Unicode whitespace is respected
|
||||
new_ucmd!()
|
||||
.arg("-w")
|
||||
.pipe_in(input)
|
||||
.succeeds()
|
||||
.stdout_is("2\n");
|
||||
|
||||
// POSIXLY_CORRECT: Only ASCII whitespace
|
||||
new_ucmd!()
|
||||
.arg("-w")
|
||||
.env("POSIXLY_CORRECT", "1")
|
||||
.pipe_in(input)
|
||||
.succeeds()
|
||||
.stdout_is("1\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user