Giter VIP home page Giter VIP logo

Comments (2)

cls1991 avatar cls1991 commented on July 28, 2024

@cpejlgc
可以参考云风的print_r函数(没有循环引用问题), 自己做一下修改, 代码见http://blog.codingnow.com/cloud/LuaPrintR

from metoo.

zhouyanlt avatar zhouyanlt commented on July 28, 2024

这个问题我也遇到了,这里贡献下我修改后的版本,可以解决循环引用的BUG:


-- @brief: table 格式化为字符串
-- @param: obj:table, 要 dump 的表
-- @param: maxLev:number, 打印的最大层级
-- @return: string, 返回格式化后的字符串
local function dumptable(obj, maxLev)
    maxLev = maxLev or 10
    --[[
    -- 触发元表
    local mt = getmetatable(obj)
    if mt and mt.__tostring then
        return mt.__tostring(obj)
    end]]

    local used = {}
    local getIndent, quoteStr, wrapKey, wrapVal, dumpObj
    getIndent = function(level)
        return string.rep("\t", level)
    end
    quoteStr = function(str)
        return '"' .. string.gsub(str, '"', '\\"') .. '"'
    end
    wrapKey = function(val)
        if type(val) == "number" then
            return "[" .. val .. "]"
        elseif type(val) == "string" then
            return "[" .. quoteStr(val) .. "]"
        else
            return "[" .. tostring(val) .. "]"
        end
    end
    wrapVal = function(val, level)
        if type(val) == "table" then
            return dumpObj(val, level)
        elseif type(val) == "number" then
            return val
        elseif type(val) == "string" then
            return quoteStr(val)
        else
            return tostring(val)
        end
    end
    dumpObj = function(obj, level)
        if type(obj) ~= "table" then
            return wrapVal(obj)
        end

        if used[obj] then
            return "*REF*" -- 重复引用
        end

        -- local objTostring = rawget(obj, "tostring")
        -- if objTostring then
        --     return objTostring
        -- end

        level = level + 1

        if level >= maxLev then
            return "*MAX LEVEL*"
        end

        used[obj] = true

        local tokens = {}
        tokens[#tokens + 1] = "{"
        for k, v in pairs(obj) do
            tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. ","
        end
        tokens[#tokens + 1] = getIndent(level - 1) .. "}"
        return table.concat(tokens, "\n")
    end
    return dumpObj(obj, 0)
end

from metoo.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.