1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
| -- @func tostring: Extend tostring to work better on tables
-- @param x: object to convert to string
-- @returns
-- @param s: string representation
_G._tostring = tostring -- make original tostring available
local _tostring = tostring
function _G.tostring (x)
return render (x,
function () return "{" end,
function () return "}" end,
_tostring,
function (t, _, _, i, v)
return i .. "=" .. v
end,
function (_, i, _, j)
if i and j then
return ","
end
return ""
end)
end
-- @func prettytostring: pretty-print a table
-- @t: table to print
-- @indent: indent between levels ["\t"]
-- @spacing: space before every line
-- @returns
-- @s: pretty-printed string
function _G.prettytostring (t, indent, spacing)
indent = indent or "\t"
spacing = spacing or ""
return render (t,
function ()
local s = spacing .. "{"
spacing = spacing .. indent
return s
end,
function ()
spacing = string.gsub (spacing, indent .. "$", "")
return spacing .. "}"
end,
function (x)
if type (x) == "string" then
return string.format ("%q", x)
else
return tostring (x)
end
end,
function (x, i, v, is, vs)
local s = spacing .. "["
if type (i) == "table" then
s = s .. "\n"
end
s = s .. is
if type (i) == "table" then
s = s .. "\n"
end
s = s .. "] ="
if type (v) == "table" then
s = s .. "\n"
else
s = s .. " "
end
s = s .. vs
return s
end,
function (_, i)
local s = "\n"
if i then
s = "," .. s
end
return s
end)
end |