-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.moon
204 lines (182 loc) · 6.47 KB
/
utils.moon
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
toArr = (aTable) ->
myArray = {}
for i,tbl in ipairs(aTable) do
nbKeys = 0
for k,v in pairs(tbl) do
nbKeys+=1
myArray[#myArray+1]=v
if(nbKeys==0) then
myArray[#myArray+1]="_null_"
for i,v in ipairs(myArray) do --quick fix for the nil value issue in table
if(v=="_null_") then myArray[i]=nil
return myArray
unpackArrValues = (aTable)->
return unpack toArr(aTable)
trim=(str)->
begin = str\match"^%s*()"
return begin > #str and "" or str\match(".*%S", begin)
--Check if a fullString begin with a startString
cmpStartsString=(fullString,startString)->
return string.sub(fullString,1,string.len(startString))==startString
clearTable = (t)->
for i=0, #t do t[i]=nil
dump = (...)->
if(type(...)=="nil") then
print "nil"
return
-- A reasonably bullet-proof table dumper that writes out in Moon syntax;
-- can limit the number of items dumped out, and cycles are detected.
-- No attempt at doing pretty indentation here, but it does try to produce
-- 'nice' looking output by separating the hash and the array part.
--
-- dump = require 'moondump'
-- ...
-- print dump t -- default, limit 1000, respect tostring
-- print dump t, limit:10000,raw:true -- ignore tostring
--
quote = (v) ->
if type(v) == 'string'
'%q'\format(v)
else
tostring(v)
--- return a string representation of a Lua value.
-- Cycles are detected, and a limit on number of items can be imposed.
-- @param t the table
-- @param options
-- limit on items, default 1000
-- raw ignore tostring
-- @return a string
dumpString = (t,options) ->
options = options or {}
limit = options.limit or 1000
buff = tables:{[t]:true}
k,tbuff = 1,nil
put = (v) ->
buff[k] = v
k += 1
put_value = (value) ->
if type(value) ~= 'table'
put quote value
if limit and k > limit
buff[k] = "..."
error "buffer overrun"
else
if not buff.tables[value] -- cycle detection!
buff.tables[value] = true
tbuff value
else
put "<cycle>"
put ','
tbuff = (t) ->
mt = getmetatable t unless options.raw
if type(t) ~= 'table' or mt and mt.__tostring
put quote t
else
put '{'
indices = #t > 0 and {i,true for i = 1,#t}
for key,value in pairs t -- first do the hash part
if indices and indices[key] then continue
if type(key) ~= 'string' then
key = '['..tostring(key)..']'
elseif key\match '%s'
key = quote key
put key..':'
put_value value
if indices -- then bang out the array part
for v in *t
put_value v
if buff[k - 1] == "," then k -= 1
put '}'
-- we pcall because that's the easiest way to bail out if there's an overrun.
pcall tbuff,t
table.concat(buff)
--return dumpString(...)
return dumpString(...,limit:40000,raw:true)
dumpPrint = (...)->
print dump(...)
dumpFile = (filename, content)->
file = io.open(filename, "w+")
file\write(content)
io.close(file)
prettyPrint = (...)->
--This module adds two functions to the `debug` module. While it may seem a
--little unorthodox, this after-the-fact modification of the existing lua
--standard libraries is because it is common that, when a project is ready for
--release, one adds `export debug = nil` to the beginning of the project, so
--that any lingering debug code that is slowing your application down is
--caught with error messages in the final stage of testing.
--Generally, `print` statements are also removed in this same stage, to
--prevent pollution of the console and because stringification of objects
--sometimes has a significant processor overhead. Consequently, it seems
--natural to put the debug printing function in the `debug` library, so that
--all unwanted testing code can be removed with one line.
--
--This module introduces two functions, `debug.write` and `debug.print`, which
--are analogous to the built-in `io.write` and `print` functions, except that
--they handle tables correctly.
--To prevent cycles from being printed, a simple rule is used that every table
--is only printed once in a given invocation of `debug.print` or
--`debug.write`. Even if there are no cycles, and the table structure just
--contains non-tree-like references, it will still print each table only once.
--Every key-value pair is printed on a separate line, so, although always
--remaining fairly readable, the output can get rather large fairly quickly.
ilevel = 0
indent = (a, b)->
steps, fn = if b
a, b
else
1, a
ilevel += steps
fn!
ilevel -= steps
writeindent = -> io.write " "\rep ilevel
debug.write = =>
visited = {}
_write = =>
if type(self) == 'table' and not visited[self]
if not (@@ and @@__name and not @__tostring)
visited[self] = true
print "{"
for k, v in pairs self
indent ->
writeindent!
_write k
io.write ': '
_write v
print!
writeindent!
_write "}"
elseif @__tostring
io.write @__tostring!
else
io.write @@__name
else
io.write tostring self
_write self
debug.print = (...)->
remaining = #{...}
for arg in *{...}
remaining -= 1
debug.write arg
io.write ', ' unless remaining == 0
print!
debug.print(...)
split = (str, sep)->
sep, fields = sep or ":", {}
pattern = string.format("([^%s]+)", sep)
str\gsub(pattern, ((c) -> fields[#fields+1] = c))
return fields
lenTbl = (t)->
i=0
for _ in pairs(t) do
i+=1
return i
getTimeStamp = ()->
if _G.socket ~= nil then
microSecs = _G.socket.gettime()*1000000
--print "genTS: "..microSecs
return microSecs
else
print "ERROR: TIMESTAMP SOCKET"
{:toArr, :cmpStartsString, :prettyPrint, :dumpPrint, :dump, :unpackArrValues,
:clearTable, :trim, :dumpFile, :split, :lenTbl, :getTimeStamp}