Skip to content

grep: reject multiple patterns with -P - #50

Closed
wondr-wclabs wants to merge 1 commit into
uutils:mainfrom
wondr-wclabs:codex/pcre-single-pattern
Closed

grep: reject multiple patterns with -P#50
wondr-wclabs wants to merge 1 commit into
uutils:mainfrom
wondr-wclabs:codex/pcre-single-pattern

Conversation

@wondr-wclabs

Copy link
Copy Markdown
Contributor

Fixes #34.

This brings -P closer to GNU grep for the specific PCRE restriction described in the issue: PCRE mode accepts exactly one logical pattern. Other matcher modes still keep the existing multi-pattern behavior.

The validation is placed after grep has already normalized all pattern sources into patterns because that is the point where the rule is easiest to express correctly. That means the same check covers:

  • repeated -e, e.g. -P -e a -e b
  • a single -e containing a newline, e.g. -P -e 'a\nb'
  • a positional pattern containing a newline
  • -f files with more than one pattern line

I kept this out of Clap argument validation because -e, -f, and positional patterns all flow through different input paths and only become equivalent after the existing pattern-splitting logic. Checking the normalized pattern count avoids duplicating that logic and keeps the restriction specific to RegexMode::Perl.

Validation:

  • printf 'abc\n' | cargo run --quiet -- -P -e a -e b now exits 2 with grep: the -P option only supports a single pattern
  • cargo test pcre_rejects_multiple_patterns -- --nocapture
  • cargo test
  • cargo test --no-fail-fast
  • cargo fmt --all -- --check
  • cargo clippy --all-targets --workspace -p uu_grep -- -D warnings
  • git diff --check

@codspeed-hq

codspeed-hq Bot commented Jun 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing wondr-wclabs:codex/pcre-single-pattern (7449b75) with main (f4798cb)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@sylvestre

Copy link
Copy Markdown
Contributor

already merged here #40

@sylvestre sylvestre closed this Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-P accepts multiple patterns where GNU allows only one

2 participants