日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

lua教程

Lua 如何輸出樹狀結構的table?

閱讀 (2337)

為了讓游戲前端數據輸出更加條理,做了一個簡單樹狀結構來打印數據。

ccmlog.lua

local function __tostring(value, indent, vmap)
    local str = ''
    indent = indent or ''
    vmap = vmap or {}

    --遞歸結束條件
    if (type(value) ~= 'table') then
        if (type(value) == 'string') then
            --字符串
            str = string.format("[[%s]]", value)
        else
            --整數
            str = tostring(value)
        end
    else
        if type(vmap) == 'table' then
            if vmap[value] then return '('..tostring(value)..')' end
            vmap[value] = true
        end

        local auxTable = {}     --保存元表KEY(非整數)
        local iauxTable = {}    --保存元表value
        local iiauxTable = {}   --保存數組(key為0)
        table.foreach(value, function(i, v)
            if type(i) == 'number' then
                if i == 0 then
                    table.insert(iiauxTable, i)
                else
                    table.insert(iauxTable, i)
                end
            elseif type(i) ~= 'table' then
                table.insert(auxTable, i)
            end
        end)
        table.sort(iauxTable)

        str = str..'{\n'
        local separator = ""
        local entry = "\n"
        local barray = true
        local kk,vv
        table.foreachi (iauxTable, function (i, k)
            if i == k and barray then
                entry = __tostring(value[k], indent..'  \t', vmap)
                str = str..separator..indent..'  \t'..entry
                separator = ", \n"
            else
                barray = false
                table.insert(iiauxTable, k)
            end
        end)
        table.sort(iiauxTable)

        table.foreachi (iiauxTable, function (i, fieldName)

            kk = tostring(fieldName)
            if type(fieldName) == "number" then 
                kk = '['..kk.."]"
            end 
            entry = kk .. " = " .. __tostring(value[fieldName],indent..'  \t',vmap)

            str = str..separator..indent..'  \t'..entry
            separator = ", \n"
        end)
        table.sort(auxTable)

        table.foreachi (auxTable, function (i, fieldName)

            kk = tostring(fieldName)
            if type(fieldName) == "number" then 
                kk = '['..kk.."]"
            end 
            vv = value[fieldName]
            entry = kk .. " = " .. __tostring(value[fieldName],indent..'  \t',vmap)

            str = str..separator..indent..'  \t'..entry
            separator = ", \n"
        end)

        str = str..'\n'..indent..'}'
    end

    return str
end

ccmlog = function(m,fmt,...)
    local args = {...}
    for k,arg in ipairs(args) do
        if type(arg) == 'table' 
            or type(arg) == 'boolean' 
            or type(arg) == 'function' 
            or type(arg) == 'userdata' then
            args[k] = __tostring(arg)
        end
    end

    args[#args+1] = "nil"
    args[#args+1] = "nil"
    args[#args+1] = "nil"
    local str = string.format("[%s]:"..fmt.." %s", m, unpack(args))
    print(str)

    local off = 1
    local p = CCLOGWARN 
    if m == 'error' then 
        p = CCLOGERROR 
    elseif m == 'warn' then 
        p = CCLOGWARN
    end
    while off <= #str do 
        local subStr = string.sub(str, off, off+1024)
        off = off + #subStr
        --p(subStr)
    end
end

--打印測試
reserved =  { [100] = { 300, 400}, 200, { 300, 500}, abc = "abc",[0] = {1,2,3,"abc"}}

ccmlog("d","d",reserved)

打印效果如下:

關閉
程序員人生
主站蜘蛛池模板: 久久久久无码国产精品一区 | 一区二区久久久 | 精品久久久一区二区 | 男女av| a√天堂中文字幕 | 亚洲第一黄色网 | 亚洲精品综合网 | 欧美日韩在线不卡 | 日韩av电影网站 | 牲高潮99爽久久久久777 | 国产精品国产三级国产aⅴ浪潮 | 欧美国产在线视频 | 久草成人网 | 91短视频在线 | 国产真实精品久久二三区 | 欧美综合国产 | 免费看成人吃奶视频在线 | 国产精品亚洲成在人线 | 久久久久成人精品 | 看亚洲a级一级毛片 | 日韩性xxx | 免费av一区二区三区 | 91香蕉视频好色先生 | 久久精品网| 天天操天天射天天爽 | 日本免费看 | 日韩欧美在线播放视频 | 日韩综合| 国产伦精品一区二区三区高清版 | 在线日韩 | 91精品国产91久久综合桃花 | 日韩a级毛片免费观看久久 精品一区二区在线播放 | 美女网站色 | 91蝌蚪色| 精品国产一二区 | 男女网站在线观看 | 亚洲精品一区二区三区 | 国产一区中文字幕 | 日韩不卡在线视频 | 免费日韩一区二区 | 美日韩精品 |