commit 9cb8110: [Project] Support fun iterators on rspamd_text objects

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Jul 28 16:21:09 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-07-28 16:25:30 +0100
URL: https://github.com/rspamd/rspamd/commit/9cb811037b7ea48ac7f19b9ff77115f37c52973d

[Project] Support fun iterators on rspamd_text objects

---
 contrib/lua-fun/fun.lua | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

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)))


More information about the Commits mailing list