vlatkovski

tableToString

Jun 7th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1.  
  2. -- By Sukinahito
  3. -- Slightly modified by Vlatkovski for his own reasons
  4. -- Original: https://github.com/Sukinahito/SH-Snack-Break/blob/master/2015/4_May31_TableToString_Solution.lua
  5.  
  6.  
  7. function tableToString(t)
  8.     if type(t) == "table" then
  9.         local function parse(object)
  10.             local objectType = type(object)
  11.             return
  12.             (objectType == "table") and tableToString(object)                       or
  13.             (objectType == "function" or objectType == "userdata") and objectType   or
  14.             (objectType == "string") and table.concat({"\"", object, "\""})         or
  15.             (object)
  16.         end
  17.    
  18.         local isArrayKey        = {}
  19.         local tableOutput       = {}
  20.         local tableOutputCount  = 0
  21.    
  22.         for key, value in ipairs(t) do
  23.             isArrayKey[key]                     = true
  24.             tableOutput[tableOutputCount + 1]   = parse(value)
  25.             tableOutput[tableOutputCount + 2]   = ", "
  26.             tableOutputCount                    = tableOutputCount + 2
  27.         end
  28.    
  29.         for key, value in next, t do
  30.             if not isArrayKey[key] then
  31.                 tableOutput[tableOutputCount + 1]   = "["
  32.                 tableOutput[tableOutputCount + 2]   = parse(key)
  33.                 tableOutput[tableOutputCount + 3]   = "] = "
  34.                 tableOutput[tableOutputCount + 4]   = parse(value)
  35.                 tableOutput[tableOutputCount + 5]   = ", "
  36.                 tableOutputCount                    = tableOutputCount + 5
  37.             end
  38.         end
  39.    
  40.         tableOutput[tableOutputCount] = nil
  41.    
  42.         local metatable = getmetatable(t)
  43.         if metatable and tableToString(metatable) then
  44.             return table.concat({"{", table.concat(tableOutput), "} <- ", tableToString(metatable)})
  45.         end
  46.         return table.concat({"{", table.concat(tableOutput), "}", })
  47.     end
  48. end
  49.  
  50.  
  51. --------------------------------------------------------------------
  52. --------------------------------------------------------------------
  53.  
  54.  
  55. local examples = {
  56.     {1, 2, 3}, -- list
  57.     {a = 1, b = 2, c = 3}, -- dictionary
  58.     {a = 0, 1, 2, 3}, -- mixed
  59.     {function() end, newproxy and newproxy(), "string"}, -- functions, userdata, and strings
  60.     setmetatable({a = 0}, {b = 1})
  61. }
  62.  
  63. for _, example in next, examples do
  64.     if type(example) == "table" then
  65.         print(tableToString(example))
  66.     end
  67. end
  68.  
  69. for _, value in next, getfenv() do
  70.     if type(value) == "table" then
  71.         print(tableToString(value))
  72.     end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment