lua-users home
lua-l archive

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


> Of course, in this case, even better would be the original form, with
> [[...]] (and multiple lines!). One size does not fit all needs, but it
> is very easy to use 'gsub' to do the formatting the way it pleases you.

A simple implementation:

----------------------------------------------------------------------
local trans = {
  ["\""] = "\\\"",
  ["\'"] = "\\\'",
  ["\\"] = "\\\\",
  ["\n"] = "\\n",   -- or "\\\n"
  -- add your own preferences
  ["\a"] = "\\a",
}

-- change as needed
local tochange = "[\0-\31\"\'\\\127-\255]"

setmetatable(trans, {__index = function (t, k)
                       t[k] = string.format("\\%03d", string.byte(k))
                       return t[k]
                     end})

function Q (s)
  return '"' .. string.gsub(s, tochange, trans) .. '"'
end
----------------------------------------------------------------------


-- Roberto