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.
This commit is contained in:
Adrian C. (anrxc) 2009-08-03 04:29:06 +02:00
parent 047dba0e5d
commit 09fda0ab05
1 changed files with 13 additions and 13 deletions

View File

@ -31,7 +31,7 @@ function format(format, args)
-- Format a string -- Format a string
for var, val in pairs(args) do for var, val in pairs(args) do
format = string.gsub(format, "$"..var, val) format = string.gsub(format, "$" .. var, val)
end end
-- Return formatted string -- Return formatted string
@ -49,7 +49,7 @@ function padd(number, padding)
for i=1, padding do for i=1, padding do
if math.floor(number/math.pow(10,(i-1))) == 0 then if math.floor(number/math.pow(10,(i-1))) == 0 then
s = "0"..s s = "0" .. s
end end
end end
@ -88,28 +88,28 @@ function bytes_to_string(bytes, sec, padding)
if padding then if padding then
bytes = padd(bytes*10, padding+1) 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 end
if sec then if sec then
return tostring(bytes)..signs[sign].."ps" return tostring(bytes) .. signs[sign] .. "ps"
else else
return tostring(bytes)..signs[sign] return tostring(bytes) .. signs[sign]
end end
end end
-- }}} -- }}}
--{{{ Escape a string --{{{ Escape a string
function escape(text) function escape(text)
if text then local xml_entities = {
text = text:gsub("&", "&") ["\""] = """,
text = text:gsub("<", "&lt;") ["&"] = "&amp;",
text = text:gsub(">", "&gt;") ["'"] = "&apos;",
text = text:gsub("'", "&apos;") ["<"] = "&lt;",
text = text:gsub("\"", "&quot;") [">"] = "&gt;"
end }
return text return text and text:gsub("[\"&'<>]", xml_entities)
end end
-- }}} -- }}}
-- }}} -- }}}