From: Vsevolod Stakhov Date: Fri, 17 Mar 2017 11:18:14 +0000 (+0000) Subject: [Minor] Update lua-fun X-Git-Tag: 1.5.3~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d3c57dbed59c112112c350f6e83da316a92364d8;p=rspamd.git [Minor] Update lua-fun --- diff --git a/contrib/lua-fun/fun.lua b/contrib/lua-fun/fun.lua index 6a536d759..bf291712b 100644 --- a/contrib/lua-fun/fun.lua +++ b/contrib/lua-fun/fun.lua @@ -1,7 +1,7 @@ --- --- Lua Fun - a high-performance functional programming library for LuaJIT --- ---- Copyright (c) 2013-2014 Roman Tsisyk +--- Copyright (c) 2013-2017 Roman Tsisyk --- --- Distributed under the MIT/X11 License. See COPYING.md for more details. --- @@ -9,6 +9,9 @@ local exports = {} local methods = {} +-- compatibility with Lua 5.1/5.2 +local unpack = rawget(table, "unpack") or unpack + -------------------------------------------------------------------------------- -- Tools -------------------------------------------------------------------------------- @@ -84,6 +87,8 @@ local string_gen = function(param, 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 local map_gen = function(tab, key) local value @@ -293,10 +298,10 @@ exports.rands = rands local nth = function(n, gen_x, param_x, state_x) assert(n > 0, "invalid first argument to nth") -- An optimization for arrays and strings - if gen_x == ipairs then + if gen_x == ipairs_gen then return param_x[n] elseif gen_x == string_gen then - if n < #param_x then + if n <= #param_x then return string.sub(param_x, n, n) else return nil @@ -593,7 +598,7 @@ methods.reduce = methods.foldl exports.reduce = exports.foldl local length = function(gen, param, state) - if gen == ipairs or gen == string_gen then + if gen == ipairs_gen or gen == string_gen then return #param end local len = 0 @@ -938,7 +943,7 @@ local chain_gen_r2 = function(param, state, state_x, ...) if state_x == nil then local i = state[1] i = i + 1 - if i > #param / 3 then + if param[3 * i - 1] == nil then return nil end local state_x = param[3 * i] @@ -978,7 +983,7 @@ exports.chain = chain -- Operators -------------------------------------------------------------------------------- -operator = { +local operator = { ---------------------------------------------------------------------------- -- Comparison operators ---------------------------------------------------------------------------- @@ -1033,8 +1038,20 @@ methods.op = operator -- a special syntax sugar to export all functions to the global table setmetatable(exports, { - __call = function(t) - for k, v in pairs(t) do _G[k] = v end + __call = function(t, override) + for k, v in pairs(t) do + if _G[k] ~= nil then + local msg = 'function ' .. k .. ' already exists in global scope.' + if override then + _G[k] = v + print('WARNING: ' .. msg .. ' Overwritten.') + else + print('NOTICE: ' .. msg .. ' Skipped.') + end + else + _G[k] = v + end + end end, })