From 6c5f5b47fa2ebe9986555cb185f559f83c4875ad Mon Sep 17 00:00:00 2001 From: Crum Date: Sun, 17 Dec 2017 15:06:40 -0600 Subject: [PATCH] apply @Loaal luaAPI updates --- 2/3/4/5/6/7/!Compatibility/api/luaAPI.lua | 90 +++++++++++++++++------ 1 file changed, 68 insertions(+), 22 deletions(-) diff --git a/2/3/4/5/6/7/!Compatibility/api/luaAPI.lua b/2/3/4/5/6/7/!Compatibility/api/luaAPI.lua index 7a5284a..d292d8c 100644 --- a/2/3/4/5/6/7/!Compatibility/api/luaAPI.lua +++ b/2/3/4/5/6/7/!Compatibility/api/luaAPI.lua @@ -2,17 +2,48 @@ local assert = assert local error = error local geterrorhandler = geterrorhandler +local loadstring = loadstring local pairs = pairs local pcall = pcall local tostring = tostring local type = type local unpack = unpack 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 find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub +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 -string.gmatch = string.gfind +string.gmatch = 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")) @@ -68,12 +99,12 @@ 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")) + 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 - 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 strmatch = string.match @@ -92,37 +123,48 @@ end 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)", 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 ---[[ + if chars then - local size = string.len(chars) - local token = "" + chars = type(chars) == "number" and tostring(chars) or chars + + 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 - token = token .. string.sub(chars, i, i + 1) .. "*" + pattern = pattern..(escapeSequences[tokens[i]] or tokens[i]).."+" if size > 1 and i < size then - token = token.."|" + pattern = pattern.."|" end end - token = "["..token.."]" + patternStart = "^["..pattern.."](.-)$" + patternEnd = "^(.-)["..pattern.."]$" + patternStart = loadstring("return \""..patternStart.."\"")() + patternEnd = loadstring("return \""..patternEnd.."\"")() - local trimed = 1 - while trimed == 1 do - str, trimed = string.gsub(str, "^"..token.."(._)"..token.."$", "") + local trimed, x, y = 1 + while trimed >= 1 do + str, x = gsub(str, patternStart, "%1") + str, y = gsub(str, patternEnd, "%1") + trimed = x + y end + + return str else -- remove leading/trailing [space][tab][return][newline] - str = string.gsub(str, "^%s*(.-)%s*$", "%1") + return string.gsub(str, "^%s*(.-)%s*$", "%1") end - - return str -]] - - return string.gsub(str, "^%s*(.-)%s*$", "%1") end strtrim = string.trim @@ -133,6 +175,10 @@ function table.wipe(t) t[k] = nil end + if getn(t) ~= 0 then + setn(t, 0) + end + return t end wipe = table.wipe