[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: confused by the wierd "q" option of "string.format".
- From: Sam Roberts <vieuxtech@...>
- Date: Thu, 7 Jul 2011 13:36:21 -0700
>> however, under some special cases (e.g. debugging output), I usually need
>> to print the result on a limited screen area and hope not to make mess of
>> other outputs.
%q does what it says it does, but it doesn't give a quoted printable
version of a string in the form I want, either. I use this to quote
strings. Note that it treats \n specially, but not \a\f\t\v\r..etc.
(they all get transformed to decimal escapes).
function quote(_)
local fmt = string.format
local _ = fmt("%q", _)
_ = string.gsub(_, "\\\n", "\\n")
_ = string.gsub(_, "[%z\1-\31\127-\255]", function (x)
return fmt("\\%03d",string.byte(x))
end)
return _
end
s = "---null=\0,space=\32,nl=\n,quote=\"/\',del=\127,bs=\\,high=\200,low=\6,vt=\v---"
print(string.format("%%q=<%q>\nquote=<%s>", s, quote(s)))
Output:
% lua q.lua
%q=<"---null=\000,space= ,nl=\
,quote=\"/',del=,bs=\\,high=�,low=,vt=
---">
quote=<"---null=\000,space=
,nl=\n,quote=\"/',del=\127,bs=\\,high=\200,low=\006,vt=\011---">
Cheers,
Sam