新增一个凑合用的urlencode编码库

This commit is contained in:
MrZ626
2020-11-05 17:27:48 +08:00
parent f0c084a255
commit b664beda1d

View File

@@ -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