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
local assert = assert
local error = error
local geterrorhandler = geterrorhandler
local pairs = pairs
local pcall = pcall
local securecall = securecall
local select = select
local tostring = tostring
local type = type
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)
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 = {}
function tostringall(...)
local n = select('#', ...)
-- Simple versions for common argument counts
if (n == 1) then
return tostring(...)
elseif (n == 2) then
local a, b = ...
return tostring(a), tostring(b)
elseif (n == 3) then
local a, b, c = ...
return tostring(a), tostring(b), tostring(c)
elseif (n == 0) then
return
end
local n = getn(arg)
-- Simple versions for common argument counts
if (n == 1) then
return tostring(arg[1])
elseif (n == 2) then
return tostring(arg[1]), tostring(arg[2])
elseif (n == 3) then
return tostring(arg[1]), tostring(arg[2]), tostring(arg[3])
elseif (n == 0) then
return
end
local needfix
for i = 1, n do
local v = select(i, ...)
if (type(v) ~= "string") then
needfix = i
break
end
end
if (not needfix) then return ... end
local needfix
for i = 1, n do
local v = arg[i]
if (type(v) ~= "string") then
needfix = i
break
end
end
if (not needfix) then return unpack(arg) end
wipe(LOCAL_ToStringAllTemp)
for i = 1, needfix - 1 do
LOCAL_ToStringAllTemp[i] = select(i, ...)
end
for i = needfix, n do
LOCAL_ToStringAllTemp[i] = tostring(select(i, ...))
end
return unpack(LOCAL_ToStringAllTemp)
wipe(LOCAL_ToStringAllTemp)
for i = 1, needfix - 1 do
LOCAL_ToStringAllTemp[i] = arg[i]
end
for i = needfix, n do
LOCAL_ToStringAllTemp[i] = tostring(arg[i])
end
return unpack(LOCAL_ToStringAllTemp)
end
local LOCAL_PrintHandler = function(...)
DEFAULT_CHAT_FRAME:AddMessage(strjoin(" ", tostringall(...)))
DEFAULT_CHAT_FRAME:AddMessage(strjoin(" ", tostringall(unpack(arg))))
end
function setprinthandler(func)
@@ -71,7 +107,7 @@ end
function getprinthandler() return LOCAL_PrintHandler end
local function print_inner(...)
local ok, err = pcall(LOCAL_PrintHandler, ...)
local ok, err = pcall(LOCAL_PrintHandler, unpack(arg))
if (not ok) then
local func = geterrorhandler()
func(err)
@@ -79,7 +115,7 @@ local function print_inner(...)
end
function print(...)
securecall(pcall, print_inner, ...)
pcall(print_inner, unpack(arg))
end
SLASH_PRINT1 = "/print"