-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobj.lua
More file actions
51 lines (35 loc) · 962 Bytes
/
Copy pathobj.lua
File metadata and controls
51 lines (35 loc) · 962 Bytes
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
-- an obj is general objs, including tables, numbers, strings, etc
local pairs,type,rawget,rawset=pairs,type,rawget,rawset
local push,concat,format=table.insert,table.concat,string.format
local copy_,obj2str_
copy_=function(src,dst)
if type(src)~='table' then return src end
dst={}
for k,v in pairs(src) do
rawset(dst,k,copy_(v))
end
return dst
end
obj2str_=function(o)
local t=type(o)
if t~='table' then return t=='string' and format("%q",o) or tostring(o) end
local tt={}
for k,v in pairs(o) do
push(tt,format("[%s]=%s",obj2str_(k),obj2str_(v)))
end
return format("{%s}",concat(tt,","))
end
copy,obj2str=copy_,obj2str_
-- extend
local loadstring,assert=loadstring,assert
str2obj=function(str)
str=format("return %s",str)
str=assert(loadstring(str))
return str and str()
end
-- test
--~ local t={4,3,4,{4,{2,"%d",222.0},c=4}}
--~ local str=obj2str_(t)
--~ local obj=str2obj(str)
--~ print(obj2str_(t))
--~ print(obj2str_(obj))