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 <daniel.hofstetter@42dh.com>

* fix: Keyword must start at first alphabetic char

* Simplify find first alphabet

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
This commit is contained in:
✿ Fleur de Blue
2026-02-17 22:49:03 +08:00
committed by GitHub
parent b4395348c8
commit 1e9555494d
2 changed files with 27 additions and 1 deletions
+13 -1
View File
@@ -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;
}
+14
View File
@@ -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!()