-
Notifications
You must be signed in to change notification settings - Fork 104
feat(releases): Add negated prefix to glob matching #5040
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
Draft
cmanallen
wants to merge
5
commits into
master
Choose a base branch
from
cmanallen/releases-inverted-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−0
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f8c9b3
Add group expression
cmanallen 4ab0be0
Add inverted handling and coverage
cmanallen 42d8520
Update changelog
cmanallen 96635ee
Use appropriate error class
cmanallen 124fdb7
Merge branch 'master' into cmanallen/releases-inverted-filter
cmanallen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| //! * `[!a-z]` matches one character that is not in the given range. | ||
| //! * `{a,b}` matches any pattern within the alternation group. | ||
| //! * `\` escapes any of the above special characters and treats it as a literal. | ||
| //! * `(a)` matches any pattern within the group. | ||
| //! * `(!a)` matches the inverted pattern within the group. | ||
| //! | ||
| //! # Complexity | ||
| //! | ||
|
|
@@ -59,6 +61,8 @@ enum ErrorKind { | |
| InvalidRange(char, char), | ||
| /// Unbalanced character class. The pattern contains unbalanced `[`, `]` characters. | ||
| UnbalancedCharacterClass, | ||
| /// Unbalanced group. The pattern contains unbalanced `(`, `)` characters. | ||
| UnbalancedGroup, | ||
| /// Character class is invalid and cannot be parsed. | ||
| InvalidCharacterClass, | ||
| /// Nested alternates are not valid. | ||
|
|
@@ -84,6 +88,7 @@ impl fmt::Display for Error { | |
| write!(f, "Invalid character range `{start}-{end}`") | ||
| } | ||
| ErrorKind::UnbalancedCharacterClass => write!(f, "Unbalanced character class"), | ||
| ErrorKind::UnbalancedGroup => write!(f, "Unbalanced group"), | ||
| ErrorKind::InvalidCharacterClass => write!(f, "Invalid character class"), | ||
| ErrorKind::NestedAlternates => write!(f, "Nested alternates"), | ||
| ErrorKind::UnbalancedAlternates => write!(f, "Unbalanced alternates"), | ||
|
|
@@ -504,6 +509,8 @@ impl<'a> Parser<'a> { | |
| match c { | ||
| '?' => self.push_token(Token::Any(NonZeroUsize::MIN)), | ||
| '*' => self.push_token(Token::Wildcard), | ||
| '(' => self.parse_group()?, | ||
| ')' => return Err(ErrorKind::UnbalancedGroup), | ||
| '[' => self.parse_class()?, | ||
| ']' => return Err(ErrorKind::UnbalancedCharacterClass), | ||
| '{' => self.start_alternates()?, | ||
|
|
@@ -555,6 +562,33 @@ impl<'a> Parser<'a> { | |
| Ok(()) | ||
| } | ||
|
|
||
| fn parse_group(&mut self) -> Result<(), ErrorKind> { | ||
| let negated = self.advance_if(|c| c == '!'); | ||
| let mut literal = String::new(); | ||
|
|
||
| loop { | ||
| let Some(c) = self.advance() else { | ||
| return Err(ErrorKind::UnbalancedGroup); | ||
| }; | ||
|
|
||
| match c { | ||
| '(' => return Err(ErrorKind::InvalidCharacterClass), | ||
| ')' => break, | ||
| c => literal.push(match c { | ||
| '\\' => self.advance().ok_or(ErrorKind::DanglingEscape)?, | ||
| c => c, | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| self.push_token(Token::Group { | ||
| negated, | ||
| literal: Literal::new(literal, self.options), | ||
| }); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn parse_class(&mut self) -> Result<(), ErrorKind> { | ||
| let negated = self.advance_if(|c| c == '!'); | ||
|
|
||
|
|
@@ -763,6 +797,8 @@ enum Token { | |
| Wildcard, | ||
| /// A class token `[abc]` or its negated variant `[!abc]`. | ||
| Class { negated: bool, ranges: Ranges }, | ||
| /// A group token `(abc)` or its negated variant `(!abc)`. | ||
| Group { negated: bool, literal: Literal }, | ||
| /// A list of nested alternate tokens `{a,b}`. | ||
| Alternates(Vec<Tokens>), | ||
| /// A list of optional tokens. | ||
|
|
@@ -1936,4 +1972,18 @@ mod tests { | |
| assert!(!patterns.is_match("foo")); | ||
| assert!(patterns.is_match("bar")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_patterns_inverted() { | ||
| // We want to match anything that is not prefixed with foo@ or bar@ | ||
| let mut builder = Patterns::builder().add("(!foo@)(!bar@)*").unwrap(); | ||
|
|
||
| let patterns = builder.take(); | ||
| assert!(!patterns.is_match("[email protected]")); | ||
| assert!(!patterns.is_match("[email protected]")); | ||
| assert!(patterns.is_match("[email protected]")); | ||
| assert!(patterns.is_match("[email protected]")); | ||
| assert!(patterns.is_match("[email protected]")); | ||
| assert!(patterns.is_match("[email protected]")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.