implemented replacement for difftime, math.huge, math.modf, string.gmatch, string.match, string.split

This commit is contained in:
Pinya
2017-12-14 20:26:07 +03:00
parent 1a7557df22
commit 01f5329d49
+45 -1
View File
@@ -7,9 +7,20 @@ local pcall = pcall
local tostring = tostring local tostring = tostring
local type = type local type = type
local unpack = unpack 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 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, ...) 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")) 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) return unpack(temp)
end 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, ...) 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")) 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 end
strjoin = string.join 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) 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")) assert(type(t) == "table", format("bad argument #1 to 'wipe' (table expected, got %s)", t and type(t) or "no value"))