1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-11-05 03:39:02 -08:00

fix a certain case in serialize.lua

this should actually check if the whole string
matches [A-Za-z_][0-9A-Za-z_]*
but thats for another time
This commit is contained in:
Connor Olding 2016-01-19 16:30:00 -08:00
parent 617a10cba3
commit e6b5fe590f

View file

@ -16,7 +16,9 @@ local function kill_bom(s)
end end
local function sanitize(v) local function sanitize(v)
return type(v) == 'string' and strfmt('%q', v) or tostring(v) local force = type(v) == 'string' and v:sub(1, 1):match('%d')
force = force and true or false
return type(v) == 'string' and strfmt('%q', v) or tostring(v), force
end end
local function _serialize(value, writer, level) local function _serialize(value, writer, level)
@ -25,15 +27,16 @@ local function _serialize(value, writer, level)
local indent = strrep('\t', level) local indent = strrep('\t', level)
writer('{\n') writer('{\n')
for key,value in opairs(value) do for key,value in opairs(value) do
local sane = sanitize(key) local sane, force = sanitize(key)
local keyval = sane == '"'..key..'"' and key or '['..sane..']' local keyval = (sane == '"'..key..'"' and not force) and key or '['..sane..']'
writer(indent..keyval..' = ') writer(indent..keyval..' = ')
_serialize(value, writer, level + 1) _serialize(value, writer, level + 1)
writer(',\n') writer(',\n')
end end
writer(strrep('\t', level - 1)..'}') writer(strrep('\t', level - 1)..'}')
else else
writer(sanitize(value)) local sane, force = sanitize(value)
writer(sane)
end end
end end