-
Notifications
You must be signed in to change notification settings - Fork 92
/
utils.lua
268 lines (253 loc) · 6.39 KB
/
utils.lua
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
-- Copyright (c) 2010 by Robert G. Jakabosky <[email protected]>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--local getmetatable = debug.getmetatable
local d_getmetatable = debug.getmetatable
local getmetatable = getmetatable
local collectgarbage = collectgarbage
local loadstring = loadstring
local assert = assert
local type = type
local print = print
local tonumber = tonumber
local tostring = tostring
local pairs = pairs
local string = string
local sformat = string.format
local char = string.char
local io = io
module(...)
--
-- test if string is printable text.
--
local allowed_special = {
[0] = true, -- null
[9] = true, -- tab
[10] = true, -- new line
[13] = true, -- carriage return
}
function is_string(bytes)
local c
local max = bytes:len()
for i=1,max do
c = bytes:byte(i)
if c >= 127 then -- not ascii character
return false
elseif c < 32 then -- control character range
if not allowed_special[c] then
-- control characters between NULL and Space
return false
elseif c == 0 and i < max then
-- null byte only allowed at end.
return false
end
end
end
return true
end
--
-- hex dump functions.
--
local write = io.write
local function printf(fmt, ...)
return write(sformat(fmt, ...))
end
local rep = string.rep
-- byte to hex map
hex = {}
for i=0,255 do
hex[char(i)] = sformat('%02X', i)
end
function hex_print(buf)
for byte=1, #buf, 16 do
local chunk = buf:sub(byte, byte+15)
printf('%08X ',byte-1)
chunk:gsub('.', hex)
write(rep(' ',3*(16-#chunk)))
write(' ',chunk:gsub('[^%w%p ]','.'),"\n")
end
end
function hex_dump(buf)
local out = ''
for byte=1, #buf, 16 do
local chunk = buf:sub(byte, byte+15)
out = out .. sformat('%08X ',byte-1)
out = out .. chunk:gsub('.', hex)
out = out .. rep(' ',3*(16-#chunk))
out = out .. ' ' .. chunk:gsub('[^%w%p ]','.') .. "\n"
end
return out
end
--
-- Recursive Lua value dump.
--
local dump_recur
local function dump_meta_recur(seen, obj, depth, dbg)
local mt
if dbg > 1 then
-- get the true metatable.
mt = d_getmetatable(obj)
else
-- get any un-hidden metatable.
mt = getmetatable(obj)
end
if not mt then return '' end
local out = ', mt: ' .. tostring(mt) .. ' '
-- check if this metatable has been seen already.
if seen[mt] then
return out .. "Already dumped..."
end
return out .. dump_recur(seen, mt, depth, dbg)
end
function dump_recur(seen, obj, depth, dbg)
local out
local t = type(obj)
-- if not a table just convert to string.
if t ~= "table" then
if t == "string" then
if is_string(obj) then
out = '"' .. obj .. '"'
else
if #obj > 32 then
out = '<bin>\n' .. hex_dump(obj) .. '\n</bin>'
else
out = '<bin>0x' ..
obj:gsub('.', hex) .. '</bin>'
end
end
else
out = tostring(obj)
if dbg > 0 then
out = out .. dump_meta_recur(seen, obj, depth, dbg)
end
end
return out
end
-- check if table has a __tostring metamethod.
if dbg == 0 then
local mt = d_getmetatable(obj)
if mt then
local tostr = mt.__tostring
if tostr then
out = '"' .. tostr(obj) .. '"'
return out
end
end
end
-- check if this table has been seen already.
if seen[obj] then
return "Already dumped " .. tostring(obj)
end
seen[obj] = true
-- restrict max depth.
if depth >= 10 then
return "{... max depth reached ...}"
end
depth = depth + 1
-- output table key/value pairs
local tabs = string.rep(" ",depth)
local out
if dbg > 0 then
out = tostring(obj) .. " {\n"
else
out = "{\n"
end
for k,v in pairs(obj) do
if type(k) ~= "number" then
out = out .. tabs .. '[' .. dump_recur(seen, k, depth, dbg) .. '] = ' ..
dump_recur(seen, v, depth, dbg) .. ',\n'
else
out = out .. tabs .. '[' .. k .. '] = ' .. dump_recur(seen, v, depth, dbg) .. ',\n'
end
end
out = out .. tabs:sub(1,-3) .. "}"
if dbg > 0 then
out = out .. dump_meta_recur(seen, obj, depth, dbg)
end
return out
end
function dbg_dump(obj, dbg)
local seen = {}
return dump_recur(seen, obj, 0, tonumber(dbg or 1))
end
function dump(obj, dbg)
local seen = {}
return dump_recur(seen, obj, 0, tonumber(dbg or 0))
end
--
-- Simple table dup function
--
local function copy_recur(seen, tab)
if type(tab) ~= 'table' then return tab end
local new = seen[tab]
-- if this table has been copied.
if new then return new end
-- create new table.
new = {}
seen[tab] = new
-- copy values from table to new table.
for k,v in pairs(tab) do
k = copy_recur(seen, k)
v = copy_recur(seen, v)
new[k] = v
end
return new
end
function copy(tab)
local seen = {}
return copy_recur(seen, tab)
end
--
-- memory usage functions.
--
local function sizeof2(size, no_gc, test, ...)
local garbage_size= collectgarbage"count"*1024 - size
if no_gc then
collectgarbage"restart"
end
collectgarbage"collect"
collectgarbage"collect"
size= collectgarbage"count"*1024 - size
if no_gc then
print("used size: " .. size .. " garbage: " .. (garbage_size - size))
else
print("used size: " .. size)
end
test = nil
return size, ...
end
local function sizeof1(no_gc, test, ...)
local size=0
if type(test) == "string" then
test=assert(loadstring(test))
end
collectgarbage"collect"
collectgarbage"collect"
if no_gc then
collectgarbage"stop"
end
size=collectgarbage"count"*1024
return sizeof2(size, no_gc, test, test(...))
end
function sizeof(...)
return sizeof1(false, ...)
end
function sizeof_no_gc(...)
return sizeof1(true, ...)
end