From 09fda0ab05c066a8f630e1d7a36f401bbc0ca68e Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Mon, 3 Aug 2009 04:29:06 +0200 Subject: Rewrite of the escape helper. It is simillar to the awful.util.escape now, using a table which we could expand (and rename) with other unwated characters if it comes to that. I saw awesome break on many occasions because of encoding problems. --- helpers.lua | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'helpers.lua') diff --git a/helpers.lua b/helpers.lua index 875c14b..67319ed 100644 --- a/helpers.lua +++ b/helpers.lua @@ -31,7 +31,7 @@ function format(format, args) -- Format a string for var, val in pairs(args) do - format = string.gsub(format, "$"..var, val) + format = string.gsub(format, "$" .. var, val) end -- Return formatted string @@ -49,7 +49,7 @@ function padd(number, padding) for i=1, padding do if math.floor(number/math.pow(10,(i-1))) == 0 then - s = "0"..s + s = "0" .. s end end @@ -88,28 +88,28 @@ function bytes_to_string(bytes, sec, padding) if padding then bytes = padd(bytes*10, padding+1) - bytes = bytes:sub(1, bytes:len()-1).."."..bytes:sub(bytes:len()) + bytes = bytes:sub(1, bytes:len()-1) .. "." .. bytes:sub(bytes:len()) end if sec then - return tostring(bytes)..signs[sign].."ps" + return tostring(bytes) .. signs[sign] .. "ps" else - return tostring(bytes)..signs[sign] + return tostring(bytes) .. signs[sign] end end -- }}} --{{{ Escape a string function escape(text) - if text then - text = text:gsub("&", "&") - text = text:gsub("<", "<") - text = text:gsub(">", ">") - text = text:gsub("'", "'") - text = text:gsub("\"", """) - end - - return text + local xml_entities = { + ["\""] = """, + ["&"] = "&", + ["'"] = "'", + ["<"] = "<", + [">"] = ">" + } + + return text and text:gsub("[\"&'<>]", xml_entities) end -- }}} -- }}} -- cgit v1.2.3