apply @Loaal luaAPI updates

This commit is contained in:
Crum
2017-12-17 15:06:40 -06:00
parent db3af6a2b1
commit 6c5f5b47fa
+68 -22
View File
@@ -2,17 +2,48 @@
local assert = assert local assert = assert
local error = error local error = error
local geterrorhandler = geterrorhandler local geterrorhandler = geterrorhandler
local loadstring = loadstring
local pairs = pairs local pairs = pairs
local pcall = pcall local pcall = pcall
local tostring = tostring local tostring = tostring
local type = type local type = type
local unpack = unpack local unpack = unpack
local ceil, floor = math.ceil, math.floor local ceil, floor = math.ceil, math.floor
local find, format, gsub, sub = string.find, string.format, string.gsub, string.sub local find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub
local getn, tinsert = table.getn, table.insert local getn, setn, tinsert = table.getn, table.setn, table.insert
local escapeSequences = {
["\a"] = "\\a", -- Bell
["\b"] = "\\b", -- Backspace
["\t"] = "\\t", -- Horizontal tab
["\n"] = "\\n", -- Newline
["\v"] = "\\v", -- Vertical tab
["\f"] = "\\f", -- Form feed
["\r"] = "\\r", -- Carriage return
["\\"] = "\\\\", -- Backslash
["\""] = "\\\"", -- Quotation mark
["|"] = "||",
[" "] = "%s",
[" "] = "%s",
["!"] = "\\!",
["#"] = "\\#",
["$"] = "\\$",
["%"] = "\\%",
["&"] = "\\&",
["'"] = "\\'",
["("] = "\\(",
[")"] = "\\)",
["*"] = "\\*",
["+"] = "\\+",
[","] = "\\,",
["-"] = "\\-",
["."] = "\\.",
["/"] = "\\/"
}
math.huge = 1/0 math.huge = 1/0
string.gmatch = string.gfind string.gmatch = gfind
function difftime(time2, time1) 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(type(time2) == "number", format("bad argument #1 to 'difftime' (number expected, got %s)", time2 and type(time2) or "no value"))
@@ -68,12 +99,12 @@ strjoin = string.join
function string.match(str, pattern, index) 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(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")) assert(type(pattern) == "string" or type(pattern) == "number", 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 str = type(str) == "number" and tostring(str) or str
local i, j = find(index and sub(str, index) or str, pattern) pattern = type(pattern) == "number" and tostring(pattern) or pattern
return i and sub(str, i, j) return gfind(index and sub(str, index) or str, pattern)()
end end
strmatch = string.match strmatch = string.match
@@ -92,37 +123,48 @@ end
strsplit = string.split 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)", delimiter and type(delimiter) 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"))
str = type(str) == "number" and tostring(str) or str str = type(str) == "number" and tostring(str) or str
--[[
if chars then if chars then
local size = string.len(chars) chars = type(chars) == "number" and tostring(chars) or chars
local token = ""
local tokens = {}
for token in gfind(chars, "[%z\1-\255]") do
tinsert(tokens, token)
end
local pattern = ""
local size = getn(tokens)
for i = 1, size do for i = 1, size do
token = token .. string.sub(chars, i, i + 1) .. "*" pattern = pattern..(escapeSequences[tokens[i]] or tokens[i]).."+"
if size > 1 and i < size then if size > 1 and i < size then
token = token.."|" pattern = pattern.."|"
end end
end end
token = "["..token.."]" patternStart = "^["..pattern.."](.-)$"
patternEnd = "^(.-)["..pattern.."]$"
patternStart = loadstring("return \""..patternStart.."\"")()
patternEnd = loadstring("return \""..patternEnd.."\"")()
local trimed = 1 local trimed, x, y = 1
while trimed == 1 do while trimed >= 1 do
str, trimed = string.gsub(str, "^"..token.."(._)"..token.."$", "") str, x = gsub(str, patternStart, "%1")
str, y = gsub(str, patternEnd, "%1")
trimed = x + y
end end
return str
else else
-- remove leading/trailing [space][tab][return][newline] -- remove leading/trailing [space][tab][return][newline]
str = string.gsub(str, "^%s*(.-)%s*$", "%1") return string.gsub(str, "^%s*(.-)%s*$", "%1")
end end
return str
]]
return string.gsub(str, "^%s*(.-)%s*$", "%1")
end end
strtrim = string.trim strtrim = string.trim
@@ -133,6 +175,10 @@ function table.wipe(t)
t[k] = nil t[k] = nil
end end
if getn(t) ~= 0 then
setn(t, 0)
end
return t return t
end end
wipe = table.wipe wipe = table.wipe