print_r.lua 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. local print = print
  2. local tconcat = table.concat
  3. local tinsert = table.insert
  4. local srep = string.rep
  5. local type = type
  6. local pairs = pairs
  7. local tostring = _tostring or tostring
  8. local next = next
  9. function print_rs(root)
  10. if type(root) ~= "table" then
  11. return "is " .. type(root) .. " not table"
  12. end
  13. local cache = { [root] = "." }
  14. local function _dump(t,space,name)
  15. local temp = {}
  16. for k,v in pairs(t) do
  17. local key = tostring(k)
  18. if cache[v] then
  19. tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
  20. elseif type(v) == "table" then
  21. local new_key = name .. "." .. key
  22. cache[v] = new_key
  23. tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
  24. else
  25. tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
  26. end
  27. end
  28. return tconcat(temp,"\n"..space)
  29. end
  30. return _dump(root, "","")
  31. end
  32. function print_r(root)
  33. print(print_rs(root))
  34. end
  35. return print_r