新增一个凑合用的urlencode编码库
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
local gc=love.graphics
|
local gc=love.graphics
|
||||||
local int=math.floor
|
local int=math.floor
|
||||||
local sub,find,format=string.sub,string.find,string.format
|
local sub,find,format=string.sub,string.find,string.format
|
||||||
|
local ins=table.insert
|
||||||
|
|
||||||
do--LOADLIB
|
do--LOADLIB
|
||||||
local libs={
|
local libs={
|
||||||
@@ -256,7 +257,7 @@ do--json
|
|||||||
end
|
end
|
||||||
if n ~= #val then error("invalid table: sparse array") end
|
if n ~= #val then error("invalid table: sparse array") end
|
||||||
-- Encode
|
-- 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
|
stack[val] = nil
|
||||||
return "[" .. table.concat(res, ",") .. "]"
|
return "[" .. table.concat(res, ",") .. "]"
|
||||||
|
|
||||||
@@ -266,7 +267,7 @@ do--json
|
|||||||
if type(k) ~= "string" then
|
if type(k) ~= "string" then
|
||||||
error("invalid table: mixed or invalid key types")
|
error("invalid table: mixed or invalid key types")
|
||||||
end
|
end
|
||||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
ins(res, encode(k, stack) .. ":" .. encode(v, stack))
|
||||||
end
|
end
|
||||||
stack[val] = nil
|
stack[val] = nil
|
||||||
return "{" .. table.concat(res, ",") .. "}"
|
return "{" .. table.concat(res, ",") .. "}"
|
||||||
@@ -531,6 +532,36 @@ do--json
|
|||||||
return pcall(decode,str)
|
return pcall(decode,str)
|
||||||
end
|
end
|
||||||
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)
|
function copyList(org)
|
||||||
local L={}
|
local L={}
|
||||||
for i=1,#org do
|
for i=1,#org do
|
||||||
|
|||||||
Reference in New Issue
Block a user