From 316af6576a3a4d008a9af4bbb093133972ff0bdf Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 28 May 2016 22:26:40 +0100 Subject: [PATCH] [Feature] Use generic global string split function --- src/lua/global_functions.lua | 8 ++++++++ src/plugins/lua/hfilter.lua | 29 ++--------------------------- src/plugins/lua/ratelimit.lua | 32 +++----------------------------- 3 files changed, 13 insertions(+), 56 deletions(-) diff --git a/src/lua/global_functions.lua b/src/lua/global_functions.lua index d2c742a9c..9cf5f9a6f 100644 --- a/src/lua/global_functions.lua +++ b/src/lua/global_functions.lua @@ -55,3 +55,11 @@ function rspamd_parse_redis_server(module_name) return ret end + +function rspamd_str_split(s, sep) + local lpeg = require "lpeg" + sep = lpeg.P(sep) + local elem = lpeg.C((1 - sep)^0) + local p = lpeg.Ct(elem * (sep * elem)^0) -- make a table capture + return lpeg.match(p, s) +end \ No newline at end of file diff --git a/src/plugins/lua/hfilter.lua b/src/plugins/lua/hfilter.lua index 1785e8a7b..3e39638a6 100644 --- a/src/plugins/lua/hfilter.lua +++ b/src/plugins/lua/hfilter.lua @@ -125,31 +125,6 @@ local function check_regexp(str, regexp_text) return false end -local function split(str, delim, maxNb) - -- Eliminate bad cases... - if string.find(str, delim) == nil then - return { str } - end - if maxNb == nil or maxNb < 1 then - maxNb = 0 -- No limit - end - local result = {} - local pat = "(.-)" .. delim .. "()" - local nb = 0 - local lastPos - for part, pos in string.gmatch(str, pat) do - nb = nb + 1 - result[nb] = part - lastPos = pos - if nb == maxNb then break end - end - -- Handle the last field - if nb ~= maxNb then - result[nb + 1] = string.sub(str, lastPos) - end - return result -end - local function check_fqdn(domain) if check_regexp(domain, '(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?