lua-users home
lua-l archive

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


Hi Egor,

> %q translates all control characters except '\n' into slashed form.
> EOF char (ASCII 0x1A) will never appear in %q-formatted output.
> Please show an example - how pure %q output may "break a content"?

You're right about escaping \n; I was thinking about the case when you
need your %q output to be on one line. EOF breaks the content this
way:

-- eof.lua
print(("print %q"):format("1\n2\0263"))

> lua eof.lua | lua

returns "unfinished string near '<eof>'".

while escaping EOF with this:

print((("print %q"):format("1\n2\0263"):gsub("\026","\\026")))

prints expected string back.

Paul.

On Fri, Nov 2, 2012 at 3:11 PM, Egor Skriptunoff
<egor.skriptunoff@gmail.com> wrote:
> On 11/2/12, Paul K <paulclinger@yahoo.com> wrote:
>> This should work, but careful with newlines and EOF codes, as those
>> may break your content. I used something like this in Serpent
>> serializer:
>> ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026")
>
> %q translates all control characters except '\n' into slashed form.
> EOF char (ASCII 0x1A) will never appear in %q-formatted output.
> 0x0A encodes to backslash + LF, but even after newline
> character modified it correctly decodes back to 0x0A,
> as any combination backslash + LF/CRLF/CR/LFCR decodes into 0x0A.
> IMHO, generated Lua code is safe even without these gsub-s.
> And of course, png files should be treated as binary ("rb", "wb" modes)
> to ignore magic of EOF chars.
>
> Please show an example - how pure %q output may "break a content"?
>