Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Result<Paths, PatternE
original: "".to_string(),
tokens: Vec::new(),
is_recursive: false,
has_metachars: false,
});
}

Expand Down Expand Up @@ -551,6 +552,9 @@ pub struct Pattern {
original: String,
tokens: Vec<PatternToken>,
is_recursive: bool,
/// A bool value that indicates whether the pattern contains any metacharacters.
/// We use this information for some fast path optimizations.
has_metachars: bool,
}

/// Show the original glob pattern.
Expand Down Expand Up @@ -604,15 +608,19 @@ impl Pattern {
let chars = pattern.chars().collect::<Vec<_>>();
let mut tokens = Vec::new();
let mut is_recursive = false;
let mut has_metachars = false;
let mut i = 0;

while i < chars.len() {
match chars[i] {
'?' => {
has_metachars = true;
tokens.push(AnyChar);
i += 1;
}
'*' => {
has_metachars = true;

let old = i;

while i < chars.len() && chars[i] == '*' {
Expand Down Expand Up @@ -674,6 +682,8 @@ impl Pattern {
}
}
'[' => {
has_metachars = true;

if i + 4 <= chars.len() && chars[i + 1] == '!' {
match chars[i + 3..].iter().position(|x| *x == ']') {
None => (),
Expand Down Expand Up @@ -714,6 +724,7 @@ impl Pattern {
tokens,
original: pattern.to_string(),
is_recursive,
has_metachars,
})
}

Expand Down Expand Up @@ -877,19 +888,6 @@ fn fill_todo(
path: &PathWrapper,
options: MatchOptions,
) {
// convert a pattern that's just many Char(_) to a string
fn pattern_as_str(pattern: &Pattern) -> Option<String> {
let mut s = String::new();
for token in &pattern.tokens {
match *token {
Char(c) => s.push(c),
_ => return None,
}
}

Some(s)
}

let add = |todo: &mut Vec<_>, next_path: PathWrapper| {
if idx + 1 == patterns.len() {
// We know it's good, so don't make the iterator match this path
Expand All @@ -904,8 +902,17 @@ fn fill_todo(
let pattern = &patterns[idx];
let is_dir = path.is_directory;
let curdir = path.as_ref() == Path::new(".");
match pattern_as_str(pattern) {
Some(s) => {
match (pattern.has_metachars, is_dir) {
(false, _) => {
debug_assert!(
pattern
.tokens
.iter()
.all(|tok| matches!(tok, PatternToken::Char(_))),
"broken invariant: pattern has metachars but shouldn't"
);
let s = pattern.as_str();

// This pattern component doesn't have any metacharacters, so we
// don't need to read the current directory to know where to
// continue. So instead of passing control back to the iterator,
Expand All @@ -915,7 +922,7 @@ fn fill_todo(
let next_path = if curdir {
PathBuf::from(s)
} else {
path.join(&s)
path.join(s)
};
let next_path = PathWrapper::from_path(next_path);
if (special && is_dir)
Expand All @@ -926,7 +933,7 @@ fn fill_todo(
add(todo, next_path);
}
}
None if is_dir => {
(true, true) => {
let dirs = fs::read_dir(path).and_then(|d| {
d.map(|e| {
e.map(|e| {
Expand Down Expand Up @@ -970,7 +977,7 @@ fn fill_todo(
}
}
}
None => {
(true, false) => {
// not a directory, nothing more to find
}
}
Expand Down