aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/lua-fun
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2020-07-28 16:25:30 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2020-07-28 16:25:30 +0100
commit9cb811037b7ea48ac7f19b9ff77115f37c52973d (patch)
treebbf05e686d4d1b4d3aaeae001caa6c26c5ee05b5 /contrib/lua-fun
parent0e21da4e3833d702db12e4129926d7918a1ac527 (diff)
downloadrspamd-9cb811037b7ea48ac7f19b9ff77115f37c52973d.tar.gz
rspamd-9cb811037b7ea48ac7f19b9ff77115f37c52973d.zip
[Project] Support fun iterators on rspamd_text objects
Diffstat (limited to 'contrib/lua-fun')
-rw-r--r--contrib/lua-fun/fun.lua18
1 files changed, 18 insertions, 0 deletions
diff --git a/contrib/lua-fun/fun.lua b/contrib/lua-fun/fun.lua
index bf291712b..e16fe07ae 100644
--- a/contrib/lua-fun/fun.lua
+++ b/contrib/lua-fun/fun.lua
@@ -9,6 +9,10 @@
local exports = {}
local methods = {}
+-- Rspamd specific
+local rspamd_text = require "rspamd_text"
+local text_cookie = rspamd_text.cookie
+
-- compatibility with Lua 5.1/5.2
local unpack = rawget(table, "unpack") or unpack
@@ -87,6 +91,15 @@ local string_gen = function(param, state)
return state, r
end
+local text_gen = function(param, state)
+ local state = state + 1
+ if state > #param then
+ return nil
+ end
+ local r = string.char(param:at(state))
+ return state, r
+end
+
local ipairs_gen = ipairs({}) -- get the generating function from ipairs
local pairs_gen = pairs({ a = 0 }) -- get the generating function from pairs
@@ -123,6 +136,11 @@ local rawiter = function(obj, param, state)
return nil_gen, nil, nil
end
return string_gen, obj, 0
+ elseif (type(obj) == "userdata" and obj.cookie == text_cookie) then
+ if #obj == 0 then
+ return nil_gen, nil, nil
+ end
+ return text_gen, obj, 0
end
error(string.format('object %s of type "%s" is not iterable',
obj, type(obj)))