mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
simplifies id generation. Fixes #28
This commit is contained in:
parent
dca60a2beb
commit
69fc645184
31
inspect.lua
31
inspect.lua
@ -127,21 +127,6 @@ local function getToStringResultSafely(t, mt)
|
|||||||
if type(str) == 'string' and #str > 0 then return str end
|
if type(str) == 'string' and #str > 0 then return str end
|
||||||
end
|
end
|
||||||
|
|
||||||
local maxIdsMetaTable = {
|
|
||||||
__index = function(self, typeName)
|
|
||||||
rawset(self, typeName, 0)
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
local idsMetaTable = {
|
|
||||||
__index = function (self, typeName)
|
|
||||||
local col = {}
|
|
||||||
rawset(self, typeName, col)
|
|
||||||
return col
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
local function countTableAppearances(t, tableAppearances)
|
local function countTableAppearances(t, tableAppearances)
|
||||||
tableAppearances = tableAppearances or {}
|
tableAppearances = tableAppearances or {}
|
||||||
|
|
||||||
@ -229,16 +214,16 @@ function Inspector:tabify()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:alreadyVisited(v)
|
function Inspector:alreadyVisited(v)
|
||||||
return self.ids[type(v)][v] ~= nil
|
return self.ids[v] ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function Inspector:getId(v)
|
function Inspector:getId(v)
|
||||||
local tv = type(v)
|
local id = self.ids[v]
|
||||||
local id = self.ids[tv][v]
|
|
||||||
if not id then
|
if not id then
|
||||||
id = self.maxIds[tv] + 1
|
local tv = type(v)
|
||||||
|
id = (self.maxIds[tv] or 0) + 1
|
||||||
self.maxIds[tv] = id
|
self.maxIds[tv] = id
|
||||||
self.ids[tv][v] = id
|
self.ids[v] = id
|
||||||
end
|
end
|
||||||
return tostring(id)
|
return tostring(id)
|
||||||
end
|
end
|
||||||
@ -336,10 +321,10 @@ function inspect.inspect(root, options)
|
|||||||
|
|
||||||
local inspector = setmetatable({
|
local inspector = setmetatable({
|
||||||
depth = depth,
|
depth = depth,
|
||||||
buffer = {},
|
|
||||||
level = 0,
|
level = 0,
|
||||||
ids = setmetatable({}, idsMetaTable),
|
buffer = {},
|
||||||
maxIds = setmetatable({}, maxIdsMetaTable),
|
ids = {},
|
||||||
|
maxIds = {},
|
||||||
newline = newline,
|
newline = newline,
|
||||||
indent = indent,
|
indent = indent,
|
||||||
tableAppearances = countTableAppearances(root)
|
tableAppearances = countTableAppearances(root)
|
||||||
|
Loading…
Reference in New Issue
Block a user