diff --git a/src/RE.cc b/src/RE.cc index b0038a333a..db5dad6d07 100644 --- a/src/RE.cc +++ b/src/RE.cc @@ -318,7 +318,7 @@ bool RE_Match_State::Match(const u_char* bv, int n, bool bol, bool eol, bool cle return accepted_matches.size() != old_matches; } -int Specific_RE_Matcher::LongestMatch(const u_char* bv, int n) { +int Specific_RE_Matcher::LongestMatch(const u_char* bv, int n, bool bol, bool eol) { if ( ! dfa ) // An empty pattern matches anything. return 0; @@ -327,11 +327,13 @@ int Specific_RE_Matcher::LongestMatch(const u_char* bv, int n) { int last_accept = -1; DFA_State* d = dfa->StartState(); - d = d->Xtion(ecs[SYM_BOL], dfa); - if ( ! d ) - return -1; + if ( bol ) { + d = d->Xtion(ecs[SYM_BOL], dfa); + if ( ! d ) + return -1; + } - if ( d->Accept() ) + if ( d->Accept() ) // initial state or bol match (e.g, / */ or /^ ?/) last_accept = 0; for ( int i = 0; i < n; ++i ) { @@ -345,7 +347,7 @@ int Specific_RE_Matcher::LongestMatch(const u_char* bv, int n) { last_accept = i + 1; } - if ( d ) { + if ( d && eol ) { d = d->Xtion(ecs[SYM_EOL], dfa); if ( d && d->Accept() ) return n; diff --git a/src/RE.h b/src/RE.h index ff6c084c92..f68f3482bb 100644 --- a/src/RE.h +++ b/src/RE.h @@ -106,7 +106,7 @@ public: int LongestMatch(const char* s); int LongestMatch(const String* s); - int LongestMatch(const u_char* bv, int n); + int LongestMatch(const u_char* bv, int n, bool bol = true, bool eol = true); EquivClass* EC() { return &equiv_class; } @@ -220,6 +220,11 @@ public: int MatchPrefix(const String* s) { return re_exact->LongestMatch(s); } int MatchPrefix(const u_char* s, int n) { return re_exact->LongestMatch(s, n); } + // MatchPrefix() version allowing control of bol and eol. + // This can be useful when searching for a pattern with an + // anchor within a larger string. + int MatchPrefix(const u_char* s, int n, bool bol, bool eol) { return re_exact->LongestMatch(s, n, bol, eol); } + bool Match(const u_char* s, int n) { return re_anywhere->Match(s, n); } const char* PatternText() const { return re_exact->PatternText(); }