]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add spairs function to lua lib
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 15 Oct 2017 11:42:56 +0000 (12:42 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 15 Oct 2017 11:42:56 +0000 (12:42 +0100)
lualib/lua_util.lua

index f048432bfab9b393c39932f63d315c74a886e736..a160bd85eb31c6c3133bbaab1a183b872e91bcb9 100644 (file)
@@ -165,4 +165,38 @@ exports.unpack = function(t)
   return unpack_function(t)
 end
 
+-- Sorted iteration:
+-- for k,v in spairs(t) do ... end
+--
+-- or with custom comparison:
+-- for k, v in spairs(t, function(t, a, b) return t[a] < t[b] end)
+--
+-- optional limit is also available (e.g. return top X elements)
+local function spairs(t, order, lim)
+  -- collect the keys
+  local keys = {}
+  for k in pairs(t) do keys[#keys+1] = k end
+
+  -- if order function given, sort by it by passing the table and keys a, b,
+  -- otherwise just sort the keys
+  if order then
+    table.sort(keys, function(a,b) return order(t, a, b) end)
+  else
+    table.sort(keys)
+  end
+
+  -- return the iterator function
+  local i = 0
+  return function()
+    i = i + 1
+    if not lim or i <= lim then
+      if keys[i] then
+        return keys[i], t[keys[i]]
+      end
+    end
+  end
+end
+
+exports.spairs = spairs
+
 return exports