Optimize regex search further

No need to compile both regex and iregex.
pull/484/head
Arijit Basu 2 years ago committed by Arijit Basu
parent f324e976da
commit e655c245c5

@ -1289,13 +1289,7 @@ pub enum NodeFilter {
} }
impl NodeFilter { impl NodeFilter {
fn apply( fn apply(&self, node: &Node, input: &str, regex: Option<&Regex>) -> bool {
&self,
node: &Node,
input: &str,
regex: Option<&Regex>,
iregex: Option<&Regex>,
) -> bool {
match self { match self {
Self::RelativePathIs => node.relative_path.eq(input), Self::RelativePathIs => node.relative_path.eq(input),
Self::IRelativePathIs => { Self::IRelativePathIs => {
@ -1356,14 +1350,14 @@ impl NodeFilter {
Self::RelativePathDoesMatchRegex => regex Self::RelativePathDoesMatchRegex => regex
.map(|r| r.is_match(&node.relative_path)) .map(|r| r.is_match(&node.relative_path))
.unwrap_or(false), .unwrap_or(false),
Self::IRelativePathDoesMatchRegex => iregex Self::IRelativePathDoesMatchRegex => regex
.map(|r| r.is_match(&node.relative_path.to_lowercase())) .map(|r| r.is_match(&node.relative_path.to_lowercase()))
.unwrap_or(false), .unwrap_or(false),
Self::RelativePathDoesNotMatchRegex => !regex Self::RelativePathDoesNotMatchRegex => !regex
.map(|r| r.is_match(&node.relative_path)) .map(|r| r.is_match(&node.relative_path))
.unwrap_or(false), .unwrap_or(false),
Self::IRelativePathDoesNotMatchRegex => !iregex Self::IRelativePathDoesNotMatchRegex => !regex
.map(|r| r.is_match(&node.relative_path.to_lowercase())) .map(|r| r.is_match(&node.relative_path.to_lowercase()))
.unwrap_or(false), .unwrap_or(false),
@ -1426,14 +1420,14 @@ impl NodeFilter {
Self::AbsolutePathDoesMatchRegex => regex Self::AbsolutePathDoesMatchRegex => regex
.map(|r| r.is_match(&node.absolute_path)) .map(|r| r.is_match(&node.absolute_path))
.unwrap_or(false), .unwrap_or(false),
Self::IAbsolutePathDoesMatchRegex => iregex Self::IAbsolutePathDoesMatchRegex => regex
.map(|r| r.is_match(&node.absolute_path.to_lowercase())) .map(|r| r.is_match(&node.absolute_path.to_lowercase()))
.unwrap_or(false), .unwrap_or(false),
Self::AbsolutePathDoesNotMatchRegex => !regex Self::AbsolutePathDoesNotMatchRegex => !regex
.map(|r| r.is_match(&node.absolute_path)) .map(|r| r.is_match(&node.absolute_path))
.unwrap_or(false), .unwrap_or(false),
Self::IAbsolutePathDoesNotMatchRegex => !iregex Self::IAbsolutePathDoesNotMatchRegex => !regex
.map(|r| r.is_match(&node.absolute_path.to_lowercase())) .map(|r| r.is_match(&node.absolute_path.to_lowercase()))
.unwrap_or(false), .unwrap_or(false),
} }
@ -1467,49 +1461,42 @@ pub struct NodeFilterApplicable {
#[serde(skip)] #[serde(skip)]
pub regex: Option<CmpRegex>, pub regex: Option<CmpRegex>,
#[serde(skip)]
pub iregex: Option<CmpRegex>,
} }
impl NodeFilterApplicable { impl NodeFilterApplicable {
pub fn new(filter: NodeFilter, input: String) -> Self { pub fn new(filter: NodeFilter, input: String) -> Self {
use NodeFilter::*; use NodeFilter::*;
let (regex, iregex) = if matches!( let regex = if matches!(
filter, filter,
RelativePathDoesMatchRegex RelativePathDoesMatchRegex
| IRelativePathDoesMatchRegex
| RelativePathDoesNotMatchRegex | RelativePathDoesNotMatchRegex
| IRelativePathDoesNotMatchRegex
| AbsolutePathDoesMatchRegex | AbsolutePathDoesMatchRegex
| IAbsolutePathDoesMatchRegex
| AbsolutePathDoesNotMatchRegex | AbsolutePathDoesNotMatchRegex
) {
Regex::new(&input).ok().map(CmpRegex)
} else if matches!(
filter,
IRelativePathDoesMatchRegex
| IRelativePathDoesNotMatchRegex
| IAbsolutePathDoesMatchRegex
| IAbsolutePathDoesNotMatchRegex | IAbsolutePathDoesNotMatchRegex
) { ) {
( Regex::new(&input.to_lowercase()).ok().map(CmpRegex)
Regex::new(&input).ok().map(CmpRegex),
Regex::new(&input.to_lowercase()).ok().map(CmpRegex),
)
} else { } else {
(None, None) None
}; };
Self { Self {
filter, filter,
input, input,
regex, regex,
iregex,
} }
} }
fn apply(&self, node: &Node) -> bool { fn apply(&self, node: &Node) -> bool {
self.filter.apply( self.filter
node, .apply(node, &self.input, self.regex.as_ref().map(|r| &r.0))
&self.input,
self.regex.as_ref().map(|r| &r.0),
self.iregex.as_ref().map(|r| &r.0),
)
} }
} }

Loading…
Cancel
Save