cleanup & assert fixes

This commit is contained in:
Pinya
2017-12-27 19:41:04 +03:00
parent c145da845e
commit 7d31b74325
+16 -19
View File
@@ -56,20 +56,20 @@ function select(n, ...)
assert(type(n) == "number" or (type(n) == "string" and n == "#"), format("bad argument #1 to 'select' (number expected, got %s)", n and type(n) or "no value")) assert(type(n) == "number" or (type(n) == "string" and n == "#"), format("bad argument #1 to 'select' (number expected, got %s)", n and type(n) or "no value"))
if type(n) == "string" then if type(n) == "string" then
if type(arg) == "table" then return getn(arg)
return getn(arg)
else
return 1
end
end end
local temp = {} if n == 1 then
return unpack(arg)
end
local args = {}
for i = n, getn(arg) do for i = n, getn(arg) do
tinsert(temp, arg[i]) args[i-n+1] = arg[i]
end end
return unpack(temp) return unpack(args)
end end
function math.modf(i) function math.modf(i)
@@ -110,13 +110,12 @@ strmatch = string.match
function string.split(delimiter, str) function string.split(delimiter, str)
assert(type(delimiter) == "string" or type(str) == "number", format("bad argument #1 to 'split' (string expected, got %s)", delimiter and type(delimiter) or "no value")) assert(type(delimiter) == "string" or type(str) == "number", format("bad argument #1 to 'split' (string expected, got %s)", delimiter and type(delimiter) or "no value"))
assert(type(str) == "string", format("bad argument #2 to 'split' (string expected, got %s)", str and type(str) or "no value")) assert(type(str) == "string" or type(str) == "number", format("bad argument #2 to 'split' (string expected, got %s)", str and type(str) or "no value"))
local fields = {}
local pattern = format("([^%s]+)", delimiter)
str = type(str) == "number" and tostring(str) or str str = type(str) == "number" and tostring(str) or str
gsub(str, pattern, function(c) fields[getn(fields) + 1] = c end)
local fields = {}
gsub(str, format("([^%s]+)", delimiter), function(c) fields[getn(fields) + 1] = c end)
return unpack(fields) return unpack(fields)
end end
@@ -124,7 +123,7 @@ strsplit = string.split
function string.trim(str, chars) function string.trim(str, chars)
assert(type(str) == "string" or type(str) == "number", format("bad argument #1 to 'trim' (string expected, got %s)", str and type(str) or "no value")) assert(type(str) == "string" or type(str) == "number", format("bad argument #1 to 'trim' (string expected, got %s)", str and type(str) or "no value"))
assert(not type(chars) == "string" or type(chars) == "number", format("bad argument #2 to 'trim' (string expected, got %s)", chars and type(chars) or "no value")) assert(not chars or type(chars) == "string" or type(chars) == "number", format("bad argument #2 to 'trim' (string expected, got %s)", chars and type(chars) or "no value"))
str = type(str) == "number" and tostring(str) or str str = type(str) == "number" and tostring(str) or str
@@ -148,10 +147,8 @@ function string.trim(str, chars)
end end
end end
patternStart = "^["..pattern.."](.-)$" local patternStart = loadstring("return \"^["..pattern.."](.-)$\"")()
patternEnd = "^(.-)["..pattern.."]$" local patternEnd = loadstring("return \"^(.-)["..pattern.."]$\"")()
patternStart = loadstring("return \""..patternStart.."\"")()
patternEnd = loadstring("return \""..patternEnd.."\"")()
local trimed, x, y = 1 local trimed, x, y = 1
while trimed >= 1 do while trimed >= 1 do
@@ -163,7 +160,7 @@ function string.trim(str, chars)
return str return str
else else
-- remove leading/trailing [space][tab][return][newline] -- remove leading/trailing [space][tab][return][newline]
return string.gsub(str, "^%s*(.-)%s*$", "%1") return gsub(str, "^%s*(.-)%s*$", "%1")
end end
end end
strtrim = string.trim strtrim = string.trim