【步兵 Lua】用github 淘 table2string

好久没写文章了,随便写写。

学会搜索

其实很多通用的功能,已经有很多人实现了,只不过方法可能不一样,然而不少人像我一样,
秉承程序员最崇高的开源精神,上传到了「我的代码仓库」github上,这个收回只要你能想到
你能按照命名规格想到这个功能的函数名,那么久不妨去github上搜一搜。

下边以table2string为例:

  1. 输入 “table2string” 然后search in all git,
  2. 选择标签code(也就是代码文件)
  3. Languages 选择 lua
  4. 开始不断的洗澡 满足需求的代码

最后翻了几页筛选到了下边的文件
git地址
https://github.com/BaBaHu/qipai023_src/blob/05deb556f50d526b60e1204fd8edb4b00702e23d/app/base/functions.lua
然而最有意思的是,明明这个项目里有这个函数,他自己却没有用。
仿佛是特意为我准备的??

然后往copy下来稍微改了改,就成了我代码库中一个不错的通用函数(跑

源码

function table2string(t, isFormat)
   local mark = {}
   local assign = {}
   local getFS = nil
   if isFormat then
       getFS = function(len)
           local ret = ""
           while len > 1 do
               ret = ret .. "    "
               len = len - 1
           end
           if len >= 1 then
               ret = ret .. "    " --"├┄┄"
           end
           return ret
       end
   end

   local function table2str(t, parent, deep)
       deep = deep or 0
       mark[t] = parent
       local ret = {}
       for f, v in pairs(t) do
           local k = type(f) == "number" and "[" .. f .. "]" or tostring(f)
           local dotkey = parent .. (type(f) == "number" and k or "." .. k)
           local t = type(v)
           if t == "userdata" or t == "function" or t == "thread" or t == "proto" or t == "upval" then
               table.insert(ret, string.format("%s=%q", k, tostring(v)))
           elseif t == "table" then
               if mark[v] then
                   table.insert(assign, dotkey .. "=" .. mark[v])
               else
                   table.insert(ret, string.format("%s=%s", k, table2str(v, dotkey, deep + 1)))
               end
           elseif t == "string" then
               table.insert(ret, string.format("%s=%q", k, v))
           elseif t == "number" then
               if v == math.huge then
                   table.insert(ret, string.format("%s=%s", k, "math.huge"))
               elseif v == -math.huge then
                   table.insert(ret, string.format("%s=%s", k, "-math.huge"))
               else
                   table.insert(ret, string.format("%s=%s", k, tostring(v)))
               end
           else
               table.insert(ret, string.format("%s=%s", k, tostring(v)))
           end
       end
       if isFormat then
           return "{\n" .. getFS(deep + 1) .. table.concat(ret, ",\n" .. getFS(deep + 1)) .. "\n" .. getFS(deep) .. "}"
       else
           return "{" .. table.concat(ret, ",") .. "}"
       end
   end

   if type(t) == "table" then
       if t.__tostring then
           return tostring(t)
       end
       local str = string.format("%s%s", table2str(t, "_"), table.concat(assign, " "))
       return str
   else
       return tostring(t)
   end
end
function string2table(lua)
   local t = type(lua)
   if t == "nil" or lua == "" then
       return nil
   elseif t == "number" or t == "string" or t == "boolean" then
       lua = tostring(lua)
   else
       error("string2table: can not transform a " .. t .. " type.")
   end
   lua = "return " .. lua
   local func = nil
   if _G._VERSION > "Lua 5.1" then
       func = load(lua)
   else
       func = loadstring(lua)
   end

   if func == nil then
       return nil
   end
   return func()
end
local t = {
   a = {1, 2, 3},
   b = 4,
   c = {x = "5", y = 6},
   d = true
}
print(table2string(t)) -- print: {a={[1]=1,[2]=2,[3]=3},b=4,c={y=6,x="5"},d=true}
print(table2string(string2table('{a={[1]=1,[2]=2,[3]=3},b=4,c={y=6,x="5"},d=true}'), true))
-- print:
-- {
--     a={
--         [1]=1,
--         [2]=2,
--         [3]=3
--     },
--     b=4,
--     c={
--         y=6,
--         x="5"
--     },
--     d=true
-- }

你可能感兴趣的:(经验总结)