2014-10-03 16:47:16 +00:00
|
|
|
|
#include "regex.hh"
|
|
|
|
|
#include "types.hh"
|
|
|
|
|
|
2014-11-25 10:47:06 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2014-10-03 16:47:16 +00:00
|
|
|
|
namespace nix {
|
|
|
|
|
|
2014-11-25 10:47:06 +00:00
|
|
|
|
Regex::Regex(const string & pattern, bool subs)
|
2014-10-03 16:47:16 +00:00
|
|
|
|
{
|
|
|
|
|
/* Patterns must match the entire string. */
|
2014-11-25 10:47:06 +00:00
|
|
|
|
int err = regcomp(&preg, ("^(" + pattern + ")$").c_str(), (subs ? 0 : REG_NOSUB) | REG_EXTENDED);
|
|
|
|
|
if (err) throw RegexError(format("compiling pattern ‘%1%’: %2%") % pattern % showError(err));
|
|
|
|
|
nrParens = subs ? std::count(pattern.begin(), pattern.end(), '(') : 0;
|
2014-10-03 16:47:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Regex::~Regex()
|
|
|
|
|
{
|
|
|
|
|
regfree(&preg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Regex::matches(const string & s)
|
|
|
|
|
{
|
|
|
|
|
int err = regexec(&preg, s.c_str(), 0, 0, 0);
|
|
|
|
|
if (err == 0) return true;
|
|
|
|
|
else if (err == REG_NOMATCH) return false;
|
|
|
|
|
throw Error(format("matching string ‘%1%’: %2%") % s % showError(err));
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 10:47:06 +00:00
|
|
|
|
bool Regex::matches(const string & s, Subs & subs)
|
|
|
|
|
{
|
|
|
|
|
regmatch_t pmatch[nrParens + 2];
|
|
|
|
|
int err = regexec(&preg, s.c_str(), nrParens + 2, pmatch, 0);
|
|
|
|
|
if (err == 0) {
|
|
|
|
|
for (unsigned int n = 2; n < nrParens + 2; ++n)
|
|
|
|
|
if (pmatch[n].rm_eo != -1)
|
|
|
|
|
subs[n - 2] = string(s, pmatch[n].rm_so, pmatch[n].rm_eo - pmatch[n].rm_so);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (err == REG_NOMATCH) return false;
|
|
|
|
|
throw Error(format("matching string ‘%1%’: %2%") % s % showError(err));
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 16:47:16 +00:00
|
|
|
|
string Regex::showError(int err)
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
regerror(err, &preg, buf, sizeof(buf));
|
|
|
|
|
return string(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|