64void Regex::init(
const char *t) {
66 DBG(cerr <<
"Regex::init() - BEGIN" << endl);
68 DBG(cerr <<
"Regex::init() - creating new regex..." << endl);
69 d_preg =
static_cast<void *
>(
new regex_t);
71 DBG(cerr <<
"Regex::init() - Calling regcomp()..." << endl);
72 int result =
regcomp(
static_cast<regex_t *
>(d_preg), t, REG_EXTENDED);
75 DBG(cerr <<
"Regex::init() - Call to regcomp FAILED" << endl);
76 DBG(cerr <<
"Regex::init() - Calling regerror()..." << endl);
78 regerror(result,
static_cast<regex_t *
>(d_preg),
static_cast<char *
>(NULL),
static_cast<size_t>(0));
80 DBG(cerr <<
"Regex::init() - Creating message" << endl);
81 vector<char> msg(msg_len + 1);
83 DBG(cerr <<
"Regex::init() - Calling regerror() again..." << endl);
84 regerror(result,
static_cast<regex_t *
>(d_preg), msg.data(), msg_len);
85 DBG(cerr <<
"Regex::init() - Throwing libdap::Error" << endl);
86 throw Error(
string(
"Regex error: ") +
string(msg.data()));
90 DBG(cerr <<
"Regex::init() - Call to regcomp() SUCCEEDED" << endl);
91 DBG(cerr <<
"Regex::init() - END" << endl);
99Regex::init(
const string &t)
107 regfree(
static_cast<regex_t *
>(d_preg));
108 delete static_cast<regex_t *
>(d_preg);
141 regmatch_t *pmatch =
new regmatch_t[len + 1];
144 int result =
regexec(
static_cast<regex_t *
>(d_preg), ss.substr(pos, len - pos).c_str(), len, pmatch, 0);
146 if (result == REG_NOMATCH)
149 matchnum = pmatch[0].rm_eo - pmatch[0].rm_so;
157 throw Error(
"Position exceed length in Regex::match()");
160 auto target = string(s + pos, len - pos);
161 bool found = regex_search(target,
match, d_exp);
163 return (
int)
match.length();
177 bool found = regex_search(s,
match, d_exp);
179 return (
int)
match.length();
183 return match(s.c_str(), s.length(), 0);
200 if (!
size_ok(
sizeof(regmatch_t), len + 1))
211 regmatch_t *pmatch =
new regmatch_t[len + 1];
214 int result =
regexec(
static_cast<regex_t *
>(d_preg), ss.substr(pos, len - pos).c_str(), len, pmatch, 0);
215 if (result == REG_NOMATCH) {
223 for (
int i = 1; i < len; ++i)
224 if (pmatch[i].rm_so != -1 && pmatch[i].rm_so < pmatch[m].rm_so)
227 matchlen = pmatch[m].rm_eo - pmatch[m].rm_so;
228 int matchpos = pmatch[m].rm_so;
238 auto target = string(s + pos, len - pos);
239 bool found = regex_search(target,
match, d_exp);
240 matchlen = (int)
match.length();
242 return (
int)
match.position();
257 bool found = regex_search(s,
match, d_exp);
258 matchlen = (int)
match.length();
260 return (
int)
match.position();
265 return search(s.c_str(), s.length(), matchlen, 0);
A class for error processing.
Regex(const char *s)
initialize a Regex with a C string
int search(const char *s, int len, int &matchlen, int pos=0) const
How much of the string does the pattern match.
int match(const char *s, int len, int pos=0) const
Does the pattern match.
top level DAP object to house generic methods
bool size_ok(unsigned int sz, unsigned int nelem)
sanitize the size of an array. Test for integer overflow when dynamically allocating an array.