-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabletolua.lua
66 lines (61 loc) · 1.3 KB
/
tabletolua.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
local _M = { }
local string_char = string.char
local string_byte = string.byte
local trans_char = {
['\"'] = "\\\"",
['\t'] = "\\t",
['\n'] = "\\n",
['\a'] = "\\a",
['\b'] = "\\b",
['\f'] = "\\f",
['\r'] = "\\r",
['\v'] = "\\v",
['\\'] = "\\\\",
['\''] = "\\\'"
}
local function trans2escapechar(str)
local newstr = ""
for i=1, #str do
local c = string_char(string_byte(str, i))
if trans_char[c] then
newstr = newstr .. trans_char[c]
else
newstr = newstr .. c
end
end
return newstr
end
local function tostringvalue(value)
if type(value) == "table" then
return _M.tabletostr(value)
elseif type(value) == "string" then
return "\"" .. trans2escapechar(value) .. "\""
end
return tostring(value)
end
function _M.tabletostr(t)
local retstr = "{"
local i, first = 1, true
for k,v in pairs(t) do
local comma = ","
if first then
comma = ""
first = false
end
if k == i then
retstr = retstr .. comma .. tostringvalue(v)
i = i + 1
elseif type(k) == "number" then
retstr = retstr .. comma .. "[" .. k .. "]=" .. tostringvalue(v)
elseif type(k) == "string" then
retstr = retstr .. comma .. k .. "=" .. tostringvalue(v)
end
end
retstr = retstr .. "}"
return retstr
end
function _M.toluastring(t)
if t == nil then return "" end
return _M.tabletostr(t)
end
return _M