會(huì)PHP的人都知道PHP中的print_r/var_export函數(shù),可以方便的用于打印數(shù)組或?qū)С鲎兞?,Lua中沒(méi)有提供,實(shí)際應(yīng)用中卻是很多時(shí)候需要類(lèi)的功能。 今天便封裝了個(gè)簡(jiǎn)單函數(shù),實(shí)現(xiàn)類(lèi)功能,用來(lái)打印/導(dǎo)出table: -- dump.lua--[[dump對(duì)象@param mixed obj@return str
會(huì)php的人都知道php中的print_r/var_export函數(shù),可以方便的用于打印數(shù)組或?qū)С鲎兞?,lua中沒(méi)有提供,實(shí)際應(yīng)用中卻是很多時(shí)候需要類(lèi)似的功能。
今天便封裝了個(gè)簡(jiǎn)單函數(shù),實(shí)現(xiàn)類(lèi)似功能,用來(lái)打印/導(dǎo)出table:
-- dump.lua --[[ dump對(duì)象 @param mixed obj @return string ]] function debug.dump(obj) local getIndent, quoteStr, wrapKey, wrapVal, isArray, dumpObj getIndent = function(level) return string.rep(" ", level) end quoteStr = function(str) str = string.gsub(str, "[%c\"]", { [" "] = "\t", [" "] = "\r", [" "] = "\n", ["""] = "\"", ["\"] = "\\", }) return '"' .. 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 local isArray = function(arr) local count = 0 for k, v in pairs(arr) do count = count + 1 end for i = 1, count do if arr[i] == nil then return false end end return true, count end dumpObj = function(obj, level) if type(obj) ~= "table" then return wrapVal(obj) end level = level + 1 local tokens = {} tokens[#tokens + 1] = "{" local ret, count = isArray(obj) if ret then for i = 1, count do tokens[#tokens + 1] = getIndent(level) .. wrapVal(obj[i], level) .. "," end else for k, v in pairs(obj) do tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. "," end end tokens[#tokens + 1] = getIndent(level - 1) .. "}" return table.concat(tokens, " ") end return dumpObj(obj, 0) end
測(cè)試代碼:
-- test.lua local obj = { string1 = "Hi! My name is LiXianlin", string2 = "aa bb cc dd\ee"ff", string3 = "a\tb\rc\n\\ee"ff", int = 9527, float = 3.1415, bool = true, array = { 1, 2, 3, { a = 21, b = 22, c = 23, }, }, table = { x = 100, y = 200, w = 960, h = 640, }, [88] = 88888, [9.7] = 22222, } print(debug.dump(obj))
輸出結(jié)果:
{ ["string1"] = "Hi! My name is LiXianlin", [9.7] = 22222, ["table"] = { ["y"] = 200, ["x"] = 100, ["h"] = 640, ["w"] = 960, }, [88] = 88888, ["string2"] = "aa bb cc dd\ee"ff", ["int"] = 9527, ["string3"] = "a\tb\rc\n\\ee"ff", ["bool"] = true, ["array"] = { 1, 2, 3, { ["b"] = 22, ["a"] = 21, ["c"] = 23, }, }, ["float"] = 3.1415, }
函數(shù)特點(diǎn):
立即學(xué)習(xí)“PHP免費(fèi)學(xué)習(xí)筆記(深入)”;
1、理論上支持無(wú)限級(jí)table嵌套
2、格式化輸出,可讀性好
3、輸出結(jié)果可直接用于lua代碼
4、function、userdata、thread類(lèi)型tostring輸出
5、當(dāng)字符串中含有控制字符時(shí),可能有影響(代碼中僅對(duì) ,
,
等進(jìn)行了處理)
PHP怎么學(xué)習(xí)?PHP怎么入門(mén)?PHP在哪學(xué)?PHP怎么學(xué)才快?不用擔(dān)心,這里為大家提供了PHP速學(xué)教程(入門(mén)到精通),有需要的小伙伴保存下載就能學(xué)習(xí)啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)