1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
mod fuzzy;
mod re_based;
#[cfg(test)]
mod tests;
use std::iter;
use std::path;
use regex;
pub struct Matcher<'a> {
fuzzy_matcher: fuzzy::FuzzyMatcher<'a>,
re_anywhere: regex::Regex,
re_consecutive: regex::Regex,
}
fn detect_smartcase(needles: &[&str]) -> bool {
for s in needles {
for ch in s.chars() {
if ch.is_uppercase() {
return false;
}
}
}
true
}
impl<'a> Matcher<'a> {
pub fn new_smartcase(needles: Vec<&'a str>) -> Matcher<'a> {
let ignore_case = detect_smartcase(&needles);
Matcher::new(needles, ignore_case)
}
pub fn new(needles: Vec<&'a str>, ignore_case: bool) -> Matcher<'a> {
let fuzzy_matcher = fuzzy::FuzzyMatcher::defaults(needles[needles.len() - 1]);
let re_anywhere =
re_based::prepare_regex(&needles, re_based::re_match_anywhere, ignore_case);
let re_consecutive =
re_based::prepare_regex(&needles, re_based::re_match_consecutive, ignore_case);
Matcher {
fuzzy_matcher: fuzzy_matcher,
re_anywhere: re_anywhere,
re_consecutive: re_consecutive,
}
}
pub fn execute<'p, P>(&'a self, haystack: &'p [P]) -> impl iter::Iterator<Item = &'p P> + 'a
where P: AsRef<path::Path>,
'p: 'a
{
macro_rules! filter_path_with_re {
($l: expr, $re: expr) => {
$l
.iter()
.filter(move |&p| $re.is_match(p.as_ref().to_string_lossy().to_mut()))
};
}
filter_path_with_re!(haystack, self.re_consecutive)
.chain(self.fuzzy_matcher.filter_path(haystack))
.chain(filter_path_with_re!(haystack, self.re_anywhere))
}
}