7 lines
6.4 KiB
Lua
7 lines
6.4 KiB
Lua
-- Serpent @879580fb21933f63eb23ece7d60ba2349a8d2848 | /src/serpent.lua | https://github.com/pkulchenko/serpent | License: MIT License | Minified using https://www.npmjs.com/package/luamin/v/1.0.4
|
|
-- Copyright (c) 2012-2018 Paul Kulchenko (paul@kulchenko.com)
|
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
local a,b="serpent","0.302"local c,d="Paul Kulchenko","Lua serializer and pretty printer"local e={[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}local f={thread=true,userdata=true,cdata=true}local getmetatable=debug and debug.getmetatable or getmetatable;local g=function(h)return next,h end;local i,j,k={},{},_G or _ENV;for l,m in ipairs({'and','break','do','else','elseif','end','false','for','function','goto','if','in','local','nil','not','or','repeat','return','then','true','until','while'})do i[m]=true end;for m,b in g(k)do j[b]=m end;for l,n in ipairs({'coroutine','debug','io','math','string','table','os'})do for m,b in g(type(k[n])=='table'and k[n]or{})do j[b]=n..'.'..m end end;local function o(h,p)local q,r,s,t=p.name,p.indent,p.fatal,p.maxnum;local u,v,w=p.sparse,p.custom,not p.nohuge;local x,y=p.compact and''or' ',p.maxlevel or math.huge;local z,A=tonumber(p.maxlength),p.metatostring;local B,C='_'..(q or''),p.comment and(tonumber(p.comment)or math.huge)local D=p.numformat or"%.17g"local E,F,G,H={},{'local '..B..'={}'},{},0;local function I(J)return'_'..tostring(tostring(J)):gsub("[^%w]",""):gsub("(%d%w+)",function(o)if not G[o]then H=H+1;G[o]=H end;return tostring(G[o])end)end;local function K(o)return type(o)=="number"and tostring(w and e[tostring(o)]or D:format(o))or type(o)~="string"and tostring(o)or("%q"):format(o):gsub("\010","n"):gsub("\026","\\026")end;local function L(o,M)return C and(M or 0)<C and' --[['..select(2,pcall(tostring,o))..']]'or''end;local function N(o,M)return j[o]and j[o]..L(o,M)or not s and K(select(2,pcall(tostring,o)))or error("Can't serialize "..tostring(o))end;local function O(P,q)local a=q==nil and''or q;local Q=type(a)=="string"and a:match("^[%l%u_][%w_]*$")and not i[a]local R=Q and a or'['..K(a)..']'return(P or'')..(Q and P and'.'or'')..R,R end;local S=type(p.sortkeys)=='function'and p.sortkeys or function(m,T,a)local U,V=tonumber(a)or 12,{number='a',string='b'}local function W(d)return("%0"..tostring(U).."d"):format(tonumber(d))end;table.sort(m,function(X,Y)return(m[X]~=nil and 0 or V[type(X)]or'z')..tostring(X):gsub("%d+",W)<(m[Y]~=nil and 0 or V[type(Y)]or'z')..tostring(Y):gsub("%d+",W)end)end;local function Z(h,q,r,_,P,a0,a1)local a2,a1,a3=type(h),a1 or 0,getmetatable(h)local a4,a5=O(P,q)local a6=a0 and(type(q)=="number"and''or q..x..'='..x)or(q~=nil and a5 ..x..'='..x or'')if E[h]then F[#F+1]=a4 ..x..'='..x..E[h]return a6 ..'nil'..L('ref',a1)end;if type(a3)=='table'and A~=false then local V,a7=pcall(function()return a3.__tostring(h)end)local a8,a9=pcall(function()return a3.__serialize(h)end)if V or a8 then E[h]=_ or a4;h=a8 and a9 or a7;a2=type(h)end end;if a2=="table"then if a1>=y then return a6 ..'{}'..L('maxlvl',a1)end;E[h]=_ or a4;if next(h)==nil then return a6 ..'{}'..L(h,a1)end;if z and z<0 then return a6 ..'{}'..L('maxlen',a1)end;local U,T,aa=math.min(#h,t or#h),{},{}for ab=1,U do T[ab]=ab end;if not t or#T<t then local a=#T;for ab in g(h)do if T[ab]~=ab then a=a+1;T[a]=ab end end end;if t and#T>t then T[t+1]=nil end;if p.sortkeys and#T>U then S(T,h,p.sortkeys)end;local u=u and#T>U;for a,ab in ipairs(T)do local ac,ad,a0=h[ab],type(ab),a<=U and not u;if p.valignore and p.valignore[ac]or p.keyallow and not p.keyallow[ab]or p.keyignore and p.keyignore[ab]or p.valtypeignore and p.valtypeignore[type(ac)]or u and ac==nil then elseif ad=='table'or ad=='function'or f[ad]then if not E[ab]and not j[ab]then F[#F+1]='placeholder'local a5=O(B,I(ab))F[#F]=Z(ab,a5,r,a5,B,true)end;F[#F+1]='placeholder'local P=E[h]..'['..tostring(E[ab]or j[ab]or I(ab))..']'F[#F]=P..x..'='..x..tostring(E[ac]or Z(ac,nil,r,P))else aa[#aa+1]=Z(ac,ab,r,nil,E[h],a0,a1+1)if z then z=z-#aa[#aa]if z<0 then break end end end end;local ae=string.rep(r or'',a1)local af=r and'{\n'..ae..r or'{'local ag=table.concat(aa,','..(r and'\n'..ae..r or x))local ah=r and"\n"..ae..'}'or'}'return(v and v(a6,af,ag,ah,a1)or a6 ..af..ag..ah)..L(h,a1)elseif f[a2]then E[h]=_ or a4;return a6 ..N(h,a1)elseif a2=='function'then E[h]=_ or a4;if p.nocode then return a6 .."function() --[[..skipped..]] end"..L(h,a1)end;local ai,aj=pcall(string.dump,h)local ak=ai and"((loadstring or load)("..K(aj)..",'@serialized'))"..L(h,a1)return a6 ..(ak or N(h,a1))else return a6 ..K(h)end end;local al=r and"\n"or";"..x;local ag=Z(h,q,r)local ah=#F>1 and table.concat(F,al)..al or''local am=p.comment and#F>1 and x.."--[[incomplete output with shared/self-references skipped]]"or''return not q and ag..am or"do local "..ag..al..ah.."return "..q..al.."end"end;local function an(ao,p)local ap=p and p.safe==false and k or setmetatable({},{__index=function(h,m)return h end,__call=function(h,...)error("cannot call functions")end})local aq,aj=(loadstring or load)('return '..ao,nil,nil,ap)if not aq then aq,aj=(loadstring or load)(ao,nil,nil,ap)end;if not aq then return aq,aj end;if setfenv then setfenv(aq,ap)end;return pcall(aq)end;local function ar(X,Y)if Y then for m,b in g(Y)do X[m]=b end end;return X end;return{_NAME=a,_COPYRIGHT=c,_DESCRIPTION=d,_VERSION=b,serialize=o,load=an,dump=function(X,p)return o(X,ar({name='_',compact=true,sparse=true},p))end,line=function(X,p)return o(X,ar({sortkeys=true,comment=true},p))end,block=function(X,p)return o(X,ar({indent=' ',sortkeys=true,comment=true},p))end}
|