implemented select and string.join functions

fix print function
This commit is contained in:
Pinya
2017-12-14 20:22:07 +03:00
parent 0c82c9a90f
commit 1a7557df22
+71 -35
View File
@@ -1,13 +1,51 @@
--Cache global variables --Cache global variables
local assert = assert
local error = error local error = error
local geterrorhandler = geterrorhandler local geterrorhandler = geterrorhandler
local pairs = pairs local pairs = pairs
local pcall = pcall local pcall = pcall
local securecall = securecall
local select = select
local tostring = tostring local tostring = tostring
local type = type local type = type
local unpack = unpack local unpack = unpack
local format = string.format
local getn, tinsert = table.getn, table.insert
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"))
if type(n) == "string" and n == "#" then
if type(arg) == "table" then
return getn(arg)
else
return 1
end
end
local temp = {}
for i = n, getn(arg) do
tinsert(temp, arg[i])
end
return unpack(temp)
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"))
local size = getn(arg)
if size == 0 then
return ""
end
local text = arg[1]
for i = 2, size do
text = text..delimiter..arg[i]
end
return text
end
strjoin = string.join
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"))
@@ -22,42 +60,40 @@ wipe = table.wipe
local LOCAL_ToStringAllTemp = {} local LOCAL_ToStringAllTemp = {}
function tostringall(...) function tostringall(...)
local n = select('#', ...) local n = getn(arg)
-- Simple versions for common argument counts -- Simple versions for common argument counts
if (n == 1) then if (n == 1) then
return tostring(...) return tostring(arg[1])
elseif (n == 2) then elseif (n == 2) then
local a, b = ... return tostring(arg[1]), tostring(arg[2])
return tostring(a), tostring(b) elseif (n == 3) then
elseif (n == 3) then return tostring(arg[1]), tostring(arg[2]), tostring(arg[3])
local a, b, c = ... elseif (n == 0) then
return tostring(a), tostring(b), tostring(c) return
elseif (n == 0) then end
return
end
local needfix local needfix
for i = 1, n do for i = 1, n do
local v = select(i, ...) local v = arg[i]
if (type(v) ~= "string") then if (type(v) ~= "string") then
needfix = i needfix = i
break break
end end
end end
if (not needfix) then return ... end if (not needfix) then return unpack(arg) end
wipe(LOCAL_ToStringAllTemp) wipe(LOCAL_ToStringAllTemp)
for i = 1, needfix - 1 do for i = 1, needfix - 1 do
LOCAL_ToStringAllTemp[i] = select(i, ...) LOCAL_ToStringAllTemp[i] = arg[i]
end end
for i = needfix, n do for i = needfix, n do
LOCAL_ToStringAllTemp[i] = tostring(select(i, ...)) LOCAL_ToStringAllTemp[i] = tostring(arg[i])
end end
return unpack(LOCAL_ToStringAllTemp) return unpack(LOCAL_ToStringAllTemp)
end end
local LOCAL_PrintHandler = function(...) local LOCAL_PrintHandler = function(...)
DEFAULT_CHAT_FRAME:AddMessage(strjoin(" ", tostringall(...))) DEFAULT_CHAT_FRAME:AddMessage(strjoin(" ", tostringall(unpack(arg))))
end end
function setprinthandler(func) function setprinthandler(func)
@@ -71,7 +107,7 @@ end
function getprinthandler() return LOCAL_PrintHandler end function getprinthandler() return LOCAL_PrintHandler end
local function print_inner(...) local function print_inner(...)
local ok, err = pcall(LOCAL_PrintHandler, ...) local ok, err = pcall(LOCAL_PrintHandler, unpack(arg))
if (not ok) then if (not ok) then
local func = geterrorhandler() local func = geterrorhandler()
func(err) func(err)
@@ -79,7 +115,7 @@ local function print_inner(...)
end end
function print(...) function print(...)
securecall(pcall, print_inner, ...) pcall(print_inner, unpack(arg))
end end
SLASH_PRINT1 = "/print" SLASH_PRINT1 = "/print"