From 1e9555494d574c46fac5fab8659daa5fbaf7fa40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=BF=20Fleur=20de=20Blue?= <135421389+Xylphy@users.noreply.github.com> Date: Tue, 17 Feb 2026 22:49:03 +0800 Subject: [PATCH] ptx: match GNU default behavior by skipping non-alphabetic index tokens (#10919) * ptx: skip non-alphabetic tokens in default word matching * Simplify no output operation Co-authored-by: Daniel Hofstetter * fix: Keyword must start at first alphabetic char * Simplify find first alphabet --------- Co-authored-by: Daniel Hofstetter --- src/uu/ptx/src/ptx.rs | 14 +++++++++++++- tests/by-util/test_ptx.rs | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index 9e5559829..1811a29bd 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -356,7 +356,19 @@ fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) -> }; // match words with given regex for mat in reg.find_iter(line) { - let (beg, end) = (mat.start(), mat.end()); + let (mut beg, end) = (mat.start(), mat.end()); + + // GNU-compatible default behavior: + // with default regexp, keyword must start at first alphabetic char. + if filter.word_regex == Config::default().context_regex { + let matched = &line[beg..end]; + if let Some(pos) = matched.find(|c: char| c.is_alphabetic()) { + beg += pos; + } else { + continue; + } + } + if config.input_ref && ((beg, end) == (ref_beg, ref_end)) { continue; } diff --git a/tests/by-util/test_ptx.rs b/tests/by-util/test_ptx.rs index 1db4a972b..4752d924e 100644 --- a/tests/by-util/test_ptx.rs +++ b/tests/by-util/test_ptx.rs @@ -300,6 +300,11 @@ fn test_gnu_mode_dumb_format() { new_ucmd!().pipe_in("a b").succeeds().stdout_only( " a b\n a b\n", ); + + new_ucmd!() + .pipe_in("2a") + .succeeds() + .stdout_only(format!("{}2 a\n", " ".repeat(35))); } #[test] @@ -330,6 +335,15 @@ fn test_unicode_padding_alignment() { .stdout_only(" a\n é\n"); } +#[test] +fn test_gnu_compat_numeric_token_with_emoji_produces_no_index() { + // GNU ptx produces no output for this input in default mode. + new_ucmd!() + .pipe_in("012345678901234567890123456789🛠\n") + .succeeds() + .no_output(); +} + #[test] fn test_unicode_truncation_alignment() { new_ucmd!()