Skip to content

grep: short-circuit inverted empty patterns#43

Open
wondr-wclabs wants to merge 1 commit into
uutils:mainfrom
wondr-wclabs:codex/empty-invert-short-circuit
Open

grep: short-circuit inverted empty patterns#43
wondr-wclabs wants to merge 1 commit into
uutils:mainfrom
wondr-wclabs:codex/empty-invert-short-circuit

Conversation

@wondr-wclabs
Copy link
Copy Markdown
Contributor

Closes #27.

Summary

  • detect the -v + empty-pattern case after regex compilation, before stdin/file traversal
  • return exit status 1 without count output or file-open errors when no line can be selected
  • add regression coverage for count mode, missing input, and invalid-regex ordering

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 5, 2026

Merging this PR will not alter performance

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing wondr-wclabs:codex/empty-invert-short-circuit (360f8a5) with main (d28bf76)

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.

Comment thread src/lib.rs Outdated
};

let matcher = Matcher::compile(&config)?;
if has_empty_inverted_pattern(&patterns, invert_match) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment explaining why

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment at the branch explaining the behavior: an empty pattern matches every line, so with -v GNU selects no lines and returns exit 1 without scanning inputs.

I kept that check before creating the searcher so missing files are not touched in this case; the direct regression check is grep -v "" missing-file, which now exits 1 with no stdout/stderr.

Comment thread src/lib.rs Outdated
searcher.finish()
}

fn has_empty_inverted_pattern(patterns: &[&str], invert_match: bool) -> bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need a function here ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it was not pulling its weight. I removed the helper and inlined the condition next to the early return.

That keeps the special-case logic visible with the explanatory comment, and avoids a one-use function whose name still required jumping back to understand why the early exit is GNU-compatible.

Copy link
Copy Markdown
Contributor

@sylvestre sylvestre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the comments

@wondr-wclabs wondr-wclabs force-pushed the codex/empty-invert-short-circuit branch from e7842d1 to 360f8a5 Compare June 5, 2026 06:42
Copy link
Copy Markdown
Collaborator

@lhecker lhecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting! Thanks!

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.

does not short-circuit an empty pattern with -v like GNU (same family as #4)

3 participants