-
Notifications
You must be signed in to change notification settings - Fork 103
feat(relay-pattern): Add negation pattern matching #5116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -368,6 +368,8 @@ enum MatchStrategy { | |
Static(bool), | ||
/// The pattern is complex and needs to be evaluated using [`wildmatch`]. | ||
Wildmatch(Tokens), | ||
/// The pattern is complex and needs to be evaluated using [`wildmatch`]. | ||
NegatedWildmatch(Tokens), | ||
Comment on lines
+371
to
+372
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't seem correct, a You can instead track this property on the |
||
// Possible future optimizations for `Any` variations: | ||
// Examples: `??`. `??suffix`, `prefix??` and `?contains?`. | ||
} | ||
|
@@ -384,6 +386,7 @@ impl MatchStrategy { | |
[Token::Wildcard, Token::Literal(literal), Token::Wildcard] => { | ||
Self::Contains(std::mem::take(literal)) | ||
} | ||
[Token::Negated, ..] => Self::NegatedWildmatch(tokens), | ||
_ => Self::Wildmatch(tokens), | ||
}; | ||
|
||
|
@@ -399,6 +402,9 @@ impl MatchStrategy { | |
MatchStrategy::Contains(contains) => match_contains(contains, haystack, options), | ||
MatchStrategy::Static(matches) => *matches, | ||
MatchStrategy::Wildmatch(tokens) => wildmatch::is_match(haystack, tokens, options), | ||
MatchStrategy::NegatedWildmatch(tokens) => { | ||
!wildmatch::is_match(haystack, tokens, options) | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -500,6 +506,10 @@ impl<'a> Parser<'a> { | |
} | ||
|
||
fn parse(&mut self) -> Result<(), ErrorKind> { | ||
if self.advance_if(|c| c == '!') { | ||
self.push_token(Token::Negated); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Negated Token Incorrectly Affects WildmatchThe Additional Locations (2)
Comment on lines
+509
to
+511
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make this behavior configurable via We can then just use a |
||
|
||
while let Some(c) = self.advance() { | ||
match c { | ||
'?' => self.push_token(Token::Any(NonZeroUsize::MIN)), | ||
|
@@ -671,6 +681,7 @@ impl<'a> Parser<'a> { | |
/// - A [`Token::Any`] is never followed by [`Token::Any`]. | ||
/// - A [`Token::Literal`] is never followed by [`Token::Literal`]. | ||
/// - A [`Token::Class`] is never empty. | ||
/// - A [`Token::Negated`] is always the first character in the string. | ||
#[derive(Clone, Debug, Default)] | ||
struct Tokens(Vec<Token>); | ||
|
||
|
@@ -761,6 +772,8 @@ enum Token { | |
Any(NonZeroUsize), | ||
/// The wildcard token `*`. | ||
Wildcard, | ||
/// The token `!`. | ||
Negated, | ||
/// A class token `[abc]` or its negated variant `[!abc]`. | ||
Class { negated: bool, ranges: Ranges }, | ||
/// A list of nested alternate tokens `{a,b}`. | ||
|
@@ -960,6 +973,7 @@ mod tests { | |
MatchStrategy::Contains(_) => "Contains", | ||
MatchStrategy::Static(_) => "Static", | ||
MatchStrategy::Wildmatch(_) => "Wildmatch", | ||
MatchStrategy::NegatedWildmatch(_) => "NegatedWildmatch", | ||
}; | ||
assert_eq!( | ||
kind, | ||
|
@@ -1585,7 +1599,7 @@ mod tests { | |
assert_pattern!("1.18.[!0-4].*", "1.18.5."); | ||
assert_pattern!("1.18.[!0-4].*", "1.18.5.aBc"); | ||
assert_pattern!("1.18.[!0-4].*", NOT "1.18.3.abc"); | ||
assert_pattern!("!*!*.md", "!foo!.md"); // no `!` outside of character classes | ||
assert_pattern!("*!*.md", "foo!.md"); // no `!` outside of character classes | ||
assert_pattern!("foo*foofoo*foobar", "foofoofooxfoofoobar"); | ||
assert_pattern!("foo*fooFOO*fOobar", "fooFoofooXfoofooBAR", i); | ||
assert_pattern!("[0-9]*a", "0aaaaaaaaa", i); | ||
|
@@ -1936,4 +1950,19 @@ mod tests { | |
assert!(!patterns.is_match("foo")); | ||
assert!(patterns.is_match("bar")); | ||
} | ||
|
||
#[test] | ||
fn test_pattern_negation() { | ||
let patterns = Patterns::builder().add("!foo@*").unwrap().take(); | ||
|
||
assert!(patterns.is_match("[email protected]")); | ||
assert!(patterns.is_match("[email protected]")); | ||
assert!(patterns.is_match("foo")); | ||
assert!(patterns.is_match("barfoo@")); | ||
|
||
// foo@ is never matched. | ||
assert!(!patterns.is_match("[email protected]")); | ||
assert!(!patterns.is_match("[email protected]")); | ||
assert!(!patterns.is_match("foo@anything")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong section now.