utils.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. -- As a more advanced solution, we can write an iterator that traverses a table following the order of its keys.
  2. -- An optional parameter f allows the specification of an alternative order. It first sorts the keys into an array,
  3. -- and then iterates on the array. At each step, it returns the key and value from the original table:
  4. function pairsByKeys (t, f)
  5. local a = {}
  6. for n in pairs(t) do table.insert(a, n) end
  7. table.sort(a, f)
  8. local i = 0 -- iterator variable
  9. local iter = function () -- iterator function
  10. i = i + 1
  11. if a[i] == nil then
  12. return nil
  13. else
  14. return a[i], t[a[i]]
  15. end
  16. end
  17. return iter
  18. end
  19. -- Compatibility: Lua-5.1
  20. function split(str, pat)
  21. local t = {} -- NOTE: use {n = 0} in Lua-5.0
  22. local fpat = "(.-)" .. pat
  23. local last_end = 1
  24. local s, e, cap = str:find(fpat, 1)
  25. while s do
  26. if s ~= 1 or cap ~= "" then
  27. table.insert(t,cap)
  28. end
  29. last_end = e+1
  30. s, e, cap = str:find(fpat, last_end)
  31. end
  32. if last_end <= #str then
  33. cap = str:sub(last_end)
  34. table.insert(t, cap)
  35. end
  36. return t
  37. end
  38. -- serialize table
  39. local DUMP_IGNORE = {os = 1, io = 1, table = 1, _VERSION = 1, math = 1, love = 1, string = 1, package = 1, _G = 1, DUMP_IGNORE = 1, ndump = 1, xpcall = 1, unpack = 1, type = 1, require = 1, setmetatable = 1, next = 1, pairs = 1, ipairs = 1, dofile = 1, collectgarbage = 1, load = 1, loadfile = 1, loadstring = 1, module = 1, pcall = 1, gcinfo = 1, getmetatable = 1, error = 1, debug = 1, coroutine = 1, assert = 1, tonumber = 1, tostring = 1, setfenv = 1, arg = 1, argv = 1, dumpAll = 1, dumpUserData = 1, getfenv = 1, newproxy = 1, select = 1}
  40. function ndump(object, map, visited, prefix, ignoredMap)
  41. map = map or {}
  42. ignoredMap = ignoredMap or {}
  43. visited = visited or {}
  44. if object ~= nil and visited[object] == nil then
  45. visited[object] = true
  46. for k, v in pairs(object) do
  47. local useMap = not prefix and DUMP_IGNORE[tostring(k)] ~= nil and ignoredMap or map
  48. local child = nil
  49. local vtype = type(v)
  50. k = tostring(k)
  51. if vtype == "table" then
  52. child = v
  53. v = "{}"
  54. elseif vtype == "string" then
  55. v = "\"" .. string.gsub(string.gsub(v, "\\", "\\\\"),"\n", "\\n") .. "\""
  56. elseif vtype == "function" then
  57. v = (prefix and (prefix .. "[\"" .. k .. "\"]") or k) .. " or function() end"
  58. elseif vtype == "userdata" then
  59. v = "{} --[[ " .. tostring(v) .. "]]--"
  60. end
  61. table.insert(useMap, (prefix and (prefix .. "[\"" .. k .. "\"]") or k) .. " = " .. tostring(v))
  62. if child then
  63. ndump(child, useMap, visited, (prefix and (prefix .. "[\"" .. k .. "\"]") or k))
  64. end
  65. end
  66. end
  67. if not prefix then
  68. local sf = function(a, b)
  69. return string.lower(a) < string.lower(b)
  70. end
  71. table.sort(map, sf)
  72. table.sort(ignoredMap, sf)
  73. return table.concat(map, "\n"), "-- " .. table.concat(ignoredMap, "\n-- ")
  74. end
  75. end