diff --git a/!Compatibility/api/luaAPI.lua b/!Compatibility/api/luaAPI.lua index d292d8c..924cbfb 100644 --- a/!Compatibility/api/luaAPI.lua +++ b/!Compatibility/api/luaAPI.lua @@ -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")) if type(n) == "string" then - if type(arg) == "table" then - return getn(arg) - else - return 1 - end + return getn(arg) end - local temp = {} + if n == 1 then + return unpack(arg) + end + + local args = {} for i = n, getn(arg) do - tinsert(temp, arg[i]) + args[i-n+1] = arg[i] end - return unpack(temp) + return unpack(args) end function math.modf(i) @@ -110,13 +110,12 @@ strmatch = string.match 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(str) == "string", format("bad argument #2 to 'split' (string expected, got %s)", str and type(str) or "no value")) - - local fields = {} - local pattern = format("([^%s]+)", delimiter) + 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")) 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) end @@ -124,7 +123,7 @@ strsplit = string.split 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(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 @@ -148,10 +147,8 @@ function string.trim(str, chars) end end - patternStart = "^["..pattern.."](.-)$" - patternEnd = "^(.-)["..pattern.."]$" - patternStart = loadstring("return \""..patternStart.."\"")() - patternEnd = loadstring("return \""..patternEnd.."\"")() + local patternStart = loadstring("return \"^["..pattern.."](.-)$\"")() + local patternEnd = loadstring("return \"^(.-)["..pattern.."]$\"")() local trimed, x, y = 1 while trimed >= 1 do @@ -163,7 +160,7 @@ function string.trim(str, chars) return str else -- remove leading/trailing [space][tab][return][newline] - return string.gsub(str, "^%s*(.-)%s*$", "%1") + return gsub(str, "^%s*(.-)%s*$", "%1") end end strtrim = string.trim