From e1626b8c64202d70f8269aafce885a952007671f Mon Sep 17 00:00:00 2001 From: Michael Kefeder Date: Fri, 12 Mar 2021 21:52:37 +0100 Subject: [PATCH] ptx: ignore empty regex (#1808) * ptx: ignore empty -W regex * ptx: test to verify empty -W regex is ignored * tests/ptx: verify word regexp from cmd-line is used --- src/uu/ptx/src/ptx.rs | 19 ++++++++++++++----- tests/by-util/test_ptx.rs | 16 ++++++++++++++++ ...roff_no_ref_word_regexp_exc_space.expected | 7 +++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index 8421e52bc..bfcd6699f 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -95,12 +95,21 @@ impl WordFilter { if matches.opt_present("b") { crash!(1, "-b not implemented yet"); } - let reg = if matches.opt_present("W") { - matches.opt_str("W").expect("parsing options failed!") - } else if config.gnu_ext { - "\\w+".to_owned() + // Ignore empty string regex from cmd-line-args + let arg_reg: Option = if matches.opt_present("W") { + matches.opt_str("W").filter(|reg| !reg.is_empty()) } else { - "[^ \t\n]+".to_owned() + None + }; + let reg = match arg_reg { + Some(arg_reg) => arg_reg, + None => { + if config.gnu_ext { + "\\w+".to_owned() + } else { + "[^ \t\n]+".to_owned() + } + } }; WordFilter { only_specified: o, diff --git a/tests/by-util/test_ptx.rs b/tests/by-util/test_ptx.rs index 77117c5c0..e44943bfa 100644 --- a/tests/by-util/test_ptx.rs +++ b/tests/by-util/test_ptx.rs @@ -8,6 +8,22 @@ fn gnu_ext_disabled_roff_no_ref() { .stdout_only_fixture("gnu_ext_disabled_roff_no_ref.expected"); } +#[test] +fn gnu_ext_disabled_roff_no_ref_empty_word_regexp() { + new_ucmd!() + .args(&["-G", "-R", "-W", "", "input"]) + .succeeds() + .stdout_only_fixture("gnu_ext_disabled_roff_no_ref.expected"); +} + +#[test] +fn gnu_ext_disabled_roff_no_ref_word_regexp_exc_space() { + new_ucmd!() + .args(&["-G", "-R", "-W", "[^\t\n]+", "input"]) + .succeeds() + .stdout_only_fixture("gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected"); +} + #[test] fn gnu_ext_disabled_roff_input_ref() { new_ucmd!() diff --git a/tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected b/tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected new file mode 100644 index 000000000..141e6c9f8 --- /dev/null +++ b/tests/fixtures/ptx/gnu_ext_disabled_roff_no_ref_word_regexp_exc_space.expected @@ -0,0 +1,7 @@ +.xx "" "" """quotes"", for roff" "" +.xx "" "" "and some other like %a, b#, c$c" "" +.xx "" "" "hello world!" "" +.xx "" "" "let's check special characters:" "" +.xx "" "" "maybe also~or^" "" +.xx "" "" "oh, and back\slash" "" +.xx "" "" "{brackets} for tex" ""