util.lua 3.07 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2
--[[
Utility module for Alpine Wall
3
Copyright (C) 2012-2016 Kaarle Ritvanen
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
4
See LICENSE file for license details
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
5 6 7
]]--


8
local M = {}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
9

10
function M.split(s, sep)
11 12 13
   if s == '' then return {} end
   local res = {}
   while true do
14
      local si, ei = s:find(sep, 1, true)
15 16 17 18
      if not si then
	 table.insert(res, s)
	 return res
      end
19 20
      table.insert(res, s:sub(1, si - 1))
      s = s:sub(ei + 1, -1)
21 22 23
   end
end

24
function M.list(var)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
25
   if not var then return {} end
26 27 28
   if type(var) ~= 'table' then return {var} end
   if not next(var) then return {} end
   return var[1] and var or {var}
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
29 30
end

31
function M.listpairs(var) return ipairs(M.list(var)) end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
32

33
function M.filter(var, func)
34 35 36 37 38
   local res = {}
   for i, v in ipairs(var) do if func(v) then table.insert(res, v) end end
   return res
end

39
function M.map(var, func)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
40 41 42 43 44
   local res = {}
   for k, v in pairs(var) do res[k] = func(v) end
   return res
end

45
function M.maplist(var, func)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
46
   if not var then return var end
47
   return M.map(M.list(var), func)
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
48 49
end

50 51
function M.contains(tbl, value)
   for k, v in M.listpairs(tbl) do if v == value then return true end end
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
52 53
   return false
end
54

55
function M.keys(tbl)
56 57 58 59 60
   local res = {}
   for k, v in pairs(tbl) do table.insert(res, k) end
   return res
end

61
function M.values(tbl)
62 63 64 65 66
   local res = {}
   for k, v in pairs(tbl) do table.insert(res, v) end
   return res   
end

67 68
function M.sortedkeys(tbl)
   local res = M.keys(tbl)
69 70 71 72
   table.sort(res)
   return ipairs(res)
end

73 74
function M.extend(tbl1, tbl2)
   for i, var in M.listpairs(tbl2) do table.insert(tbl1, var) end
75
   return tbl1
76
end
77

78
function M.update(tbl1, tbl2)
79 80 81 82
   if tbl2 then for k, v in pairs(tbl2) do tbl1[k] = v end end
   return tbl1
end

83 84 85 86 87
function M.setdefault(t, k, v)
   if t[k] == nil then t[k] = v end
   return t[k]
end

88
function M.copy(tbl) return M.update({}, tbl) end
89

90
function M.compare(a, b)
91 92 93 94 95 96
   local t = type(a)
   if t ~= type(b) then return false end
   if t ~= 'table' then return a == b end

   local keys = {}
   for k, v in pairs(a) do
97
      if not M.compare(v, b[k]) then return false end
98 99 100
      table.insert(keys, k)
   end
   for k, v in pairs(b) do
101
      if not M.contains(keys, k) then return false end
102 103 104 105
   end
   return true
end

106 107 108 109 110 111 112 113 114 115 116 117 118
function M.join(a, sep, b)
   local comps = {}
   local function add(s)
      if not s then return end
      s = tostring(s)
      if s > '' then table.insert(comps, s) end
   end
   add(a)
   add(b)
   if comps[1] then return table.concat(comps, sep) end
end


119
function M.printtabulars(tables)
120
   local colwidth = {}
121 122 123
   for i, tbl in ipairs(tables) do
      for j, row in ipairs(tbl) do
	 for k, col in ipairs(row) do
124
	    colwidth[k] = math.max(colwidth[k] or 0, col:len())
125
	 end
126 127
      end
   end
128 129 130 131 132 133
   for i, tbl in ipairs(tables) do
      for j, row in ipairs(tbl) do
	 for k = 1,#row do
	    if k > 1 then io.write('  ') end
	    io.write(row[k])
	    if k < #row then
134
	       for l = 1,colwidth[k] - row[k]:len() do io.write(' ') end
135 136 137
	    end
	 end
	 io.write('\n')
138 139 140 141
      end
      io.write('\n')
   end
end
142

143 144
function M.printtabular(tbl) M.printtabulars({tbl}) end

145 146 147

function M.printmsg(msg) io.stderr:write(msg..'\n') end

148
return M