string/table扩展模块新增几个方法用于数据打包/解包

This commit is contained in:
MrZ626
2021-06-06 17:29:57 +08:00
parent f9ed93641c
commit 628365cb49
4 changed files with 89 additions and 30 deletions

View File

@@ -1,3 +1,4 @@
local data=love.data
local next,type=next,type
local TABLE={}
@@ -149,4 +150,54 @@ do--function TABLE.dump(L,t)
TABLE.dump=dump
end
--Dump a simple lua table (no whitespaces)
do--function TABLE.dumpDeflate(L,t)
local find=string.find
local function dump(L)
local s="return{"
if type(L)~='table'then return end
local count=1
for k,v in next,L do
local T=type(k)
if T=='number'then
if k==count then
k=""
count=count+1
else
k="["..k.."]="
end
elseif T=='string'then
if find(k,"[^0-9a-zA-Z_]")then
k="[\""..k.."\"]="
else
k=k.."="
end
elseif T=='boolean'then k="["..k.."]="
else error("Error key type!")
end
T=type(v)
if T=='number'then v=tostring(v)
elseif T=='string'then v="\""..v.."\""
elseif T=='table'then v=dump(v)
elseif T=='boolean'then v=tostring(v)
else error("Error data type!")
end
end
return s.."}"
end
TABLE.dumpDeflate=dump
end
function TABLE.pack(t)
return STRING.packText(TABLE.dumpDeflate(t))
end
function TABLE.unpack(s)
s=loadstring(STRING.unpackText(s))
if s then
setfenv(s,{})
return s()
end
end
return TABLE