function Encode(j)if not inited then Init()end local H=Copy(dictionary)local p,e,A="",{},#H local M,G,n=1,{},0 local function q(j)local p=ToBase93(H[j])if#p>M then M,n,G[M]=#p,0,n end e[#e+1]=(" "):rep(M-#p)..p n=n+1 end j=Escape(j)for e=1,#j,1 do local M=j:sub(e,e)local G=p..M if H[G]then p=G else q(p)p,A=M,A+1 H[G],H[A]=A,G end end q(p)G[M]=n return table.concat(G,"\044")..("\124"..table.concat(e))end function Decode(j)if not inited then Init()end local H=Copy(dictionary)local p,e,A={},j:match("\040\046-\041\124\040\046\042\041")local M,G={},1 for j in e:gmatch("\037\100\043")do local H=#M+1 M[H]=A:sub(G,(G+j*H)-1)G=G+j*H end local n for j=1,#M,1 do for j in M[j]:gmatch(("\046"):rep(j))do local e=H[ToBase10(j)]if n then if e then p[#p+1]=e H[#H+1]=n..e:sub(1,1)else e=n..n:sub(1,1)p[#p+1]=e H[#H+1]=e end else p[1]=e end n=e end end return Unescape(table.concat(p))end function Escape(j)return j:gsub("\091\037\099\"\092\093",function(j)return"\127"..escapemap[j]end)end function Unescape(j)return j:gsub("\127\040\046\041",function(j)return escapemap[j]end)end function Copy(j)local H={}for j,p in pairs(j)do H[j]=p end return H end function ToBase93(j)local H=""repeat local p=j%93 H=dictionary[p]..H j=(j-p)/93 until j==0 return H end function ToBase10(j)local H=0 for p=1,#j,1 do H=H+93^(p-1)*dictionary[j:sub(-p,-p)]end return H end function Init()for j=32,127,1 do if j~=34 and j~=92 then local H=string.char(j)dictionary[H],dictionary[length]=length,H length=length+1 end end for j=1,34,1 do j=({34;92,127})[j-31]or j local H,p=string.char(j),string.char(j+31)escapemap[H],escapemap[p]=p,H end inited=true end



function Encode(text)
	--- Init (if haven't already)
	if not inited then
		Init()
	end
	
	--- Main
	local dictionary = Copy(dictionary)
	local key, sequence, size = "", {}, #dictionary
	local width, spans, span = 1, {}, 0
	local function listkey(key)
		local value = ToBase93(dictionary[key])
		if #value > width then
			width, span, spans[width] = #value, 0, span
		end
		sequence[#sequence+1] = (" "):rep(width - #value)..value
		span = span + 1
	end
	text = Escape(text)
	
	for i = 1, #text do
		local c = text:sub(i, i)
		local new = key..c
		if dictionary[new] then
			key = new
		else
			listkey(key)
			key, size = c, size+1
			dictionary[new], dictionary[size] = size, new
		end
	end
    listkey(key)
	spans[width] = span
	
	--
    return table.concat(spans, ",").."|"..table.concat(sequence)
end


function Decode(text)
	--- Init (if haven't already)
	if not inited then
		Init()
	end
	
	--- Main
	local dictionary = Copy(dictionary)
	local sequence, spans, content = {}, text:match("(.-)|(.*)")
	local groups, start = {}, 1
	for span in spans:gmatch("%d+") do
		local width = #groups+1
		groups[width] = content:sub(start, start + span*width - 1)
		start = start + span*width
	end
	
	local previous;
	for width = 1, #groups do
		for value in groups[width]:gmatch(('.'):rep(width)) do
			local entry = dictionary[ToBase10(value)]
			if previous then
				if entry then
					sequence[#sequence+1] = entry
					dictionary[#dictionary+1] = previous..entry:sub(1, 1)
				else
					entry = previous..previous:sub(1, 1)
					sequence[#sequence+1] = entry
					dictionary[#dictionary+1] = entry
				end
			else
				sequence[1] = entry
			end
			previous = entry
		end
	end
	
	--
	return Unescape(table.concat(sequence))
end


function Escape(s)
	return (s:gsub("[%c\"\\]", function(c)
		return "\127"..escapemap[c]
	end))
end


function Unescape(s)
	return (s:gsub("\127(.)", function(c)
		return escapemap[c]
	end))
end


function Copy(t)
	local new = {}
	for k, v in pairs(t) do
		new[k] = v
	end
	return new
end


function ToBase93(n)
	local value = ""
	repeat
		local remainder = n%93
		value = dictionary[remainder]..value
		n = (n - remainder)/93
	until n == 0
	return value
end


function ToBase10(value)
	local n = 0
	for i = 1, #value do
		n = n + 93^(i-1)*dictionary[value:sub(-i, -i)]
	end
	return n
end


function Init()
	for i = 32, 127 do
		if i ~= 34 and i ~= 92 then
			local c = string.char(i)
			dictionary[c], dictionary[length] = length, c
			length = length + 1
		end
	end
	
	for i = 1, 34 do
		i = ({34, 92, 127})[i-31] or i
		local c, e = string.char(i), string.char(i + 31)
		escapemap[c], escapemap[e] = e, c
	end
	
	--
	inited = true
end

