Lua打印table的工具函数dump

在 Lua 中,print 是不能直接打印出 table 里面的数据的,如果用循环遍历打印遇到嵌套 table 的话也是很不友好的!

local tblTest = {1, a = {111, 222}, "ccc"}

print(tblTest)
-- table: 00A17E20

for k, v in pairs(tblTest) do
    print(k, v)
end
--[[
1	1
2	ccc
a	table: 00A17C40
]]

在 Cocos2d-x 的工具函数集 functions.lua 中,提供了一个打印 table 的工具函数 dump,记录如下:

function dump_value_(v)
    if type(v) == "string" then
        v = "\"" .. v .. "\""
    end
    return tostring(v)
end

function split(input, delimiter)
    input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter == "") then return false end
    local pos, arr = 0, {}
    for st, sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

function trim(input)
    return (string.gsub(input, "^%s*(.-)%s*$", "%1"))
end

--[[
打印table的工具函数
@params value 需要打印的内容
@params desciption 描述
@params nesting 打印内容的嵌套级数,默认3级
]]
function dump(value, desciption, nesting)
    if type(nesting) ~= "number" then nesting = 3 end

    local lookupTable = {}
    local result = {}

    local traceback = split(debug.traceback("", 2), "\n")
    -- print("dump from: " .. trim(traceback[3]))

    local function dump_(value, desciption, indent, nest, keylen)
        desciption = desciption or "<var>"
        local spc = ""
        if type(keylen) == "number" then
            spc = string.rep(" ", keylen - string.len(dump_value_(desciption)))
        end
        if type(value) ~= "table" then
            result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(desciption), spc, dump_value_(value))
        elseif lookupTable[tostring(value)] then
            result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(desciption), spc)
        else
            lookupTable[tostring(value)] = true
            if nest > nesting then
                result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(desciption))
            else
                result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(desciption))
                local indent2 = indent.."    "
                local keys = {}
                local keylen = 0
                local values = {}
                for k, v in pairs(value) do
                    keys[#keys + 1] = k
                    local vk = dump_value_(k)
                    local vkl = string.len(vk)
                    if vkl > keylen then keylen = vkl 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 ipairs(keys) do
                    dump_(values[k], k, indent2, nest + 1, keylen)
                end
                result[#result +1] = string.format("%s}", indent)
            end
        end
    end
    dump_(value, desciption, "- ", 1)

    for i, line in ipairs(result) do
        print(line)
    end
end

test:

-- string test
local strTest = "hello world!"
dump(strTest)
-- - "<var>" = "hello world!"

-- table test
local tblTest = {a = {one = 111, two = 222}, b = "666", c = {{id = 1, num = 123}, {id = 2, num = 456}}}
dump(tblTest, "table print->")
--[[
- "table print->" = {
-     "a" = {
-         "one" = 111
-         "two" = 222
-     }
-     "b" = "666"
-     "c" = {
-         1 = {
-             "id"  = 1
-             "num" = 123
-         }
-         2 = {
-             "id"  = 2
-             "num" = 456
-         }
-     }
- }
]]

tblTest = {{nesting1 = 1}, {nesting2 = 2, {nesting3 = 3, {nesting4 = 4, {nesting = 5}}}}}
dump(tblTest, "nesting test->")  -- 默认最多只打印3级
--[[
- "nesting test->" = {
-     1 = {
-         "nesting1" = 1
-     }
-     2 = {
-         1 = {
-             1 = *MAX NESTING*
-             "nesting3" = 3
-         }
-         "nesting2" = 2
-     }
- }
]]
dump(tblTest, "nesting test->", 5)
--[[
- "nesting test->" = {
-     1 = {
-         "nesting1" = 1
-     }
-     2 = {
-         1 = {
-             1 = {
-                 1 = {
-                     "nesting" = 5
-                 }
-                 "nesting4" = 4
-             }
-             "nesting3" = 3
-         }
-         "nesting2" = 2
-     }
- }
]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fightsyj

您的鼓励将是我分享的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值