lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

Rici Lake wrote:
> If the string to be escaped is ISO-8859-1, and you really want to 
> escape high-ascii numerically, just extend the escapes table:
> 
> do
>   local escapes = {["&"] = "&amp;", ["<"] = "&lt;", [">"] = "&gt;"}
>   for i = 128, 255 do escapes[string.char(i)] = "&#"..i..";" end
>   local function escape(c) return escapes[c] end
>   function html_escape(str) return (str:gsub("[&<>\128-\255]", escape)) end
> end

I suggest lazy creation of the non-ascii substitutions since
only a handful of them is ever used in most texts:

do
  local byte = string.byte
  local escapes = setmetatable({["&"]="&amp;", ["<"]="&lt;", [">"]="&gt;"},
    { __index = function(t, c)
        local s = "&#"..byte(c)..";"; t[c] = s; return s; end})
  local function escape(c) return escapes[c] end
  function html_escape(str) return (str:gsub("[&<>\128-\255]", escape)) end
end

[Yes, this works just fine with my patch, too.]

Bye,
     Mike