about summary refs log tree commit diff
path: root/src/libutil/regex.cc
blob: 84274b3e1da93d1efce5984d7e1fecf166510395 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "regex.hh"
#include "types.hh"

#include <algorithm>

namespace nix {

Regex::Regex(const string & pattern, bool subs)
{
    /* Patterns must match the entire string. */
    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;
}

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

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

string Regex::showError(int err)
{
    char buf[256];
    regerror(err, &preg, buf, sizeof(buf));
    return string(buf);
}

}