diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-03-06 13:07:30 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-03-06 13:07:30 +0000 |
commit | 8272fa5dcda360bc573a3877194a6430c301738c (patch) | |
tree | 5ac7b2511733638d38590c4bb956a265ed11a5cd /src/lua | |
parent | 2868939004792107fa2dae733667ca934b1cd90e (diff) | |
download | rspamd-8272fa5dcda360bc573a3877194a6430c301738c.tar.gz rspamd-8272fa5dcda360bc573a3877194a6430c301738c.zip |
Distinguish match and search in lua regexp.
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_regexp.c | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/src/lua/lua_regexp.c b/src/lua/lua_regexp.c index d363ee4a6..a67bed334 100644 --- a/src/lua/lua_regexp.c +++ b/src/lua/lua_regexp.c @@ -49,6 +49,7 @@ LUA_FUNCTION_DEF (regexp, create); LUA_FUNCTION_DEF (regexp, create_cached); LUA_FUNCTION_DEF (regexp, get_cached); LUA_FUNCTION_DEF (regexp, get_pattern); +LUA_FUNCTION_DEF (regexp, search); LUA_FUNCTION_DEF (regexp, match); LUA_FUNCTION_DEF (regexp, split); LUA_FUNCTION_DEF (regexp, destroy); @@ -56,6 +57,7 @@ LUA_FUNCTION_DEF (regexp, destroy); static const struct luaL_reg regexplib_m[] = { LUA_INTERFACE_DEF (regexp, get_pattern), LUA_INTERFACE_DEF (regexp, match), + LUA_INTERFACE_DEF (regexp, search), LUA_INTERFACE_DEF (regexp, split), LUA_INTERFACE_DEF (regexp, destroy), {"__tostring", lua_regexp_get_pattern}, @@ -219,8 +221,8 @@ lua_regexp_get_pattern (lua_State *L) } /*** - * @method re:match(line) - * Match line against regular expression object. If line matches then this + * @method re:search(line) + * Search line in regular expression object. If line matches then this * function returns the table of captured strings. Otherwise, nil is returned. * * @param {string} line match the specified line against regexp object @@ -228,13 +230,13 @@ lua_regexp_get_pattern (lua_State *L) * @example * local re = regexp.create_cached('/^\s*([0-9]+)\s*$/') * -- returns nil - * local m1 = re:match('blah') - * local m2 = re:match(' 190 ') + * local m1 = re:search('blah') + * local m2 = re:search(' 190 ') * -- prints '190' * print(m2[1]) */ static int -lua_regexp_match (lua_State *L) +lua_regexp_search (lua_State *L) { struct rspamd_lua_regexp *re = lua_check_regexp (L); const gchar *data; @@ -267,6 +269,44 @@ lua_regexp_match (lua_State *L) } /*** + * @method re:match(line) + * Matches line against the regular expression and return true if line matches + * (partially or completely) + * + * @param {string} line match the specified line against regexp object + * @return {table or nil} table of strings matched or nil + * @example + * local re = regexp.create_cached('/^\s*([0-9]+)\s*$/') + * -- returns nil + * local m1 = re:match('blah') + * local m2 = re:match(' 190 ') + */ +static int +lua_regexp_match (lua_State *L) +{ + struct rspamd_lua_regexp *re = lua_check_regexp (L); + const gchar *data; + gsize len; + + if (re) { + data = luaL_checklstring (L, 2, &len); + if (data) { + if (rspamd_regexp_search (re->re, data, len, NULL, NULL, FALSE)) { + lua_pushboolean (L, TRUE); + } + else { + lua_pushboolean (L, FALSE); + } + return 1; + } + } + + lua_pushnil (L); + + return 1; +} + +/*** * @method re:split(line) * Split line using the specified regular expression. * Breaks the string on the pattern, and returns an array of the tokens. |