diff --git a/!Compatibility/api/luaAPI.lua b/!Compatibility/api/luaAPI.lua index e04b4da..aee56b4 100644 --- a/!Compatibility/api/luaAPI.lua +++ b/!Compatibility/api/luaAPI.lua @@ -7,9 +7,20 @@ local pcall = pcall local tostring = tostring local type = type local unpack = unpack -local format = string.format +local ceil, floor = math.ceil, math.floor +local find, format, gsub, sub = string.find, string.format, string.gsub, string.sub local getn, tinsert = table.getn, table.insert +math.huge = 1/0 +string.gmatch = string.gfind + +function difftime(time2, time1) + assert(type(time2) == "number", format("bad argument #1 to 'difftime' (number expected, got %s)", time2 and type(time2) or "no value")) + assert(not time1 or type(time1) == "number", format("bad argument #2 to 'difftime' (number expected, got %s)", time1 and type(time1) or "no value")) + + return time1 and time2 - time1 or time2 +end + function select(n, ...) assert(type(n) == "number" or type(n) == "string", format("bad argument #1 to 'select' (number expected, got %s)", n and type(n) or "no value")) @@ -30,6 +41,14 @@ function select(n, ...) return unpack(temp) end +function math.modf(i) + assert(type(i) == "number", format("bad argument #1 to 'modf' (number expected, got %s)", i and type(i) or "no value")) + + local int = i >= 0 and floor(i) or ceil(i) + + return int, i - int +end + function string.join(delimiter, ...) assert(type(delimiter) == "string" or type(delimiter) == "number", format("bad argument #1 to 'join' (string expected, got %s)", delimiter and type(delimiter) or "no value")) @@ -47,6 +66,31 @@ function string.join(delimiter, ...) end strjoin = string.join +function string.match(str, pattern, index) + assert(type(str) == "string" or type(str) == "number", format("bad argument #1 to 'match' (string expected, got %s)", str and type(str) or "no value")) + assert(type(pattern) == "string", format("bad argument #2 to 'match' (string expected, got %s)", pattern and type(pattern) or "no value")) + + str = type(str) == "number" and tostring(str) or str + local _, _, match = find(index and sub(str, index) or str, "("..pattern..")") + + return match +end +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) + + str = type(str) == "number" and tostring(str) or str + gsub(str, pattern, function(c) fields[getn(fields) + 1] = c end) + + return unpack(fields) +end +strsplit = string.split + function table.wipe(t) assert(type(t) == "table", format("bad argument #1 to 'wipe' (table expected, got %s)", t and type(t) or "no value"))