Module:Sandbox/Nyoron/anyToString

local p = {}

local T = {} local T_table = {}

local function table_sort_f(a,b) if type(a) == 'nil' then return true end

if type(a) == 'boolean' then if type(b) == 'nil' then return false end if type(b) == 'boolean' then return tostring(a) < tostring(b) end return true end

if type(a) == 'number' then if type(b) == 'nil' then return false end if type(b) == 'boolean' then return false end if type(b) == 'number' then return a < b end return true end

if type(a) == 'string' then if type(b) == 'nil' then return false end if type(b) == 'boolean' then return false end if type(b) == 'number' then return false end if type(b) == 'string' then return a < b end return true end

if type(a) == 'function' then if type(b) == 'nil' then return false end if type(b) == 'boolean' then return false end if type(b) == 'number' then return false end if type(b) == 'string' then return false end if type(b) == 'function' then return T.func.name[a] < T.func.name[b] end return true end

if type(a) == 'table' then if type(b) == 'nil' then return false end if type(b) == 'boolean' then return false end if type(b) == 'number' then return false end if type(b) == 'string' then return false end if type(b) == 'function' then return false end if type(b) == 'table' then return T_table.name[a] < T_table.name[b] end return true end

return true end

local function __anyToString( var, indent, indent2 )

local tp = type( var ) if tp == 'table' then if T_table.name[var] == nil then T_table.num = T_table.num + 1 T_table.name[var] = 'table_' .. tostring( T_table.num ) end if T_table.done[var] then return T_table.name[var] end T_table.done[var] = true local sb = {} if not T_table.started then T_table.started = true sb = { string.rep( " ", indent ) } sb[#sb + 1] = T_table.name[var] .. ' = {\n' else sb[#sb + 1] = '{ -- ' .. T_table.name[var] .. '  --\n' end local array_keys = {} local table_keys = {} for key, value in ipairs( var ) do           array_keys[key] = true table_keys[#table_keys + 1] = key end for key in pairs( var ) do           if not array_keys[key] then table_keys[#table_keys + 1] = key if type( key ) == 'table' then if T_table.name[key] == nil then T_table.num = T_table.num + 1 T_table.name[key] = 'table_' .. tostring( T_table.num ) end end if type( key ) == 'function' then if T.func.name[key] == nil then T.func.num = T.func.num + 1 T.func.name[key] = 'function_' .. tostring( T.func.num ) end end end end if T_table.do_sort then table.sort( table_keys, table_sort_f ) end for i = 1, #table_keys do           local key = table_keys[i] sb[#sb + 1] = string.rep( " ", indent + indent2 ) if type( key ) == 'table' then sb[#sb + 1] = '[' .. T_table.name[key] .. '] = '           else sb[#sb + 1] = '[' .. __anyToString( key ) .. '] = '           end sb[#sb + 1] = __anyToString( var[key], indent + indent2, indent2 ) sb[#sb + 1] = "," if array_keys[key] then sb[#sb + 1] = '\t-- Array[' .. tostring( key ) .. ']'           elseif type(key) == 'number' then sb[#sb + 1] = '\t-- Table[' .. tostring( key ) .. ']'       	end sb[#sb + 1] = "\n" end sb[#sb + 1] = string.rep( " ", indent ) sb[#sb + 1] = "}" return table.concat( sb ) elseif tp == 'function' then if T.func.name[var] == nil then T.func.num = T.func.num + 1 T.func.name[var] = 'function_' .. tostring( T.func.num ) end return T.func.name[var] elseif tp == 'string' then return string.format( "%q", var ) else return tostring( var ) end end

p.anyToString = function( var, do_sort, indent, indent2 ) -- initialize T.func = {} T.func.name = {} T.func.num = 0 T_table.name = {} T_table.num = 0 T_table.started = false T_table.done = {} T_table.do_sort = do_sort indent = indent or 0 indent2 = indent2 or 4

return __anyToString( var, indent, indent2 ) end

return p