lua 表元素打印

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值