From 628365cb495727d01edc191414d4f064ab1705e9 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Sun, 6 Jun 2021 17:29:57 +0800 Subject: [PATCH] =?UTF-8?q?string/table=E6=89=A9=E5=B1=95=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=96=B0=E5=A2=9E=E5=87=A0=E4=B8=AA=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=95=B0=E6=8D=AE=E6=89=93=E5=8C=85/?= =?UTF-8?q?=E8=A7=A3=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/stringExtend.lua | 42 ++++++++++++++++++++++-------- Zframework/tableExtend.lua | 51 +++++++++++++++++++++++++++++++++++++ parts/data.lua | 10 +++----- parts/scenes/savedata.lua | 16 +++--------- 4 files changed, 89 insertions(+), 30 deletions(-) diff --git a/Zframework/stringExtend.lua b/Zframework/stringExtend.lua index 70907c65..96d4192b 100644 --- a/Zframework/stringExtend.lua +++ b/Zframework/stringExtend.lua @@ -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 \ No newline at end of file diff --git a/Zframework/tableExtend.lua b/Zframework/tableExtend.lua index 8511354e..18d3b359 100644 --- a/Zframework/tableExtend.lua +++ b/Zframework/tableExtend.lua @@ -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 \ No newline at end of file diff --git a/parts/data.lua b/parts/data.lua index 2d1ad870..57f7c8ea 100644 --- a/parts/data.lua +++ b/parts/data.lua @@ -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 diff --git a/parts/scenes/savedata.lua b/parts/scenes/savedata.lua index 5b0e165d..03613178 100644 --- a/parts/scenes/savedata.lua +++ b/parts/scenes/savedata.lua @@ -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