mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-08-04 21:52:54 +02:00
cli: make rg -vf file
behave sensibly
Previously, when `file` is empty (literally empty, as in, zero byte), `rg -f file` and `rg -vf file` would behave identically. This is odd and also doesn't match how GNU grep behaves. It's also not logically correct. An empty file means _zero_ patterns which is an empty set. An empty set matches nothing. Inverting the empty set should result in matching everything. This was because of an errant optimization that lets ripgrep quit early if it can statically detect that no matches are possible. Moreover, there was *also* a bug in how we constructed the PCRE2 pattern when there are zero patterns. PCRE2 doesn't have a concept of sets of patterns (unlike the `regex` crate), so we need to fake it with an empty character class. Fixes #1332, Fixes #3001, Closes #3041
This commit is contained in:
@ -517,7 +517,7 @@ impl HiArgs {
|
||||
/// When this returns false, it is impossible for ripgrep to ever report
|
||||
/// a match.
|
||||
pub(crate) fn matches_possible(&self) -> bool {
|
||||
if self.patterns.patterns.is_empty() {
|
||||
if self.patterns.patterns.is_empty() && !self.invert_match {
|
||||
return false;
|
||||
}
|
||||
if self.max_count == Some(0) {
|
||||
|
@ -55,7 +55,12 @@ impl RegexMatcherBuilder {
|
||||
format!("(?:{})", p.as_ref())
|
||||
});
|
||||
}
|
||||
let mut singlepat = pats.join("|");
|
||||
let mut singlepat = if patterns.is_empty() {
|
||||
// A way to spell a pattern that can never match anything.
|
||||
r"[^\S\s]".to_string()
|
||||
} else {
|
||||
pats.join("|")
|
||||
};
|
||||
if self.case_smart && !has_uppercase_literal(&singlepat) {
|
||||
builder.caseless(true);
|
||||
}
|
||||
|
Reference in New Issue
Block a user