From b664beda1d841ab4dd46b54eeecb9de7b40f9b82 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Thu, 5 Nov 2020 17:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AA=E5=87=91?= =?UTF-8?q?=E5=90=88=E7=94=A8=E7=9A=84urlencode=E7=BC=96=E7=A0=81=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/toolfunc.lua | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Zframework/toolfunc.lua b/Zframework/toolfunc.lua index 92fe201a..eaede9ed 100644 --- a/Zframework/toolfunc.lua +++ b/Zframework/toolfunc.lua @@ -1,6 +1,7 @@ local gc=love.graphics local int=math.floor local sub,find,format=string.sub,string.find,string.format +local ins=table.insert do--LOADLIB local libs={ @@ -256,7 +257,7 @@ do--json end if n ~= #val then error("invalid table: sparse array") end -- Encode - for _, v in ipairs(val) do table.insert(res, encode(v, stack)) end + for _, v in ipairs(val) do ins(res, encode(v, stack)) end stack[val] = nil return "[" .. table.concat(res, ",") .. "]" @@ -266,7 +267,7 @@ do--json if type(k) ~= "string" then error("invalid table: mixed or invalid key types") end - table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) + ins(res, encode(k, stack) .. ":" .. encode(v, stack)) end stack[val] = nil return "{" .. table.concat(res, ",") .. "}" @@ -531,6 +532,36 @@ do--json return pcall(decode,str) end end +do--urlencode + local rshift=bit.rshift + local b16={[0]="0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"} + urlencode={} + function urlencode.encode(T) + local out={} + for k,v in next,T do + local k1="" + for i=1,#k do + if k:sub(i,i):match("[a-zA-Z0-9]")then + k1=k1..k:sub(i,i) + else + local b=k:byte(i) + k1=k1.."%"..b16[rshift(b,4)]..b16[b%16] + end + end + local v1="" + for i=1,#v do + if v:sub(i,i):match("[a-zA-Z0-9]")then + v1=v1..v:sub(i,i) + else + local b=v:byte(i) + v1=v1.."%"..b16[rshift(b,4)]..b16[b%16] + end + end + ins(out,k1.."="..v1) + end + return table.concat(out,"&") + end +end function copyList(org) local L={} for i=1,#org do