diff options
| author | OpportunityLiu <[email protected]> | 2019-07-26 20:55:55 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-28 14:45:01 +0800 |
| commit | bfdfbe9b260824a9254a7be7bb7ffe9f0e9cf3d5 (patch) | |
| tree | c9172dc2cdedf961281b375082e5b99cad9550db | |
| parent | ef1b33a90b2fcb87c070de07d22ed89d830fcbd0 (diff) | |
optimize _maketable
| -rw-r--r-- | xmake/core/base/serialize.lua | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/xmake/core/base/serialize.lua b/xmake/core/base/serialize.lua index 59feae24a..1a9294a09 100644 --- a/xmake/core/base/serialize.lua +++ b/xmake/core/base/serialize.lua @@ -101,12 +101,6 @@ function serialize._maketable(object, opt, level, path, reftab) isarr = false end - -- make indent - local indent = "" - if opt.indent then - indent = string.rep(opt.indent, level) - end - -- make body local bodystrs = {} if isarr then @@ -114,38 +108,40 @@ function serialize._maketable(object, opt, level, path, reftab) bodystrs[i] = serialized[i] or "nil" end else - local con = opt.indent and " = " or "=" + local dformat = opt.indent and "%s = %s" or "%s=%s" + local sformat = opt.indent and "[%q] = %s" or "[%q]=%s" + local nformat = opt.indent and "[%s] = %s" or "[%s]=%s" for k, v in pairs(serialized) do + local format -- serialize key if type(k) == "string" then if keywords:has(k) or not k:match("^[%a_][%w_]*$") then - k = string.format("[%q]", k) + format = sformat + else + format = dformat end else -- type(k) == "number" - local nval, err = serialize._makedefault(k, opt) - if err ~= nil then - return nil, err - end - k = string.format("[%s]", nval) + format = nformat end -- concat k = v - table.insert(bodystrs, k .. con .. v) + table.insert(bodystrs, string.format(format, k, v)) end end - -- make head - local headstr = opt.indent and ("{\n" .. indent .. opt.indent) or "{" - - -- make tail - local tailstr + -- make head and tail + local headstr, bodysep, tailstr if opt.indent then - tailstr = "\n" .. indent .. "}" + local indent = "\n" .. string.rep(opt.indent, level) + tailstr = indent .. "}" + indent = indent .. opt.indent + headstr = "{" .. indent + bodysep = "," .. indent else - tailstr = "}" + headstr, bodysep, tailstr = "{", ",", "}" end -- concat together - return headstr .. table.concat(bodystrs, opt.indent and (",\n" .. indent .. opt.indent) or ",") .. tailstr + return headstr .. table.concat(bodystrs, bodysep) .. tailstr end function serialize._makefunction(func, opt) |
