[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: confused by the wierd "q" option of "string.format".
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 8 Jul 2011 10:55:49 -0300
> 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