(转)lua dump函数

[cpp]  view plain copy
 
  1. function dump(value, desciption, nesting)    
  2.     if type(nesting) ~= "number" then nesting = 3 end    
  3.     
  4.     local lookupTable = {}    
  5.     local result = {}    
  6.     
  7.     local function _v(v)    
  8.         if type(v) == "string" then    
  9.             v = "\"" .. v .. "\""    
  10.         end    
  11.         return tostring(v)    
  12.     end    
  13.     
  14.     local traceback = string.split(debug.traceback("", 2), "\n")    
  15.     print("dump from: " .. string.trim(traceback[3]))    
  16.     
  17.     local function _dump(value, desciption, indent, nest, keylen)    
  18.         desciption = desciption or "<var>"    
  19.         spc = ""    
  20.         if type(keylen) == "number" then    
  21.             spc = string.rep(" ", keylen - string.len(_v(desciption)))    
  22.         end    
  23.         if type(value) ~= "table" then    
  24.             result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(desciption), spc, _v(value))    
  25.         elseif lookupTable[value] then    
  26.             result[#result +1 ] = string.format("%s%s%s = *REF*", indent, desciption, spc)    
  27.         else    
  28.             lookupTable[value] = true    
  29.             if nest > nesting then    
  30.                 result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, desciption)    
  31.             else    
  32.                 result[#result +1 ] = string.format("%s%s = {", indent, _v(desciption))    
  33.                 local indent2 = indent.."    "    
  34.                 local keys = {}    
  35.                 local keylen = 0    
  36.                 local values = {}    
  37.                 for k, v in pairs(value) do    
  38.                     keys[#keys + 1] = k    
  39.                     local vk = _v(k)    
  40.                     local vkl = string.len(vk)    
  41.                     if vkl > keylen then keylen = vkl end    
  42.                     values[k] = v    
  43.                 end    
  44.                 table.sort(keys, function(a, b)    
  45.                     if type(a) == "number" and type(b) == "number" then    
  46.                         return a < b    
  47.                     else    
  48.                         return tostring(a) < tostring(b)    
  49.                     end    
  50.                 end)    
  51.                 for i, k in ipairs(keys) do    
  52.                     _dump(values[k], k, indent2, nest + 1, keylen)    
  53.                 end    
  54.                 result[#result +1] = string.format("%s}", indent)    
  55.             end    
  56.         end    
  57.     end    
  58.     _dump(value, desciption, "- ", 1)    
  59.     
  60.     for i, line in ipairs(result) do    
  61.         print(line)    
  62.     end    
  63. end    


用法:dump(t)  --t为打印的内容

原文地址:https://blog.csdn.net/u013517637/article/details/53977732

猜你喜欢

转载自www.cnblogs.com/wodehao0808/p/9133733.html