fixed util.concat handling of nil parameters

This commit is contained in:
Mikayla Fischler
2023-11-12 16:06:16 -05:00
parent 76403b4ddc
commit 6e92097544

View File

@ -14,11 +14,15 @@ local print = print
local tostring = tostring local tostring = tostring
local type = type local type = type
local t_concat = table.concat
local t_insert = table.insert
local t_unpack = table.unpack
---@class util ---@class util
local util = {} local util = {}
-- scada-common version -- scada-common version
util.version = "1.1.8" util.version = "1.1.9"
util.TICK_TIME_S = 0.05 util.TICK_TIME_S = 0.05
util.TICK_TIME_MS = 50 util.TICK_TIME_MS = 50
@ -70,7 +74,7 @@ function util.strval(val)
if t == "string" then return val end if t == "string" then return val end
-- this depends on Lua short-circuiting the or check for metatables (note: metatables won't have metatables) -- this depends on Lua short-circuiting the or check for metatables (note: metatables won't have metatables)
if (t == "table" and (getmetatable(val) == nil or getmetatable(val).__tostring == nil)) or t == "function" then if (t == "table" and (getmetatable(val) == nil or getmetatable(val).__tostring == nil)) or t == "function" then
return table.concat{"[", tostring(val), "]"} return t_concat{"[", tostring(val), "]"}
else return tostring(val) end else return tostring(val) end
end end
@ -90,7 +94,7 @@ function util.pad(str, n)
local lpad = math.floor((n - len) / 2) local lpad = math.floor((n - len) / 2)
local rpad = (n - len) - lpad local rpad = (n - len) - lpad
return table.concat{util.spaces(lpad), str, util.spaces(rpad)} return t_concat{util.spaces(lpad), str, util.spaces(rpad)}
end end
-- wrap a string into a table of lines -- wrap a string into a table of lines
@ -109,8 +113,9 @@ function util.strwrap(str, limit) return cc_strings.wrap(str, limit) end
---@diagnostic disable-next-line: unused-vararg ---@diagnostic disable-next-line: unused-vararg
function util.concat(...) function util.concat(...)
local strings = {} local strings = {}
for i = 1, #arg do strings[i] = util.strval(arg[i]) end ---@diagnostic disable-next-line: undefined-field
return table.concat(strings) for i = 1, arg.n do strings[i] = util.strval(arg[i]) end
return t_concat(strings)
end end
-- alias -- alias
@ -121,7 +126,7 @@ util.c = util.concat
---@param format string ---@param format string
---@vararg any ---@vararg any
---@diagnostic disable-next-line: unused-vararg ---@diagnostic disable-next-line: unused-vararg
function util.sprintf(format, ...) return string.format(format, table.unpack(arg)) end function util.sprintf(format, ...) return string.format(format, t_unpack(arg)) end
-- luacheck: unused args -- luacheck: unused args
@ -185,7 +190,7 @@ function util.mov_avg(length, default)
---@param x number value ---@param x number value
function public.reset(x) function public.reset(x)
data = {} data = {}
for _ = 1, length do table.insert(data, x) end for _ = 1, length do t_insert(data, x) end
end end
-- record a new value -- record a new value