comparison Utility.lua @ 62:619e4b9c3cd9

Stop recursion loops
author John@Yosemite-PC
date Mon, 26 Mar 2012 19:20:27 -0400
parents b3679847e292
children 790266dbcaff
comparison
equal deleted inserted replaced
61:9b1588bd4398 62:619e4b9c3cd9
1 local bsk=bsk 1 local bsk=bsk
2 local pairs=pairs 2 local pairs=pairs
3 local tostring=tostring 3 local tostring=tostring
4 local type=type 4 local type=type
5 local table=table
5 local getmetatable=getmetatable 6 local getmetatable=getmetatable
6 local setmetatable=setmetatable 7 local setmetatable=setmetatable
7 8
8 setfenv(1,bsk) 9 setfenv(1,bsk)
9 10
35 local u = { } 36 local u = { }
36 for k, v in pairs(t) do u[k] = v end 37 for k, v in pairs(t) do u[k] = v end
37 return setmetatable(u, getmetatable(t)) 38 return setmetatable(u, getmetatable(t))
38 end 39 end
39 40
40 function PrintTable(table, depth) 41 function PrintTable(tabl, depth, history)
41 depth = depth or "" 42 depth = depth or ""
42 if not table then return end 43 history = history or {}
44 if not tabl then return end
43 if #depth > 3*5 then print(depth.."Recursion too deep - stopping"); return end 45 if #depth > 3*5 then print(depth.."Recursion too deep - stopping"); return end
44 for i,v in pairs(table) do 46 for i,v in pairs(tabl) do
45 if( type(v) == "string" ) then 47 if( type(v) == "string" ) then
46 print(depth .. i .. " - " .. v) 48 print(depth .. i .. " - " .. v)
47 elseif( type(v) == "number" ) then 49 elseif( type(v) == "number" ) then
48 print(depth .. i .. " - " .. tostring(v)) 50 print(depth .. i .. " - " .. tostring(v))
49 elseif( type(v) == "table" ) then 51 elseif( type(v) == "table" ) then
50 --if v == table then return end 52 --if v == table then return end
51 print(depth .. tostring(i) .." - ") 53 print(depth .. tostring(i) .." - ")
52 PrintTable(v,depth.." ") 54 if history then
55 for i,vh in pairs(history) do
56 if vh==tabl then
57 print(depth.. " - circular reference detected; halting print")
58 return
59 end
60 end
61 end
62 table.insert(history,tabl)
63 PrintTable(v,depth.." ",history)
64 table.remove(history)
53 elseif( type(v) == "boolean" ) then 65 elseif( type(v) == "boolean" ) then
54 print(depth .. i .. " - " .. tostring(v)) 66 print(depth .. i .. " - " .. tostring(v))
55 elseif( type(v) == "function" ) then 67 elseif( type(v) == "function" ) then
56 print(depth .. "function " .. i .. "()") 68 print(depth .. "function " .. i .. "()")
57 else 69 else