string/table扩展模块新增几个方法用于数据打包/解包
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
local data=love.data
|
||||||
local STRING={}
|
local STRING={}
|
||||||
local int,format=math.floor,string.format
|
local int,format=math.floor,string.format
|
||||||
local find,sub,upper=string.find,string.sub,string.upper
|
local find,sub,upper=string.find,string.sub,string.upper
|
||||||
@@ -16,10 +17,10 @@ do--function STRING.shiftChar(c)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function STRING.trim(str)
|
function STRING.trim(s)
|
||||||
if not str:find("%S")then return""end
|
if not s:find("%S")then return""end
|
||||||
str=str:sub((str:find("%S"))):reverse()
|
s=s:sub((s:find("%S"))):reverse()
|
||||||
return str:sub((str:find("%S"))):reverse()
|
return s:sub((s:find("%S"))):reverse()
|
||||||
end
|
end
|
||||||
|
|
||||||
function STRING.split(s,sep,regex)
|
function STRING.split(s,sep,regex)
|
||||||
@@ -63,16 +64,16 @@ function STRING.time(s)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
do--function STRING.urlEncode(str)
|
do--function STRING.urlEncode(s)
|
||||||
local rshift=bit.rshift
|
local rshift=bit.rshift
|
||||||
local b16={[0]='0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
|
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=""
|
local out=""
|
||||||
for i=1,#str do
|
for i=1,#s do
|
||||||
if str:sub(i,i):match("[a-zA-Z0-9]")then
|
if s:sub(i,i):match("[a-zA-Z0-9]")then
|
||||||
out=out..str:sub(i,i)
|
out=out..s:sub(i,i)
|
||||||
else
|
else
|
||||||
local b=str:byte(i)
|
local b=s:byte(i)
|
||||||
out=out.."%"..b16[rshift(b,4)]..b16[b%16]
|
out=out.."%"..b16[rshift(b,4)]..b16[b%16]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -80,4 +81,25 @@ do--function STRING.urlEncode(str)
|
|||||||
end
|
end
|
||||||
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
|
return STRING
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
local data=love.data
|
||||||
local next,type=next,type
|
local next,type=next,type
|
||||||
local TABLE={}
|
local TABLE={}
|
||||||
|
|
||||||
@@ -149,4 +150,54 @@ do--function TABLE.dump(L,t)
|
|||||||
TABLE.dump=dump
|
TABLE.dump=dump
|
||||||
end
|
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
|
return TABLE
|
||||||
@@ -93,7 +93,7 @@ function DATA.copyBoard(page)--Copy the [page] board
|
|||||||
end
|
end
|
||||||
str=str..S
|
str=str..S
|
||||||
end
|
end
|
||||||
return data.encode('string','base64',data.compress('string','zlib',str))
|
return STRING.packBin(str)
|
||||||
end
|
end
|
||||||
function DATA.copyBoards()
|
function DATA.copyBoards()
|
||||||
local out={}
|
local out={}
|
||||||
@@ -108,12 +108,8 @@ function DATA.pasteBoard(str,page)--Paste [str] data to [page] board
|
|||||||
local F=FIELD[page]
|
local F=FIELD[page]
|
||||||
|
|
||||||
--Decode
|
--Decode
|
||||||
local res
|
str=STRING.unpackBin(str)
|
||||||
str=STRING.trim(str)
|
if not str then return end
|
||||||
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
|
|
||||||
|
|
||||||
local fX,fY=1,1--*ptr for Field(r*10+(c-1))
|
local fX,fY=1,1--*ptr for Field(r*10+(c-1))
|
||||||
local p=1
|
local p=1
|
||||||
|
|||||||
@@ -1,15 +1,7 @@
|
|||||||
local scene={}
|
local scene={}
|
||||||
|
|
||||||
local function dumpCB(T)
|
local function dumpCB(T)
|
||||||
love.system.setClipboardText(
|
love.system.setClipboardText(STRING.packText(TABLE.dump(T)))
|
||||||
love.data.encode(
|
|
||||||
'string','base64',
|
|
||||||
love.data.compress(
|
|
||||||
'string','zlib',
|
|
||||||
TABLE.dump(T)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
LOG.print(text.exportSuccess,'message')
|
LOG.print(text.exportSuccess,'message')
|
||||||
end
|
end
|
||||||
local function parseCB()
|
local function parseCB()
|
||||||
@@ -17,10 +9,8 @@ local function parseCB()
|
|||||||
local s=love.system.getClipboardText()
|
local s=love.system.getClipboardText()
|
||||||
|
|
||||||
--Decode
|
--Decode
|
||||||
_,s=pcall(love.data.decode,'string','base64',s)
|
s=STRING.unpackText(s)
|
||||||
if not _ then LOG.print(text.dataCorrupted,'error')return end
|
if not s 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=loadstring(s)
|
s=loadstring(s)
|
||||||
if s then
|
if s then
|
||||||
|
|||||||
Reference in New Issue
Block a user