forked from Minebea-Intec/zbssvn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
145 lines (129 loc) · 2.66 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
#!/usr/bin/utils
--require"strict"
--
-- my common shortcuts
--
local sprintf=string.format
local sbyte=string.byte
local push=table.insert
local join=table.concat
local utils={}
local function printf(...)
local ok,msg=pcall(sprintf,...)
if not ok then error(msg,2) end
io.write(msg)
io.flush()
end
utils.printf=printf
--
-- my replacement for %q
--
local st=setmetatable({['\n']='\\n',['\r']='\\r',['\t']='\\t',['\"']='\\"',['\\']='\\\\'},
{__index=function(t,k)local v=sprintf("\\%03d",sbyte(k)) t[k]=v return v end})
local function vis(val,len)
if type(val)~="string" then return tostring(val) end
if len and #val>len then return vis(val:sub(1,len)).."..." end
return '"'..(val:gsub("[%z\001-\031\127-\255\\\"]",st))..'"'
end
utils.vis=vis
local function vist(val,sep)
local did={}
sep=","..(sep or "")
local function vt(v)
if type(v)~="table" then return vis(v) end
if did[v] then return did[v] end
did[v]=tostring(v)
local r={}
local d={}
local kk=1
local vv=v[kk]
while vv do
d[kk]=true
push(r,vt(vv))
kk=kk+1
vv=v[kk]
end
for kk,vv in pairs(v) do
if not d[kk] then
if type(kk)=="string" and kk:match("^[_a-zA-Z][_a-zA-Z0-9]*$") then
push(r,sprintf("%s=%s",kk,vt(vv)))
else
push(r,sprintf("[%s]=%s",vt(kk),vt(vv)))
end
end
end
return '{'..join(r,sep)..'}'
end
return vt(val)
end
utils.vist=vist
local function cmp(a,b)
local ta,tb=type(a),type(b)
if ta~=tb then return ta<tb end
return a<b
end
--
-- like pairs, but sorted by keys
--
local function spairs(t,f)
local ks={}
for k in pairs(t) do
ks[#ks+1]=k
end
table.sort(ks,f or cmp)
local n=0
return function()
n=n+1
local k=ks[n]
if k then return k,t[k] end
end
end
utils.spairs=spairs
--
-- (debug) visualisation of data structs
--
local function ShowData(nam,val,file)
local out,fd=io.write,nil
if file then
fd=assert(io.open(file,"w"))
out=function(...)fd:write(...)end
end
local have={}
local function show_data(nam,val)
if type(val)~="table" then
out(nam,"=",vis(val),'\n')
return
end
if have[val] then
out(nam,"=",have[val],'\n')
return
end
have[val]=nam
for k,v in spairs(val) do
if type(k)=="string" and k:match("^[_a-z]%w*$") then
show_data(sprintf("%s.%s",nam,k),v)
else
show_data(sprintf("%s[%s]",nam,vis(k)),v)
end
end
end
show_data(nam,val)
if fd then fd:close() end
end
utils.ShowData=ShowData
local function QW(text)
local words={}
for word in text:gmatch("%S+") do
push(words,word)
words[word]=true
end
return words
end
utils.QW=QW
local function save_file(file,...)
local fd=assert(io.open(file,"wb"))
fd:write(...)
fd:close()
end
utils.save_file=save_file
return utils