Skip to content

Commit 1d66de7

Browse files
committed
Optimize memory allocations
1 parent 97e5ee9 commit 1d66de7

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/lib.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +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,
261262
});
262263
}
263264

@@ -551,6 +552,7 @@ pub struct Pattern {
551552
original: String,
552553
tokens: Vec<PatternToken>,
553554
is_recursive: bool,
555+
is_regular: bool,
554556
}
555557

556558
/// Show the original glob pattern.
@@ -604,15 +606,19 @@ impl Pattern {
604606
let chars = pattern.chars().collect::<Vec<_>>();
605607
let mut tokens = Vec::new();
606608
let mut is_recursive = false;
609+
let mut is_regular = true;
607610
let mut i = 0;
608611

609612
while i < chars.len() {
610613
match chars[i] {
611614
'?' => {
615+
is_regular = false;
612616
tokens.push(AnyChar);
613617
i += 1;
614618
}
615619
'*' => {
620+
is_regular = false;
621+
616622
let old = i;
617623

618624
while i < chars.len() && chars[i] == '*' {
@@ -674,6 +680,8 @@ impl Pattern {
674680
}
675681
}
676682
'[' => {
683+
is_regular = false;
684+
677685
if i + 4 <= chars.len() && chars[i + 1] == '!' {
678686
match chars[i + 3..].iter().position(|x| *x == ']') {
679687
None => (),
@@ -714,6 +722,7 @@ impl Pattern {
714722
tokens,
715723
original: pattern.to_string(),
716724
is_recursive,
725+
is_regular,
717726
})
718727
}
719728

@@ -877,19 +886,6 @@ fn fill_todo(
877886
path: &PathWrapper,
878887
options: MatchOptions,
879888
) {
880-
// convert a pattern that's just many Char(_) to a string
881-
fn pattern_as_str(pattern: &Pattern) -> Option<String> {
882-
let mut s = String::new();
883-
for token in &pattern.tokens {
884-
match *token {
885-
Char(c) => s.push(c),
886-
_ => return None,
887-
}
888-
}
889-
890-
Some(s)
891-
}
892-
893889
let add = |todo: &mut Vec<_>, next_path: PathWrapper| {
894890
if idx + 1 == patterns.len() {
895891
// We know it's good, so don't make the iterator match this path
@@ -904,8 +900,10 @@ fn fill_todo(
904900
let pattern = &patterns[idx];
905901
let is_dir = path.is_directory;
906902
let curdir = path.as_ref() == Path::new(".");
907-
match pattern_as_str(pattern) {
908-
Some(s) => {
903+
match (pattern.is_regular, is_dir) {
904+
(true, _) => {
905+
let s = pattern.as_str();
906+
909907
// This pattern component doesn't have any metacharacters, so we
910908
// don't need to read the current directory to know where to
911909
// continue. So instead of passing control back to the iterator,
@@ -926,7 +924,7 @@ fn fill_todo(
926924
add(todo, next_path);
927925
}
928926
}
929-
None if is_dir => {
927+
(false, true) => {
930928
let dirs = fs::read_dir(path).and_then(|d| {
931929
d.map(|e| {
932930
e.map(|e| {
@@ -970,7 +968,7 @@ fn fill_todo(
970968
}
971969
}
972970
}
973-
None => {
971+
_ => {
974972
// not a directory, nothing more to find
975973
}
976974
}

0 commit comments

Comments
 (0)