diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2016-10-19T14·32+0200 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2016-10-19T14·32+0200 |
commit | 795d9b866881871419651081eb112f23b2f5bff5 (patch) | |
tree | e51077f7170bb04bb003fdadcd0e462bc52d9776 /src/libutil/regex.cc | |
parent | 19c278de89fa60844fa1ea2b3c2abfa639f75b39 (diff) | |
parent | c935e8eeaf3ef5f49f66468ba551fd016fd76aa0 (diff) |
Merge branch 'regex' of https://github.com/groxxda/nix
Diffstat (limited to 'src/libutil/regex.cc')
-rw-r--r-- | src/libutil/regex.cc | 50 |
1 files changed, 0 insertions, 50 deletions
diff --git a/src/libutil/regex.cc b/src/libutil/regex.cc deleted file mode 100644 index 84274b3e1da9..000000000000 --- a/src/libutil/regex.cc +++ /dev/null @@ -1,50 +0,0 @@ -#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); -} - -} |