diff --git a/boreal/src/compiler/variable.rs b/boreal/src/compiler/variable.rs index 0d036cd2..1888175b 100644 --- a/boreal/src/compiler/variable.rs +++ b/boreal/src/compiler/variable.rs @@ -89,20 +89,7 @@ pub(crate) fn compile_variable( regex::compile_regex(&ast.into(), dot_all, &modifiers) } VariableDeclarationValue::HexString(hex_string) => { - // Nocase, fullword and wide is not compatible with hex strings - modifiers.nocase = false; - modifiers.fullword = false; - modifiers.wide = false; - - let hir = hex_string.into(); - match only_literals::hir_to_only_literals(&hir) { - Some(literals) => Ok(CompiledVariable { - literals, - matcher_kind: matcher::MatcherKind::Literals, - non_wide_regex: None, - }), - None => regex::compile_regex(&hir, true, &modifiers), - } + regex::compile_regex(&hex_string.into(), true, &modifiers) } }; diff --git a/boreal/src/compiler/variable/only_literals.rs b/boreal/src/compiler/variable/only_literals.rs index 339b6323..b451767d 100644 --- a/boreal/src/compiler/variable/only_literals.rs +++ b/boreal/src/compiler/variable/only_literals.rs @@ -1,9 +1,9 @@ use crate::regex::{visit, Hir, VisitAction, Visitor}; /// Can the hex string be expressed using only literals. -pub(super) fn hir_to_only_literals(hir: &Hir) -> Option>> { +pub(super) fn hir_to_only_literals(hir: &Hir, dot_all: bool) -> Option>> { // TODO: move this count into the HirStatistics visitor - match visit(hir, CountLiterals::new(true)) { + match visit(hir, CountLiterals::new(dot_all)) { Some(count) if count < 100 => visit(hir, Literals::new()), Some(_) | None => None, } @@ -297,7 +297,7 @@ mod tests { let regex = parse_regex_string(regex); let hir = regex.ast.into(); - let res = hir_to_only_literals(&hir); + let res = hir_to_only_literals(&hir, true); match &res { Some(v) => assert_eq!(v, expected.unwrap()), None => assert!(expected.is_none()), @@ -342,7 +342,7 @@ mod tests { let hex_string = parse_hex_string(hex_string); let hir = hex_string.into(); - let res = hir_to_only_literals(&hir); + let res = hir_to_only_literals(&hir, true); match &res { Some(v) => assert_eq!(v, expected.unwrap()), None => assert!(expected.is_none()), diff --git a/boreal/src/compiler/variable/regex.rs b/boreal/src/compiler/variable/regex.rs index 89cfd973..3f28140c 100644 --- a/boreal/src/compiler/variable/regex.rs +++ b/boreal/src/compiler/variable/regex.rs @@ -5,7 +5,7 @@ use crate::regex::{regex_hir_to_string, visit, Hir, Regex, VisitAction, Visitor} use super::literals::LiteralsDetails; use super::matcher::MatcherKind; -use super::{CompiledVariable, VariableCompilationError}; +use super::{only_literals, CompiledVariable, VariableCompilationError}; /// Build a matcher for the given regex and string modifiers. /// @@ -18,6 +18,18 @@ pub(super) fn compile_regex( dot_all: bool, modifiers: &VariableModifiers, ) -> Result { + // Try to convert into only literals if possible + // TODO: handle more modifiers + if !modifiers.nocase && !modifiers.wide { + if let Some(literals) = only_literals::hir_to_only_literals(hir, dot_all) { + return Ok(CompiledVariable { + literals, + matcher_kind: MatcherKind::Literals, + non_wide_regex: None, + }); + } + } + let LiteralsDetails { mut literals, pre_hir,