diff options
author | Pixel <pixel@nobis-crew.org> | 2008-10-16 02:44:54 +0200 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2008-10-16 02:44:54 +0200 |
commit | ab4700c941b95a3c7f6c19b42a1e0e2b35e1fe81 (patch) | |
tree | b05936a89c487de372be657020ec619da910f543 | |
parent | 59288143eaf405ea4f855464dea1b1c0eb382ad5 (diff) |
Adding submatches system.
-rw-r--r-- | include/BRegex.h | 2 | ||||
-rw-r--r-- | lib/LuaRegex.cc | 27 | ||||
-rw-r--r-- | lib/Regex.cc | 4 |
3 files changed, 28 insertions, 5 deletions
diff --git a/include/BRegex.h b/include/BRegex.h index 4d4f98f..ced5e25 100644 --- a/include/BRegex.h +++ b/include/BRegex.h @@ -29,7 +29,7 @@ class Regex : public Base { Regex(const String &, int = REG_EXTENDED, int = 0) throw (GeneralException); Regex(const Regex &); ~Regex(); - bool Match(const String &) const; + bool Match(const String &, int nmatch = 0, regmatch_t * = 0) const; String GetPattern() const; private: regex_t preg; diff --git a/lib/LuaRegex.cc b/lib/LuaRegex.cc index 16cf732..00c44ee 100644 --- a/lib/LuaRegex.cc +++ b/lib/LuaRegex.cc @@ -86,17 +86,40 @@ void LuaRegex::pushstatics(Lua * L) throw (GeneralException) { L->settable(LUA_GLOBALSINDEX); } +#define MAX_MATCHES 64 + int sLua_Regex::Regex_proceed(Lua * L, int n, Regex * r, int caller) { + int r = 0, i, n; + bool m; String s; + regmatch_t pmatches[MAX_MATCHES]; switch (caller) { case REGEX_MATCH: s = L->tostring(2); - L->push(r->Match(s)); + L->push(m = r->Match(s, MAX_MATCHES, pmatches)); + if (!m) + return 1; + L->newtable(); + n = 1; + for (i = 0; i < MAX_MATCHES; i++) { + if (pmatches[i].rm_so == -1) + continue; + L->push((lua_Number) n++); + L->newtable(); + L->push("start"); + L->push((lua_Number) patches.rm_so + 1); + L->settable(); + L->push("size"); + L->push((lua_Number) patches.rm_eo - patches.rm_so); + L->settable(); + L->settable(); + } + r = 2; break; } - return 1; + return r; } int sLua_Regex::Regex_proceed_statics(Lua * L, int n, int caller) { diff --git a/lib/Regex.cc b/lib/Regex.cc index c614d85..68ccba8 100644 --- a/lib/Regex.cc +++ b/lib/Regex.cc @@ -54,8 +54,8 @@ Regex::~Regex() { free(pattern); } -bool Regex::Match(const String & s) const { - if (regexec(&preg, s.to_charp(), 0, 0, eflags)) { +bool Regex::Match(const String & s, int nmatch, regmatch_t * pmatch) const { + if (regexec(&preg, s.to_charp(), nmatch, pmatch, eflags)) { return false; } else { return true; |