awful.util: optimize escape/unescape

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Matthew Wild 2009-01-28 10:05:26 +01:00 committed by Julien Danjou
parent 62bbc395ab
commit 798943af31
1 changed files with 5 additions and 16 deletions

View File

@ -94,29 +94,18 @@ end
-- Useful to set raw text in textbox. -- Useful to set raw text in textbox.
-- @param text Text to escape. -- @param text Text to escape.
-- @return Escape text. -- @return Escape text.
local xml_entity_names = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
function escape(text) function escape(text)
if text then return text and text:gsub("['&<>\"]", xml_entity_names) or nil
text = text:gsub("&", "&amp;")
text = text:gsub("<", "&lt;")
text = text:gsub(">", "&gt;")
text = text:gsub("'", "&apos;")
text = text:gsub("\"", "&quot;")
end
return text
end end
--- Unescape a string from entities. --- Unescape a string from entities.
-- @param text Text to unescape. -- @param text Text to unescape.
-- @return Unescaped text. -- @return Unescaped text.
local xml_entity_chars = { lt = "<", gt = ">", nbsp = " ", quot = "\"", apos = "'", ndash = "-", mdash = "-", amp = "&" };
function unescape(text) function unescape(text)
if text then return text and text:gsub("&(%a+);", xml_entity_chars) or nil
text = text:gsub("&amp;", "&")
text = text:gsub("&lt;", "<")
text = text:gsub("&gt;", ">")
text = text:gsub("&apos;", "'")
text = text:gsub("&quot;", "\"")
end
return text
end end
--- Check if a file is a Lua valid file. --- Check if a file is a Lua valid file.