Lua打印Table的数据结构工具类

时间:2023-03-09 08:55:24
Lua打印Table的数据结构工具类
--这是quick中的工具,作用就是打印Lua中强大的table的结构, 当table的嵌套层级比较多的时候,这个工具非常方便,开发中必备的工具。
--具体使用方法:local debug = require("debug")
--       debug.dump(dataTable, "dataTable", 3) 接下来就可以在控制台看到输出的结果了,非常漂亮的树形结构
module(..., package.seeall) local g_Debug_Flag = true function log(msg)
if g_Debug_Flag == false then
return
end
cclog(msg)
end ---
--@function dump()
--@param value table 需要打印的
--@param description string 描述
--@param nesting int table嵌套层级
--@end function dump(value, description, nesting)
if g_Debug_Flag == false then
return
end --默认打印层级3
if type(nesting) ~= "number" then
nesting =
end local lookupTable = {}
local result = {} local function _v(v)
if type(v) == "string" then
v = "\"" .. v .. "\""
end
return tostring(v)
end local function _dump(value, description, indent, nest, keylen)
description = description or "<var>"
spc = ""
if type(keylen) == "number" then
spc = string.rep(" ",keylen - string.len(_v(description)))
end if type(value) ~= "table" then
result[#result + ] = string.format("%s%s%s = %s", indent, _v(description), spc, _v(value))
elseif lookupTable[value] then
result[#result + ] = string.format("%s%s%s = *REF*", indent, description, spc)
else
lookupTable[value] = true
if nest > nesting then
result[#result + ] = string.format("%s%s = *MAX NESTING*", indent, description)
else
result[#result + ] = string.format("%s%s = {" , indent, _v(description))
local indent2 = indent .. " "
local keys = {}
local keylen =
local values = {}
for k, v in pairs(value) do
keys[#keys + ] = k
local vk = _v(k)
local vk1 = string.len(vk)
if vk1 > keylen then
keylen = vk1
end
values[k] = v
end
table.sort(keys,function(a, b)
if type(a) == "number" and type(b) == "number" then
return a < b
else
return tostring(a) < tostring(b)
end
end) for i, k in pairs(keys) do
_dump(values[k], k, indent2,nest + ,keylen)
end
result[#result + ] = string.format("%s}", indent)
end
end
end
_dump(value,description, "- ", ) for i, line in pairs(result) do
print(line)
end end