-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
globset: add matches_all method #2900
Open
tmccombs
wants to merge
2
commits into
BurntSushi:master
Choose a base branch
from
tmccombs:matches-all
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.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -351,6 +351,27 @@ impl GlobSet { | |
false | ||
} | ||
|
||
/// Returns true if ALL globs in this set match the path given. | ||
pub fn matches_all<P: AsRef<Path>>(&self, path: P) -> bool { | ||
self.matches_all_candidate(&Candidate::new(path.as_ref())) | ||
} | ||
|
||
/// Returns ture if all globs in this set match the path given. | ||
/// | ||
/// This takes a Candidate as input, which can be used to amortize the | ||
/// cost of peparing a path for matching. | ||
/// | ||
/// This will return true if the set of globs is empty, as in that case all `0` of | ||
/// the globs will match. | ||
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. Please wrap lines to 79 columns (inclusive). 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. Also, this note should be added to |
||
pub fn matches_all_candidate(&self, path: &Candidate<'_>) -> bool { | ||
for strat in &self.strats { | ||
if !strat.is_match(path) { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
|
||
/// Returns the sequence number of every glob pattern that matches the | ||
/// given path. | ||
pub fn matches<P: AsRef<Path>>(&self, path: P) -> Vec<usize> { | ||
|
@@ -461,20 +482,33 @@ impl GlobSet { | |
required_exts.0.len(), | ||
regexes.literals.len() | ||
); | ||
Ok(GlobSet { | ||
len: pats.len(), | ||
strats: vec![ | ||
GlobSetMatchStrategy::Extension(exts), | ||
GlobSetMatchStrategy::BasenameLiteral(base_lits), | ||
GlobSetMatchStrategy::Literal(lits), | ||
GlobSetMatchStrategy::Suffix(suffixes.suffix()), | ||
GlobSetMatchStrategy::Prefix(prefixes.prefix()), | ||
GlobSetMatchStrategy::RequiredExtension( | ||
required_exts.build()?, | ||
), | ||
GlobSetMatchStrategy::Regex(regexes.regex_set()?), | ||
], | ||
}) | ||
let mut strats = Vec::with_capacity(7); | ||
// Only add strategies that are populated | ||
if !exts.0.is_empty() { | ||
strats.push(GlobSetMatchStrategy::Extension(exts)); | ||
} | ||
if !base_lits.0.is_empty() { | ||
strats.push(GlobSetMatchStrategy::BasenameLiteral(base_lits)); | ||
} | ||
if !lits.0.is_empty() { | ||
strats.push(GlobSetMatchStrategy::Literal(lits)); | ||
} | ||
if !suffixes.is_empty() { | ||
strats.push(GlobSetMatchStrategy::Suffix(suffixes.suffix())); | ||
} | ||
if !prefixes.is_empty() { | ||
strats.push(GlobSetMatchStrategy::Prefix(prefixes.prefix())); | ||
} | ||
if !required_exts.0.is_empty() { | ||
strats.push(GlobSetMatchStrategy::RequiredExtension( | ||
required_exts.build()?, | ||
)); | ||
} | ||
if !regexes.is_empty() { | ||
strats.push(GlobSetMatchStrategy::Regex(regexes.regex_set()?)); | ||
} | ||
|
||
Ok(GlobSet { len: pats.len(), strats }) | ||
} | ||
} | ||
|
||
|
@@ -892,6 +926,10 @@ impl MultiStrategyBuilder { | |
patset: Arc::new(Pool::new(create)), | ||
}) | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.literals.is_empty() | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
|
@@ -979,6 +1017,7 @@ mod tests { | |
let set = GlobSetBuilder::new().build().unwrap(); | ||
assert!(!set.is_match("")); | ||
assert!(!set.is_match("a")); | ||
assert!(set.matches_all("a")); | ||
} | ||
|
||
#[test] | ||
|
@@ -1019,4 +1058,17 @@ mod tests { | |
let matches = set.matches("nada"); | ||
assert_eq!(0, matches.len()); | ||
} | ||
|
||
#[test] | ||
fn matches_all_works() { | ||
let mut builder = GlobSetBuilder::new(); | ||
builder.add(Glob::new("src/*").unwrap()); | ||
builder.add(Glob::new("**/*.rs").unwrap()); | ||
let set = builder.build().unwrap(); | ||
|
||
println!("matches={:?}", set.matches("src/foo.rs")); | ||
assert!(set.matches_all("src/foo.rs")); | ||
assert!(!set.matches_all("src/bar.c")); | ||
assert!(!set.matches_all("test.rs")); | ||
} | ||
} |
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.
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.
I don't think we need to use caps for
ALL
here. Just "all" is fine.