1 ---------------------------------------------------------------------------
2 -- Copyright Julien Danjou <julien@danjou.info> 2008
3 -- Copyright Mason Larobina <mason.larobina@gmail.com> 2010
4 ---------------------------------------------------------------------------
6 -- Grab environment we need
19 -- Utility module for awful
24 local xml_entity_names
= { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
25 -- Escape a string from XML char.
27 return text
and text
:gsub("['&<>\"]", xml_entity_names
) or nil
30 local xml_entity_chars
= { lt
= "<", gt
= ">", nbsp
= " ", quot
= "\"", apos
= "'", ndash
= "-", mdash
= "-", amp
= "&" };
31 -- Unescape a string from entities.
32 function unescape(text
)
33 return text
and text
:gsub("&(%a+);", xml_entity_chars
) or nil
36 -- Return the difference of another table as a new table.
37 -- (I.e. all elements in the first table but not in the other)
38 function table.difference(t
, other
)
40 for k
, v
in pairs(t
) do
41 if type(k
) == "number" then
43 for _
, ov
in ipairs(other
) do
49 if not found
then rtable
.insert(ret
, v
) end
51 if not other
[k
] then ret
[k
] = v
end
57 -- Join all tables given as parameters.
58 -- This will iterate all tables and insert all their keys into a new table.
59 function table.join(...)
63 for k
, v
in pairs(arg
[i
]) do
64 if type(k
) == "number" then
75 -- Check if a table has an item and return its key.
76 function table.hasitem(t
, item
)
77 for k
, v
in pairs(t
) do
84 -- Get a sorted table with all integer keys from a table
85 function table.keys(t
)
87 for k
, _
in pairs(t
) do
88 rtable
.insert(keys
, k
)
90 rtable
.sort(keys
, function (a
, b
)
91 return type(a
) == type(b
) and a
< b
or false
97 function table.reverse(t
)
99 -- reverse all elements with integer keys
100 for _
, v
in ipairs(t
) do
101 rtable
.insert(tr
, 1, v
)
103 -- add the remaining elements
104 for k
, v
in pairs(t
) do
105 if type(k
) ~= "number" then
113 function table.clone(t
)
115 for k
, v
in pairs(t
) do
121 -- Return true if table `b` is identical to table `a`
122 function table.isclone(a
, b
)
123 if #a
~= #b
then return false end
124 for k
, v
in pairs(a
) do
125 if a
[k
] ~= b
[k
] then return false end
130 -- Remove an element at a given position (or key) in a table and return the
131 -- value that was in that position.
132 function table.pop(t
, k
)
134 if type(k
) == "number" then
142 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80