summaryrefslogtreecommitdiffstats
path: root/lualib/lua_util.lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2018-07-25 16:46:12 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2018-07-25 16:46:56 +0100
commitade28944a126040408f005971589d64f16332dbc (patch)
tree8b51f6008512e5dd5dbc080350b769f30f8b57bc /lualib/lua_util.lua
parent37d135cdbbb87fcca88c887a9b20259af47a57d4 (diff)
downloadrspamd-ade28944a126040408f005971589d64f16332dbc.tar.gz
rspamd-ade28944a126040408f005971589d64f16332dbc.zip
[Minor] Add default split pattern (on spaces)
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r--lualib/lua_util.lua38
1 files changed, 26 insertions, 12 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 5bf8dc026..520318fe6 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -25,15 +25,33 @@ local rspamd_util = require "rspamd_util"
local fun = require "fun"
local split_grammar = {}
+local spaces_split_grammar
+local space = lpeg.S' \t\n\v\f\r'
+local nospace = 1 - space
+local ptrim = space^0 * lpeg.C((space^0 * nospace^1)^0)
+local match = lpeg.match
+
local function rspamd_str_split(s, sep)
- local gr = split_grammar[sep]
-
- if not gr then
- local _sep = lpeg.P(sep)
- local elem = lpeg.C((1 - _sep)^0)
- local p = lpeg.Ct(elem * (_sep * elem)^0)
- gr = p
- split_grammar[sep] = gr
+ local gr
+ if not sep then
+ if not spaces_split_grammar then
+ local _sep = space
+ local elem = lpeg.C((1 - _sep)^0)
+ local p = lpeg.Ct(elem * (_sep * elem)^0)
+ spaces_split_grammar = p
+ end
+
+ gr = spaces_split_grammar
+ else
+ gr = split_grammar[sep]
+
+ if not gr then
+ local _sep = lpeg.P(sep)
+ local elem = lpeg.C((1 - _sep)^0)
+ local p = lpeg.Ct(elem * (_sep * elem)^0)
+ gr = p
+ split_grammar[sep] = gr
+ end
end
return gr:match(s)
@@ -50,10 +68,6 @@ end
exports.rspamd_str_split = rspamd_str_split
exports.str_split = rspamd_str_split
-local space = lpeg.S' \t\n\v\f\r'
-local nospace = 1 - space
-local ptrim = space^0 * lpeg.C((space^0 * nospace^1)^0)
-local match = lpeg.match
exports.rspamd_str_trim = function(s)
return match(ptrim, s)
end