diff options
Diffstat (limited to 'nerv/init.lua')
-rw-r--r-- | nerv/init.lua | 75 |
1 files changed, 34 insertions, 41 deletions
diff --git a/nerv/init.lua b/nerv/init.lua index 4d7b687..da7df29 100644 --- a/nerv/init.lua +++ b/nerv/init.lua @@ -98,24 +98,27 @@ function nerv.class(tname, parenttname) end function table.val_to_str(v) - if "string" == type(v) then - v = string.gsub(v, "\n", "\\n") - if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then - return "'" .. v .. "'" + if "string" == type(v) then + v = string.gsub(v, "\n", "\\n") + if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then + return "'" .. v .. "'" + end + return '"' .. string.gsub(v,'"', '\\"') .. '"' + else + return "table" == type(v) and table.tostring(v) or + (("number" == type(v) or + "string" == type(v) or + "boolean" == type(v)) and tostring(v)) or + nil -- failed to serialize end - return '"' .. string.gsub(v,'"', '\\"') .. '"' - else - return "table" == type(v) and table.tostring(v) or - tostring(v) - end end function table.key_to_str (k) - if "string" == type(k) and string.match(k, "^[_%a][_%a%d]*$") then - return k - else - return "[" .. table.val_to_str(k) .. "]" - end + if "string" == type(k) and string.match(k, "^[_%a][_%a%d]*$") then + return k + else + return "[" .. table.val_to_str(k) .. "]" + end end --- Get the string representation of a table, which can be executed as a valid @@ -124,18 +127,18 @@ end -- @return the string representation which will result in a Lua table entity -- when evaluated function table.tostring(tbl) - local result, done = {}, {} - for k, v in ipairs(tbl) do - table.insert(result, table.val_to_str(v)) - done[k] = true - end - for k, v in pairs(tbl) do - if not done[k] then - table.insert(result, - table.key_to_str(k) .. "=" .. table.val_to_str(v)) + local result, done = {}, {} + for k, v in ipairs(tbl) do + table.insert(result, table.val_to_str(v)) + done[k] = true end - end - return "{" .. table.concat(result, ",") .. "}" + for k, v in pairs(tbl) do + if not done[k] then + table.insert(result, + table.key_to_str(k) .. "=" .. table.val_to_str(v)) + end + end + return "{" .. table.concat(result, ",") .. "}" end --- Get the class by name. @@ -332,27 +335,17 @@ function nerv.print_usage(options) (opt_full and '--' .. opt_full) or "", (opt_short and '-' .. opt_short) or "", opt_type, - v.default or "", + (v.default ~= nil and tostring(v.default)) or "", v.desc or "") end nerv.printf("\n") end --- function nerv.copy_file(fname1, fname2) --- local fin, fout, err --- fin, err = io.open(fname1, "r") --- if fin then --- fout, err = io.open(fname2, "w") --- end --- if not (fin and fout) then --- nerv.error("[copy] from %s to %s: %s", fname1, fname2, err) --- end --- while true do --- local b = fin:read(1024) --- if b == nil then break end --- fout:write(b) --- end --- end +function table.extend(tbl1, tbl2) + for _, v in ipairs(tbl2) do + table.insert(tbl1, v) + end +end -- the following lines trigger the initialization of basic modules |