Created
April 17, 2015 01:04
-
-
Save nefftd/79845a575509b855ed84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- util.lua: Shared utility functions | |
-- Namespace | |
local util = {} | |
-- Error, type checking | |
do | |
local errfmt = "bad argument #%s to '%s' (%s expected, got %s)" | |
local function checktypelist(t,a1,...) | |
if a1 ~= nil then | |
return t == a1 or checktypelist(t,...) | |
end | |
end | |
function util.argcheck(val,argn,...) | |
if checktypelist(type(val),...) then return end | |
local fname = debug.getinfo(2,'n').name or '?' | |
local types = table.concat({util.tostringall(...)},'/') | |
argn = tonumber(argn) or '?' | |
error(errfmt:format(argn,fname,types,type(val)),3) | |
end | |
end | |
-- String functions | |
do | |
local function tsa(n,a,...) | |
if n > 0 then | |
return tostring(a),tsa(n-1,...) | |
end | |
end | |
function util.tostringall(...) | |
return tsa(select('#',...),...) | |
end | |
end | |
return util |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment