Log In  

-- serial
-------------------------------
srl={}srl.sts={open="o",key="k",
value="v"}srl.tok={open="{",
close="}",ass="=",st1=",",
st2=";",quote="\"",sp=" ",nl=
"\n"}srl.strisnum=function(a)
local b=tonum(a)local c=tostr(b)
return a==c,b end;srl.strisbool=
function(a)if a=="true"then
return true,true elseif a==
"false"then return true,false
end;return false,nil end;
srl.trim=function(a)local d=0;
local e=0;local f;for g=1,#a do
f=sub(a,g,g)if not(f==" "or f==
"\n")then if d==0 then d=g;e=g
else e=g end end end;return sub
(a,d,e)end;srl.tostr=function(h)
local a="{"local i=""for g,j in
pairs(h)do a=a..tostr(g).."="i=
type(j)if i=="number"then a=a..
tostr(j)elseif i=="string"then
a=a..j elseif i=="boolean"then
a=a..(j and"true"or"false")
elseif i=="table"then a=a..srl.
tostr(j)elseif i=="function"then
a=a.."(f)"else a=a.."(r)"end;a=a
..";"end;a=a.."}"return a end;
srl.fromstr=function(a,k)k=k or
1;local l={}local m=""local n=""
local o=false;local p=""local q=
""local r=srl.sts.open;local
g=1;while g<=#a do m=sub(a,g,g)
if r==srl.sts.open then if m==
srl.tok.open then r=srl.sts.key
end elseif r==srl.sts.key then
if m==srl.tok.close then return
l,g+k-1 elseif m==srl.tok.ass
then if#n>0 then n=srl.trim(n)
p=n;n=""r=srl.sts.value end
elseif m==srl.tok.st1 or m==srl.
tok.st2 then if#n>0 then n=n..m
end elseif m==" " or m==
"\n" or m=="\t" then if#n>0 then n=n
..m end else n=n..m end elseif
r==srl.sts.value then if m==srl.
tok.open then if#n==0 then l[p],
g=srl.fromstr(sub(a,g),g)end
elseif m==srl.tok.close then n=n
..m elseif m==srl.tok.ass then n
=n..m elseif m==srl.tok.st1 or m
==srl.tok.st2 then if#n>0 then q
=srl.trim(n)local s,t=srl.
strisnum(q)local u,v=srl.
strisbool(q)if s then q=t
elseif u then q=v end;n=""l[p]=
q end;r=srl.sts.key elseif m==
srl.tok.sp or m==srl.tok.nl then
if#n>0 then n=n..m end else n=n..
m end end;g=g+1 end;return l end
--------------------------------

Usage:
s = srl.tostr(t) :: Converts a table to a string
t = srl.fromstr(s) :: Converts a string back to a table.

Formatting and Differences:

  • Strings aren't quoted. The parser will trim any whitespace before and after.
  • Strings that look like numbers will always be turned to numbers.
  • Strings that look like booleans will always be turned to booleans.
  • The input string must have an opening and closing curly bracket.
  • You must include a comma or semicolon after every entry, including the last one.
    • The exception to the rule is the very last closing bracket.
  • nil will be the string "nil" and not actual nil.
  • Keys are always strings.

Example:

{
  a = 10;
  b = 20;
  c = {
    d = 12.34;
    e = some string;
    f = false;
    g = { 
      h = true;
    };
  };
}

It's roughly 600 tokens, and could use further optimization.
But, it works! Now you can store game data in a string, which
costs only one token!

P#131764 2023-07-09 00:24 ( Edited 2023-07-09 01:50)

2

Version 2!
We're down to ~500 tokens!

-- serial
--------------------------------
srl_strisnum=function(a)local b=
tonum(a)local c=tostr(b)return a
==c,b end;srl_strisbool=function
(a)if a=="true"then return true,
true elseif a=="false"then return
true,false end;return false,nil
end;srl_trim=function(a)local d=0;
local e=0;local f;for g=1,#a do
f=sub(a,g,g)if not(f==" "or f==
"\n")then if d==0 then d=g;e=g
else e=g end end end;return sub
(a,d,e)end;srl_tostr=function(h)
local a="{"local i=""for g,j in
pairs(h)do a=a..tostr(g).."="i=
type(j)if i=="number"then a=a..
tostr(j)elseif i=="string"then a
=a..j elseif i=="boolean"then a=
a..(j and"true"or"false")elseif
i=="table"then a=a..srl_tostr(j)
elseif i=="function"then a=a..
"(f)"else a=a.."(r)"end;a=a..";"
end;a=a.."}"return a end;
srl_fromstr=function(a,k)k=k or
1;local l={}local m=""local n=""
local o=false;local p=""local q=
""local r="o";local g=1;while g<=
#a do m=sub(a,g,g)if r=="o" then
if m=="{" then r="k"end elseif r
=="k" then if m=="}" then return
l,g+k-1 elseif m=="=" then if#n>
0 then n=srl_trim(n)p=n;n=""r=
"v" end elseif m=="," or m==";"
then if#n>0 then n=n..m end
elseif m==" " or m=="\n" or m==
"\t"then if#n>0 then n=n..m end
else n=n..m end elseif r=="v"
then if m=="{" then if#n==0 then
l[p],g=srl_fromstr(sub(a,g),g)
end elseif m=="}" then n=n..m
elseif m=="=" then n=n..m
elseif m=="," or m==";" then if
#n>0 then q=srl_trim(n)local s,t
=srl_strisnum(q)local u,v=
srl_strisbool(q)if s then q=t
elseif u then q=v end;n=""l[p]=
q end;r="k" elseif m==" " or m
=="\n"then if#n>0 then n=n..m
end else n=n..m end end;g=g+1
end;return l end
--------------------------------
P#131825 2023-07-11 03:25

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-01 23:18:38 | 0.005s | Q:7