Skip to content

Commit 23cf875

Browse files
committed
Fix code style
1 parent 257f279 commit 23cf875

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Result<Paths, PatternE
258258
original: "".to_string(),
259259
tokens: Vec::new(),
260260
is_recursive: false,
261-
is_regular: true,
261+
has_metachars: false,
262262
});
263263
}
264264

@@ -552,7 +552,9 @@ pub struct Pattern {
552552
original: String,
553553
tokens: Vec<PatternToken>,
554554
is_recursive: bool,
555-
is_regular: bool,
555+
/// A bool value that indicates whether the pattern contains any metacharacters.
556+
/// We use this information for some fast path optimizations.
557+
has_metachars: bool,
556558
}
557559

558560
/// Show the original glob pattern.
@@ -606,18 +608,18 @@ impl Pattern {
606608
let chars = pattern.chars().collect::<Vec<_>>();
607609
let mut tokens = Vec::new();
608610
let mut is_recursive = false;
609-
let mut is_regular = true;
611+
let mut has_metachars = false;
610612
let mut i = 0;
611613

612614
while i < chars.len() {
613615
match chars[i] {
614616
'?' => {
615-
is_regular = false;
617+
has_metachars = true;
616618
tokens.push(AnyChar);
617619
i += 1;
618620
}
619621
'*' => {
620-
is_regular = false;
622+
has_metachars = true;
621623

622624
let old = i;
623625

@@ -680,7 +682,7 @@ impl Pattern {
680682
}
681683
}
682684
'[' => {
683-
is_regular = false;
685+
has_metachars = true;
684686

685687
if i + 4 <= chars.len() && chars[i + 1] == '!' {
686688
match chars[i + 3..].iter().position(|x| *x == ']') {
@@ -722,7 +724,7 @@ impl Pattern {
722724
tokens,
723725
original: pattern.to_string(),
724726
is_recursive,
725-
is_regular,
727+
has_metachars,
726728
})
727729
}
728730

@@ -900,8 +902,15 @@ fn fill_todo(
900902
let pattern = &patterns[idx];
901903
let is_dir = path.is_directory;
902904
let curdir = path.as_ref() == Path::new(".");
903-
match (pattern.is_regular, is_dir) {
904-
(true, _) => {
905+
match (pattern.has_metachars, is_dir) {
906+
(false, _) => {
907+
debug_assert!(
908+
pattern
909+
.tokens
910+
.iter()
911+
.all(|tok| matches!(tok, PatternToken::Char(_))),
912+
"broken invariant: pattern has metachars but shouldn't"
913+
);
905914
let s = pattern.as_str();
906915

907916
// This pattern component doesn't have any metacharacters, so we
@@ -924,7 +933,7 @@ fn fill_todo(
924933
add(todo, next_path);
925934
}
926935
}
927-
(false, true) => {
936+
(true, true) => {
928937
let dirs = fs::read_dir(path).and_then(|d| {
929938
d.map(|e| {
930939
e.map(|e| {
@@ -968,7 +977,7 @@ fn fill_todo(
968977
}
969978
}
970979
}
971-
_ => {
980+
(true, false) => {
972981
// not a directory, nothing more to find
973982
}
974983
}

0 commit comments

Comments
 (0)