RE_Matcher: Add MatchPrefix with bol/eol control

This commit is contained in:
Arne Welzel 2023-11-16 08:22:58 +01:00
parent abc32b3b46
commit a3bd3e4c50
2 changed files with 14 additions and 7 deletions

View file

@ -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;

View file

@ -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(); }