-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper.lua
More file actions
212 lines (176 loc) · 5.91 KB
/
helper.lua
File metadata and controls
212 lines (176 loc) · 5.91 KB
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
local helper = {}
function helper.serialize(t)
local mark={}
local assign={}
local function ser_table(tbl,parent)
mark[tbl]=parent
local tmp={}
for k,v in pairs(tbl) do
local key= type(k)=="number" and "["..k.."]" or k
if type(v)=="table" then
local dotkey= parent..(type(k)=="number" and key or "."..key)
if mark[v] then
table.insert(assign,dotkey.."="..mark[v])
else
table.insert(tmp, key.."="..ser_table(v,dotkey))
end
else
if type(v) == "string" then
table.insert(tmp, key.."='"..v.. "'")
else
table.insert(tmp, key.."="..v)
end
end
end
return "{"..table.concat(tmp,",").."}"
end
return ser_table(t,"ret")..table.concat(assign," ")
-- return "do local ret="..ser_table(t,"ret")..table.concat(assign," ").." return ret end"
end
function helper.unserialize(lua)
local t = type(lua)
if t == "nil" or lua == "" then
return nil
elseif t == "number" or t == "string" or t == "boolean" then
lua = tostring(lua)
else
error("can not unserialize a " .. t .. " type.")
end
lua = "do local ret="..lua.." return ret end"
local func = load(lua)
if func == nil then
return nil
end
return func()
end
function helper.split(input, delimiter)
if not input then
return nil
end
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
function helper.hex(s,radix)
local rad = {[10]="%03u ",[8]="%03o ",[16]="%02X "}
local t = radix or 16
s=string.gsub(s,"(.)",function (x) return string.format(rad[t],string.byte(x)) end)
return s
end
function string.split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
function string.ltrim(input)
return string.gsub(input, "^[ \t\n\r]+", "")
end
-- start --
--------------------------------
-- 去除输入字符串尾部的空白字符,返回结果
-- @function [parent=#string] rtrim
-- @param string input 输入字符串
-- @return string#string 结果
-- @see string.ltrim, string.trim
--[[--
去除输入字符串尾部的空白字符,返回结果
~~~ lua
local input = "ABC "
print(string.rtrim(input))
-- 输出 ABC,输入字符串最后的两个空格被去掉了
~~~
]]
-- end --
function string.rtrim(input)
return string.gsub(input, "[ \t\n\r]+$", "")
end
-- start --
--------------------------------
-- 去掉字符串首尾的空白字符,返回结果
-- @function [parent=#string] trim
-- @param string input 输入字符串
-- @return string#string 结果
-- @see string.ltrim, string.rtrim
--[[--
去掉字符串首尾的空白字符,返回结果
]]
-- end --
function string.trim(input)
input = string.gsub(input, "^[ \t\n\r]+", "")
return string.gsub(input, "[ \t\n\r]+$", "")
end
function helper.dump(value, desciption, nesting)
if type(nesting) ~= "number" then nesting = 3 end
local lookupTable = {}
local result = {}
local function _v(v)
if type(v) == "string" then
v = "\"" .. v .. "\""
end
return tostring(v)
end
local traceback = string.split(debug.traceback("", 2), "\n")
print("dump from: " .. string.trim(traceback[3]))
local function _dump(value, desciption, indent, nest, keylen)
desciption = desciption or "<var>"
local spc = ""
if type(keylen) == "number" then
spc = string.rep(" ", keylen - string.len(_v(desciption)))
end
if type(value) ~= "table" then
result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(desciption), spc, _v(value))
elseif lookupTable[value] then
result[#result +1 ] = string.format("%s%s%s = *REF*", indent, desciption, spc)
else
lookupTable[value] = true
if nest > nesting then
result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, desciption)
else
result[#result +1 ] = string.format("%s%s = {", indent, _v(desciption))
local indent2 = indent.." "
local keys = {}
local keylen = 0
local values = {}
for k, v in pairs(value) do
keys[#keys + 1] = k
local vk = _v(k)
local vkl = string.len(vk)
if vkl > keylen then keylen = vkl end
values[k] = v
end
table.sort(keys, function(a, b)
if type(a) == "number" and type(b) == "number" then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
_dump(values[k], k, indent2, nest + 1, keylen)
end
result[#result +1] = string.format("%s}", indent)
end
end
end
_dump(value, desciption, "- ", 1)
for i, line in ipairs(result) do
print(line)
end
end
return helper