diff options
| author | ruki <[email protected]> | 2018-12-05 23:23:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-12-05 14:23:57 +0800 |
| commit | 212e85ed60305fa8c3e74261835ecaa32f881232 (patch) | |
| tree | fd08ae45a2fcdf6bfd4aad6abf8e0f9c46ef67eb /xmake/core/base/table.lua | |
| parent | b7e17b562985e6854ddcf67c5524da9c5d045875 (diff) | |
move table.makestr to string.serialize
Diffstat (limited to 'xmake/core/base/table.lua')
| -rw-r--r-- | xmake/core/base/table.lua | 149 |
1 files changed, 2 insertions, 147 deletions
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 6b600ad1e..33f2ce587 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -25,105 +25,6 @@ -- define module: table local table = table or {} --- make string with the level -function table._makestr(self, deflate, serialize, level) - if type(self) == "string" then - return serialize and string.format("%q", self) or self - elseif type(self) == "boolean" or type(self) == "number" then - return tostring(self) - elseif not serialize and type(self) == "table" and (getmetatable(self) or {}).__tostring then - return tostring(self) - elseif type(self) == "table" then - - -- make head - local s = "" - if deflate then - s = s .. "{" - else - s = s .. "\n" - for l = 1, level do - s = s .. " " - end - s = s .. "{\n" - end - - -- make body - local i = 0 - for k, v in pairs(self) do - - if deflate then - s = s .. (i ~= 0 and "," or "") - else - for l = 1, level do - s = s .. " " - end - if i == 0 then - s = s .. " " - else - s = s .. ", " - end - end - - -- make key = value - if type(k) == "string" then - if serialize then - k = string.format("[%q]", k) - end - if deflate then - s = s .. k .. "=" - else - s = s .. k .. " = " - end - end - local substr, errors = table._makestr(v, deflate, serialize, level + 1) - if substr == nil then - return nil, errors - end - s = s .. substr - - if not deflate then - s = s .. "\n" - end - i = i + 1 - end - - -- make tail - if not deflate then - for l = 1, level do - s = s .. " " - end - end - s = s .. "}" - return s - elseif serialize and type(self) == "function" then - return string.format("%q", string.dump(self)) - elseif serialize then - return nil, "cannot serialize object: " .. type(self) - elseif self ~= nil then - return "<" .. tostring(self) .. ">" - else - return "nil" - end -end - --- load table from string in table -function table._loadstr(self) - -- only load luajit function data: e.g. "\27LJ\2\0\6=stdin" - if type(self) == "string" and self:startswith("\27LJ") then - return loadstring(self) - elseif type(self) == "table" then - for k, v in pairs(self) do - local value, errors = table._loadstr(v) - if value ~= nil then - self[k] = value - else - return nil, errors - end - end - end - return self -end - -- clear the table function table.clear(self) for k in next, self do @@ -290,59 +191,13 @@ function table.is_dictionary(dict) end -- dump table -function table.dump(self, deflate, serialize) - local str = table.makestr(self, deflate, serialize) +function table.dump(self, deflate) + local str = string.dump(self, deflate) if str then io.write(str) end end --- make string from the given table --- --- @param deflate deflate empty characters --- @param serialize make string which can be deserialized, we can use table.loadstr to load it --- --- @return string, errors --- -function table.makestr(self, deflate, serialize) - return table._makestr(self, deflate, serialize, 0) -end - --- load table from the serialized string --- --- @param str the serialized string --- --- @return table, errors --- -function table.loadstr(str) - - -- load table as script - local result = nil - local script, errors = loadstring("return " .. str) - if script then - - -- load object - local ok, object = pcall(script) - if ok and object then - result = object - elseif object then - -- error - errors = object - else - -- error - errors = string.format("cannot deserialize string: %s", str) - end - end - - -- load function from string in table - if result then - result, errors = table._loadstr(result) - end - - -- ok? - return result, errors -end - -- unwrap object if be only one function table.unwrap(object) if type(object) == "table" then |
