您的位置:首页 > 编程语言 > Lua

lua打印Table

2020-03-01 13:07 453 查看
function LogUtil.PrintTable(t,tableName)
print((tableName or "unknown").." = "..LogUtil.FormatTable(t))
end

function LogUtil.FormatTable(t, prefix, tableList)
prefix = prefix or "";
tableList = tableList or {};

if tableList[t] then
return "[ReFormat:"..tostring(t).."]";
end

tableList[t] = true;

local str = "{\n"

for k, v in pairs(t) do
str = str..LogUtil.FormatField(k, v, prefix.."\t", tableList).."\n";
end

str = str..prefix.."}";

return str;
end

function LogUtil.FormatField(key, value, prefix, tableList)
return prefix..LogUtil.FormatKey(key, prefix, tableList) .." = "..LogUtil.FormatValue(value, prefix, tableList)..";";
end

function LogUtil.FormatKey(key, prefix, tableList)
local keyType = type(key);
if keyType == "string" then
return key;
elseif keyType == "number" then
return "["..key.."]";
end

return "["..tostring(key).."]";
end

function LogUtil.FormatValue(value, prefix, tableList)
local valueType = type(value);
if valueType == "string" then
return "\""..value.."\"";
elseif valueType == "number" or valueType == "boolean" then
return tostring(value)
elseif valueType == "table" then
return LogUtil.FormatTable(value, prefix, tableList);
end

return "["..tostring(value).."]";
end
  • 点赞
  • 收藏
  • 分享
  • 文章举报
user_兴 发布了2 篇原创文章 · 获赞 0 · 访问量 94 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: