summaryrefslogtreecommitdiff
path: root/src/BRegex.cc
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-11-16 20:54:36 -0800
committerPixel <pixel@nobis-crew.org>2011-11-16 20:56:15 -0800
commit0c4f27688622053a35d5b14ad138a6cd0fba20c4 (patch)
treebe626a776ad031b880dc751de129b712e234ff1c /src/BRegex.cc
parent8ed05c55f14823b4f8782d3f096803c1d3f27d3a (diff)
Adding the Regex class.
Diffstat (limited to 'src/BRegex.cc')
-rw-r--r--src/BRegex.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/BRegex.cc b/src/BRegex.cc
new file mode 100644
index 0000000..a7b37b0
--- /dev/null
+++ b/src/BRegex.cc
@@ -0,0 +1,49 @@
+#include "BRegex.h"
+
+Balau::Regex::Regex(const char * regex, bool icase) throw (GeneralException) {
+ int r = regcomp(&m_regex, regex, REG_EXTENDED | (icase ? REG_ICASE : 0));
+ if (r)
+ throw GeneralException(getError(r));
+}
+
+Balau::Regex::Captures Balau::Regex::match(const char * str) throw (GeneralException) {
+ Captures ret;
+ regmatch_t * matches = (regmatch_t *) alloca((m_regex.re_nsub + 1) * sizeof(regmatch_t));
+
+ int r = regexec(&m_regex, str, m_regex.re_nsub + 1, matches, 0);
+
+ if (r == REG_NOMATCH)
+ return ret;
+
+ if (r != 0)
+ throw GeneralException(getError(r));
+
+ for (int i = 0; i < m_regex.re_nsub + 1; i++) {
+ if (matches[i].rm_so >= 0) {
+ regoff_t begin = matches[i].rm_so;
+ regoff_t end = matches[i].rm_eo;
+ String t(str + begin, end - begin);
+ ret.push_back(t);
+ }
+ }
+
+ return ret;
+}
+
+Balau::Regex::~Regex() {
+ regfree(&m_regex);
+}
+
+Balau::String Balau::Regex::getError(int err) {
+ size_t s;
+ char * t;
+
+ s = regerror(err, &m_regex, NULL, 0);
+ t = (char *) malloc(s);
+ regerror(err, &m_regex, t, s);
+ String r(t, s - 1);
+ free(t);
+
+ return r;
+}
+