From 258de49a11242e24c7f6ab338639285e04059df7 Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux Date: Sun, 19 Jan 2020 10:19:20 -0500 Subject: [PATCH] fix(regex-match): Avoid recompiling the RegexSet and math full string Avoid recompiling the regexset for each value, it is wasteful. Add start and end anchor to match the full value with the regex by default TODO: Does the regex crate have an option for this? --- src/lib/shell/flow.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lib/shell/flow.rs b/src/lib/shell/flow.rs index 71247a61..874e86a5 100644 --- a/src/lib/shell/flow.rs +++ b/src/lib/shell/flow.rs @@ -503,13 +503,16 @@ impl<'a> Shell<'a> { let is_array = is_array(expression.as_ref()); let value = self.expand_string(expression.as_ref())?; for case in cases.iter() { - if value.iter().all(|value| { - case.value - .as_ref() - .and_then(|v| self.expand_string(v).ok()) - .and_then(|v| RegexSet::new(v).ok()) - .map_or(false, |regex| regex.is_match(value)) - }) { + let is_match = if let Some(v) = &case.value { + let v = self.expand_string(v)?; + // Anchor to start and end + let v = v.into_iter().map(|v| format!("^{}$", v)); + RegexSet::new(v).ok().map_or(false, |regex| value.iter().all(|v| regex.is_match(&v))) + } else { + true + }; + + if is_match { // let pattern_is_array = is_array(&value); let previous_bind = case.binding.as_ref().and_then(|bind| { if is_array { -- GitLab