diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-12-18 16:50:31 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-12-18 16:50:31 -0800 |
| commit | 21a6314e270344e6923bb4f0a06fd09b0d9ae581 (patch) | |
| tree | 9a12aaed18ad4237e45149d53785295a35b58cd2 /utils.lua | |
| parent | 0987fde8e71b28c392c505332091ebf02e84f5c1 (diff) | |
Tweaked repr and added a depth parameter.
Diffstat (limited to 'utils.lua')
| -rw-r--r-- | utils.lua | 22 |
1 files changed, 14 insertions, 8 deletions
@@ -24,22 +24,28 @@ local function size(t) return n end -local function repr(x) +local function repr(x, depth) + -- Create a string representation of the object that is close to the lua code that will + -- reproduce the object (similar to Python's "repr" function) + depth = depth or math.huge + if depth == 0 then + return tostring(x) + end local x_type = type(x) if x_type == 'table' then - local mt = getmetatable(x) - if mt and mt.__tostring then - return mt.__tostring(x) + if getmetatable(x) then + -- If this object has a weird metatable, then don't pretend like it's a regular table + return tostring(x) elseif is_list(x) then local ret = {} for i=1,#x do - ret[i] = repr(x[i]) + ret[i] = repr(x[i], depth-1) end return "{"..table.concat(ret, ", ").."}" else local ret = {} for k, v in pairs(x) do - ret[#ret+1] = "["..repr(k).."]= "..repr(v) + ret[#ret+1] = "["..repr(k,depth-1).."]= "..repr(v,depth-1) end return "{"..table.concat(ret, ", ").."}" end @@ -67,11 +73,11 @@ local function repr(x) end end -local function stringify(x) +local function stringify(x, depth) if type(x) == 'string' then return x else - return repr(x) + return repr(x, depth) end end |
