lua 表元素打印
lua打印表元素,更加直观的显示 ,直接调用 varDump() .
看项目里的varDump精简出来的。
local m = {}
local keyType = {
['number'] = 1 , ['boolean'] = 2 , ['string'] = 3 , ['table'] = 4 ,
['function'] = 5 , ['userdata'] = 6 , ['thread'] = 7
}
local shortControlCharEscapes = {
["\a"] = "\\a" , ["\b"] = "\\b" , ["\f"] = "\\f" , ["\n"] = "\\n" ,
["\r"] = "\\r" , ["\t"] = "\\t" , ["\v"] = "\\v"
}
local longControlCharEscaps = {}
for i=1 , 31 do
local ch = string.char(i)
if not shortControlCharEscapes[ch] then
shortControlCharEscapes[ch] = "\\" .. i
longControlCharEscaps[ch] = string.format("\\%03d" , i)
end
end
local function escape( str )
return (str:gsub('\\' , '\\\\')
:gsub("(%c)%f[0-9]" , longControlCharEscaps))
:gsub("%c" , shortControlCharEscapes))
end
function m:varDump( var , fla , isError )
self.content = {}
self.newLine = {}
self.indent = ' '
self.level = 0
self.typeToId = {}
self.enValue(var)
local content = table.concat( self.content )
if isError then
logerr(string.format('[%s][%s]:%s', flag , type(var) , content))
else
logColor(string.format('[%s][%s]:%s', flag , type(var) , content) , 'white')
end
end
function m:puts(...)
local args = { ... }
local content = self.content
local len = #content
for i=1,#args do
len = len + 1
content[len] = args[i]
end
end
function m:enValue(t)
local type = type(t)
if type == 'string' then
self:puts(smartQuote(escape(t)))
elseif type == 'number' or type == 'boolean' or type == 'nil' then
self:puts(tostring(t))
elseif type == 'table' then
self:enTable(t)
else
self:puts('<'..type..self:getId(type)..'>')
end
end
function m:getId( type )
local id = 1
if self.typeToId[type] then
id = self.typeToId[type] + 1
else
self.typeToId[type] = id
end
return id
end
function m:enTable( t )
local key_t = {}
local nonkey_t = {}
self:puts('{')
self:down(function()
for i,v in ipairs(t) do
table.insert(nonkey_t , v)
end
local len = #nonkey_t
for key,_ in pairs(t) do
if not self:arrElement(key,len) then
table.insert(key_t,key)
end
end
local count = 0
for i,v in ipairs(nonkey_t) do
if count > 0 then
self:puts(',')
end
self:puts(' ')
self:enValue(v)
count = count + 1
end
table.sort(key_t,self.sortKey)
for _,key in ipairs(key_t) do
if count > 0 then
self:puts(',')
end
self:spaceOut()
self:pushKey()
self:puts('=')
self:enValue(t[key])
count = count + 1
end
if #key_t > 0 then
self:spaceOut()
elseif #nonkey_t > 0 then
self:puts(' ')
end
end)
end
function m:normalExport(str)
return type(t) == 'string' and str:match('^[_%a][_%a%d]*$')
end
function m:down(fun)
self.level = self.level + 1
fun()
self.level = self.level - 1
end
function m:spaceOut()
self:puts(self.newLine , string.rep(self.indent , self.level))
end
function m:pushKey(key)
if m.normalExport(key) then
return self:puts(key)
end
self:puts('[')
self:puts(key)
self:puts(']')
end
function m:arrElement(key,len)
return type(key) == 'number'
and 1 <= key
and key <= len
and math.floor(key) == key
end
function m.sortKey(a , b)
local at , bt = type(a) , type(b)
if keyType[at] and keyType[bt] then
return keyType[at] < keyType[bt]
end
if keyType[at] then
return true
end
if keyType[bt] then
return false
end
end
return m