lua-users home
lua-l archive

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


> local png_file_content = 'A\0\r\nB'  -- any binary data here
> local insert_this_text_into_your_lua_source =
>    'local png = '..('%q'):format(png_file_content)..'\n'

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")

This is for Lua 5.1. I think there were some changes in %q behavior in Lua 5.2.

Paul.

On Fri, Nov 2, 2012 at 1:23 PM, Egor Skriptunoff
<egor.skriptunoff@gmail.com> wrote:
> On 11/2/12, Jim Studt <jim@studt.net> wrote:
>> It would be nice to have a long literal that was actually literal
>> for when you need precise values. e.g. you might want
>> to embed a PNG, which has the offending pair in bytes 5 and 6,
>> in a machine generated source file.
>
> Use this "preprocessor" (%q format option rules!):
>
> local png_file_content = 'A\0\r\nB'  -- any binary data here
> local insert_this_text_into_your_lua_source =
>    'local png = '..('%q'):format(png_file_content)..'\n'
>
> :-)
>