33#include <libdap/util.h>
35#include "BESInternalError.h"
40#if !HAVE_WORKING_REGEX
42BESRegex::init(
const char *t)
44 d_preg.reset(
new regex_t);
46 int result = regcomp(d_preg.get(), t, REG_EXTENDED);
48 size_t msg_len = regerror(result, d_preg.get(),
nullptr, (
size_t)0);
50 vector<char> msg(msg_len+1);
51 regerror(result, d_preg.get(), msg.data(), msg_len);
52 string err = string(
"BESRegex error: ") + string(msg.data(), msg_len);
53 throw BESError(err, BES_SYNTAX_USER_ERROR, __FILE__, __LINE__);
59 regfree(d_preg.get());
74 throw BESInternalError(
"Position exceed length in BESRegex::match()", __FILE__, __LINE__);
80 auto target = string(s+pos, len-pos);
81 bool found = regex_search(target,
match, d_exp);
87 vector<regmatch_t> pmatch(len+1);
88 int result = regexec(d_preg.get(), s + pos, len, pmatch.data(), 0);
90 if (result == REG_NOMATCH)
93 matchnum = (
int)(pmatch[0].rm_eo - pmatch[0].rm_so);
107#if HAVE_WORKING_REGEX
109 bool found = regex_search(s,
match, d_exp);
112 return match(s.c_str(), (
int)s.size(), 0);
129#if HAVE_WORKING_REGEX
131 auto target = string(s+pos, len-pos);
132 bool found = regex_search(target,
match, d_exp);
133 if (found &&
match.ready()) {
135 return (
int)
match.position();
143 if (!libdap::size_ok(
sizeof(regmatch_t), len+1))
154 vector<regmatch_t> pmatch(len+1);
155 int result = regexec(d_preg.get(), s + pos, len, pmatch.data(), 0);
156 if (result == REG_NOMATCH)
161 for (
int i = 1; i < len; ++i)
162 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
165 matchlen = (
int)(pmatch[m].rm_eo - pmatch[m].rm_so);
166 auto matchpos = (
int)pmatch[m].rm_so;
181#if HAVE_WORKING_REGEX
183 bool found = regex_search(s,
match, d_exp);
184 if (found &&
match.ready()) {
186 return (
int)
match.position();
193 return search(s.c_str(), (
int)s.size(), matchlen, 0);
exception thrown if internal error encountered
int match(const char *s, int len, int pos=0) const
Does the pattern match.
int search(const char *s, int len, int &matchlen, int pos=0) const
Where does the pattern match.