diff options
author | Peter Melnichenko <mpeterval@gmail.com> | 2018-09-21 17:54:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-21 17:54:30 +0300 |
commit | da8f97a86a00b4f88e35c546b96aefa2b22cffbe (patch) | |
tree | 4c04d860534cb51844c280a76ae14c7d8e690923 | |
parent | 754a0ef9ebf1eb040e44ff92cefcd97ce9cf8435 (diff) | |
parent | 140919952ff5afff5389ad77024745a5d0b902a9 (diff) |
Merge pull request #274 from Tieske/unpack
added an unpack that honors the n field
-rw-r--r-- | examples/symbols.lua | 2 | ||||
-rw-r--r-- | lua/pl/compat.lua | 10 | ||||
-rw-r--r-- | lua/pl/func.lua | 3 | ||||
-rw-r--r-- | lua/pl/test.lua | 3 | ||||
-rw-r--r-- | lua/pl/utils.lua | 25 | ||||
-rw-r--r-- | tests/test-utils.lua | 10 |
6 files changed, 44 insertions, 9 deletions
diff --git a/examples/symbols.lua b/examples/symbols.lua index 1aed745..e73c4ba 100644 --- a/examples/symbols.lua +++ b/examples/symbols.lua @@ -4,7 +4,7 @@ local ops = require 'pl.operator' local List = require 'pl.List' local append,concat = table.insert,table.concat local compare,find_if,compare_no_order,imap,reduce,count_map = tablex.compare,tablex.find_if,tablex.compare_no_order,tablex.imap,tablex.reduce,tablex.count_map -local unpack = utils.unpack +local unpack = table.unpack function bindval (self,val) rawset(self,'value',val) diff --git a/lua/pl/compat.lua b/lua/pl/compat.lua index f3832b8..775e2cc 100644 --- a/lua/pl/compat.lua +++ b/lua/pl/compat.lua @@ -128,7 +128,6 @@ end --- pack an argument list into a table. -- @param ... any arguments -- @return a table with field n set to the length --- @return the length -- @function table.pack if not table.pack then function table.pack (...) -- luacheck: ignore @@ -136,6 +135,15 @@ if not table.pack then end end +--- unpack a table and return the elements. +-- @param t table to unpack +-- @param[opt] i index from which to start unpacking, defaults to 1 +-- @param[opt] t index of the last element to unpack, defaults to #t +-- @return multiple returns values from the table +if not table.unpack then + table.unpack = unpack -- luacheck: ignore +end + ------ -- return the full path where a Lua module name would be matched. -- @param mod module name, possibly dotted diff --git a/lua/pl/func.lua b/lua/pl/func.lua index f1f41f0..daac0fc 100644 --- a/lua/pl/func.lua +++ b/lua/pl/func.lua @@ -21,12 +21,11 @@ local type,setmetatable,getmetatable,rawset = type,setmetatable,getmetatable,raw local concat,append = table.concat,table.insert local tostring = tostring local utils = require 'pl.utils' -local pairs,rawget,unpack = pairs,rawget,utils.unpack +local pairs,rawget,unpack,pack = pairs,rawget,utils.unpack,utils.pack local tablex = require 'pl.tablex' local map = tablex.map local _DEBUG = rawget(_G,'_DEBUG') local assert_arg = utils.assert_arg -local pack = table.pack or function(...) return { n = select("#", ...), ... } end local func = {} diff --git a/lua/pl/test.lua b/lua/pl/test.lua index ddf8345..2c3a5da 100644 --- a/lua/pl/test.lua +++ b/lua/pl/test.lua @@ -12,11 +12,10 @@ local tablex = require 'pl.tablex' local utils = require 'pl.utils' local pretty = require 'pl.pretty' local path = require 'pl.path' -local type,unpack = type,utils.unpack +local type,unpack,pack = type,utils.unpack,utils.pack local clock = os.clock local debug = require 'debug' local io = io -local pack = table.pack or function(...) return { select("#", ...), ...} end local function dump(x) if type(x) == 'table' and not (getmetatable(x) and getmetatable(x).__tostring) then diff --git a/lua/pl/utils.lua b/lua/pl/utils.lua index bcd0e01..5b5a0d8 100644 --- a/lua/pl/utils.lua +++ b/lua/pl/utils.lua @@ -8,7 +8,7 @@ local format = string.format local compat = require 'pl.compat' local stdout = io.stdout local append = table.insert -local unpack = rawget(_G,'unpack') or rawget(table,'unpack') +local _unpack = table.unpack -- always injected by 'compat' local utils = { _VERSION = "1.5.4", @@ -19,9 +19,28 @@ local utils = { execute = compat.execute, dir_separator = compat.dir_separator, is_windows = compat.is_windows, - unpack = unpack } + +--- pack an argument list into a table. +-- @param ... any arguments +-- @return a table with field n set to the length +-- @return table +-- @function utils.pack +utils.pack = table.pack -- added here to be symmetrical with unpack + +--- unpack a table and return its contents. +-- NOTE: this implementation differs from the Lua implementation in the way +-- that this one DOES honor the n field in the table t, such that it is 'nil-safe'. +-- @param t table to unpack +-- @param[opt] i index from which to start unpacking, defaults to 1 +-- @param[opt] t index of the last element to unpack, defaults to `t.n` or #t +-- @return multiple returns values from the table +-- @function utils.unpack +function utils.unpack(t, i, j) + return _unpack(t, i or 1, j or t.n or #t) +end + --- end this program gracefully. -- @param code The exit code or a message to be printed -- @param ... extra arguments for message's format' @@ -209,7 +228,7 @@ end -- @usage first,next = splitv('jane:doe',':') -- @see split function utils.splitv (s,re) - return unpack(utils.split(s,re)) + return _unpack(utils.split(s,re)) end --- convert an array of values to strings. diff --git a/tests/test-utils.lua b/tests/test-utils.lua index b115bea..9ddc39d 100644 --- a/tests/test-utils.lua +++ b/tests/test-utils.lua @@ -123,6 +123,16 @@ asserteq(type(sin),"function") asserteq(type(abs),"function") +-- packing and unpacking arguments in a nil-safe way +local t = utils.pack(nil, nil, "hello", nil) +asserteq(t.n, 4) -- the last nil does count as an argument + +local arg1, arg2, arg3, arg4 = utils.unpack(t) +assert(arg1 == nil) +assert(arg2 == nil) +asserteq("hello", arg3) +assert(arg4 == nil) + |