forked from Xrysnow/lstgx_LuaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting_util.lua
66 lines (62 loc) · 1.75 KB
/
setting_util.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 = {}
function M.format_json(str)
local ret = ''
local indent = ' '
local level = 0
local in_string = false
for i = 1, #str do
local s = string.sub(str, i, i)
if s == '{' and (not in_string) then
level = level + 1
ret = ret .. '{\n' .. string.rep(indent, level)
elseif s == '}' and (not in_string) then
level = level - 1
ret = string.format(
'%s\n%s}', ret, string.rep(indent, level))
elseif s == '"' then
in_string = not in_string
ret = ret .. '"'
elseif s == ':' and (not in_string) then
ret = ret .. ': '
elseif s == ',' and (not in_string) then
ret = ret .. ',\n'
ret = ret .. string.rep(indent, level)
elseif s == '[' and (not in_string) then
level = level + 1
ret = ret .. '[\n' .. string.rep(indent, level)
elseif s == ']' and (not in_string) then
level = level - 1
ret = string.format(
'%s\n%s]', ret, string.rep(indent, level))
else
ret = ret .. s
end
end
return ret
end
local function isval(s)
return s == 'boolean' or s == 'number' or s == 'string'
end
function M.compare(t1, t2)
for k, v in pairs(t1) do
local v2 = t2[k]
if v2 == nil then
return false
end
local ty = type(v)
if ty ~= type(v2) then
return false
end
if isval(ty) then
if v ~= v2 then
return false
end
elseif ty == 'table' then
return M.compare(v, v2)
else
return false
end
end
return true
end
return M