util.lua 3.18 KB
Newer Older
Kaarle Ritvanen's avatar
Kaarle Ritvanen committed
1 2
--[[
Utility module for Alpine Wall
3
Copyright (C) 2012-2017 Kaarle Ritvanen
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 89 90
function M.copy(var)
   return type(var) == 'table' and M.update({}, var) or var
end
91

92
function M.compare(a, b)
93 94 95 96 97 98
   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
99
      if not M.compare(v, b[k]) then return false end
100 101 102
      table.insert(keys, k)
   end
   for k, v in pairs(b) do
103
      if not M.contains(keys, k) then return false end
104 105 106 107
   end
   return true
end

108 109 110 111 112 113 114 115 116 117 118 119 120
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


121 122 123
function M.quote(s) return '"'..s:gsub('(["\\])', '\\%1')..'"' end


124
function M.printtabulars(tables)
125
   local colwidth = {}
126 127 128
   for i, tbl in ipairs(tables) do
      for j, row in ipairs(tbl) do
	 for k, col in ipairs(row) do
129
	    colwidth[k] = math.max(colwidth[k] or 0, col:len())
130
	 end
131 132
      end
   end
133 134 135 136 137 138
   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
139
	       for l = 1,colwidth[k] - row[k]:len() do io.write(' ') end
140 141 142
	    end
	 end
	 io.write('\n')
143 144 145 146
      end
      io.write('\n')
   end
end
147

148 149
function M.printtabular(tbl) M.printtabulars({tbl}) end

150 151 152

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

153
return M