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 STRING={}
local int,format=math.floor,string.format
local find,sub,upper=string.find,string.sub,string.upper
@@ -16,10 +17,10 @@ do--function STRING.shiftChar(c)
end
end
function STRING.trim(str)
if not str:find("%S")then return""end
str=str:sub((str:find("%S"))):reverse()
return str:sub((str:find("%S"))):reverse()
function STRING.trim(s)
if not s:find("%S")then return""end
s=s:sub((s:find("%S"))):reverse()
return s:sub((s:find("%S"))):reverse()
end
function STRING.split(s,sep,regex)
@@ -63,16 +64,16 @@ function STRING.time(s)
end
end
do--function STRING.urlEncode(str)
do--function STRING.urlEncode(s)
local rshift=bit.rshift
local b16={[0]='0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
function STRING.urlEncode(str)
function STRING.urlEncode(s)
local out=""
for i=1,#str do
if str:sub(i,i):match("[a-zA-Z0-9]")then
out=out..str:sub(i,i)
for i=1,#s do
if s:sub(i,i):match("[a-zA-Z0-9]")then
out=out..s:sub(i,i)
else
local b=str:byte(i)
local b=s:byte(i)
out=out.."%"..b16[rshift(b,4)]..b16[b%16]
end
end
@@ -80,4 +81,25 @@ do--function STRING.urlEncode(str)
end
end
function STRING.packBin(s)
return data.encode('string','base64',data.compress('string','zlib',s))
end
function STRING.packText(s)
return data.encode('string','base64',data.compress('string','gzip',s))
end
function STRING.unpackBin(str)
local res
res,str=pcall(data.decode,'string','base64',str)
if not res then return end
res,str=pcall(data.decompress,'string','zlib',str)
if res then return str end
end
function STRING.unpackText(str)
local res
res,str=pcall(data.decode,'string','base64',str)
if not res then return end
res,str=pcall(data.decompress,'string','gzip',str)
if res then return str end
end
return STRING

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

View File

@@ -93,7 +93,7 @@ function DATA.copyBoard(page)--Copy the [page] board
end
str=str..S
end
return data.encode('string','base64',data.compress('string','zlib',str))
return STRING.packBin(str)
end
function DATA.copyBoards()
local out={}
@@ -108,12 +108,8 @@ function DATA.pasteBoard(str,page)--Paste [str] data to [page] board
local F=FIELD[page]
--Decode
local res
str=STRING.trim(str)
res,str=pcall(data.decode,'string','base64',str)
if not res then return end
res,str=pcall(data.decompress,'string','zlib',str)
if not res then return end
str=STRING.unpackBin(str)
if not str then return end
local fX,fY=1,1--*ptr for Field(r*10+(c-1))
local p=1

View File

@@ -1,15 +1,7 @@
local scene={}
local function dumpCB(T)
love.system.setClipboardText(
love.data.encode(
'string','base64',
love.data.compress(
'string','zlib',
TABLE.dump(T)
)
)
)
love.system.setClipboardText(STRING.packText(TABLE.dump(T)))
LOG.print(text.exportSuccess,'message')
end
local function parseCB()
@@ -17,10 +9,8 @@ local function parseCB()
local s=love.system.getClipboardText()
--Decode
_,s=pcall(love.data.decode,'string','base64',s)
if not _ then LOG.print(text.dataCorrupted,'error')return end
_,s=pcall(love.data.decompress,'string','zlib',s)
if not _ then LOG.print(text.dataCorrupted,'error')return end
s=STRING.unpackText(s)
if not s then LOG.print(text.dataCorrupted,'error')return end
s=loadstring(s)
if s then