grep: short-circuit inverted empty patterns#43
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
| }; | ||
|
|
||
| let matcher = Matcher::compile(&config)?; | ||
| if has_empty_inverted_pattern(&patterns, invert_match) { |
There was a problem hiding this comment.
please add a comment explaining why
There was a problem hiding this comment.
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.
| searcher.finish() | ||
| } | ||
|
|
||
| fn has_empty_inverted_pattern(patterns: &[&str], invert_match: bool) -> bool { |
There was a problem hiding this comment.
do we really need a function here ?
There was a problem hiding this comment.
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.
e7842d1 to
360f8a5
Compare
lhecker
left a comment
There was a problem hiding this comment.
Oh interesting! Thanks!
Closes #27.
Summary
-v+ empty-pattern case after regex compilation, before stdin/file traversal