1. --[[
    2. print_dump是一个用于调试输出数据的函数,能够打印出nil,boolean,number,string,table类型的数据,以及table类型值的元表
    3. 参数data表示要输出的数据
    4. 参数showMetatable表示是否要输出元表
    5. 参数lastCount用于格式控制,用户请勿使用该变量
    6. ]]
    7. function print_dump(data, showMetatable, lastCount)
    8. if type(data) ~= "table" then
    9. --Value
    10. if type(data) == "string" then
    11. io.write("\"", data, "\"")
    12. else
    13. io.write(tostring(data))
    14. end
    15. else
    16. --Format
    17. local count = lastCount or 0
    18. count = count + 1
    19. io.write("{\n")
    20. --Metatable
    21. if showMetatable then
    22. for i = 1,count do
    23. io.write("\t")
    24. end
    25. local mt = getmetatable(data)
    26. io.write("\"__metatable\" = ")
    27. print_dump(mt, showMetatable, count) -- 如果不想看到元表的元表,可将showMetatable处填nil
    28. io.write(",\n") --如果不想在元表后加逗号,可以删除这里的逗号
    29. end
    30. --Key
    31. for key,value in pairs(data) do
    32. for i = 1,count do
    33. io.write("\t")
    34. end
    35. if type(key) == "string" then
    36. io.write("\"", key, "\" = ")
    37. elseif type(key) == "number" then
    38. io.write("[", key, "] = ")
    39. else
    40. io.write(tostring(key))
    41. end
    42. print_dump(value, showMetatable, count) -- 如果不想看到子table的元表,可将showMetatable处填nil
    43. io.write(",\n") --如果不想在table的每一个item后加逗号,可以删除这里的逗号
    44. end
    45. --Format
    46. for i = 1,lastCount or 0 do
    47. io.write("\t")
    48. end
    49. io.write("}")
    50. end
    51. --Format
    52. if not lastCount then
    53. io.write("\n")
    54. end
    55. end
    --->字符串分割
    ---按照sign将string分割为table
    function string:split(sign)
        local tab = {}
        self:gsub('[^' .. sign .. ']+', function(f)
                table.insert(tab, f)
            end)
        return tab
    end
    --->删除字符串空格
    function string:trim()
        return self:gsub('%s', '')
    end
    
    --->万能连接
    ---搭配万能print及万能toast使用
    function connectContent(...)
        local content = { ... }
        local function tabToStr(t)
            local str = {}
            local function tab(_t, num)
                num = num + 1
                for k, v in pairs(_t) do
                    local value = tostring(v)
                    local _type = type(v)
                    if _type == "table" and k ~= _G and not v.package then
                        if not next(v) then
                            str[#str + 1] = num == 0 and '' or ('\t'):rep(num) .. '[' .. tostring(k) .. '](table) = {}\r'
                        else
                            str[#str + 1] = num == 0 and '' or ('\t'):rep(num) .. '[' .. tostring(k) .. '](table) = {\r'
                            tab(v, num)
                            str[#str + 1] = num == 0 and '' or ('\t'):rep(num) .. '}\r'
                        end
                    else
                        if _type == "function" then
                            value = value:split(': ')[2]
                        end
                        str[#str + 1] = num == 0 and '' or ('\t'):rep(num) .. '[' .. tostring(k) .. '](' .. _type .. ') = ' .. value .. '\r'
                    end
                end
            end
            if next(t) then
                str[#str + 1] = '\r◇ ' .. tostring(t):split(': ')[2] .. '(table) = {\r'
                tab(t, 0)
                str[#str + 1] = '}\r'
            else
                str[#str + 1] = '◇ ' .. tostring(t):split(': ')[2] .. '(table) = {}\r'
            end
            str = table.concat(str)
            return str
        end
        for k, v in pairs(content) do
            if type(v) == "table" then
                content[k] = tabToStr(v)
            else
                content[k] = tostring(v)
            end
        end
        content = table.concat(content)
        return content
    end
    --->万能print
    function log(...)
        local content = connectContent(...)
        print(content)
    end