lua-users home
lua-l archive

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


Am 03.04.2010 08:26, schrieb M Joonas Pihlaja:
I want to embed a huge blob of binary data into a lua script and not
put it all on one line (that makes my editor cry.)
As far as I can tell, neither Lua nor Scite has Problems with (very) long lines.
Try the following script script to check your editors and luas capabilities:

local lua=io.open("longstrings.lua","w")
local len=1
while len<=8*1024*1024 do
    lua:write("str=\"",string.rep("X",len),"\"\nprint(#str)\n")
    len=len*2
end
lua:close()

The created line opens with scite and runs with lua.

>lua -e "io.stdout:setvbuf 'no'" "longstrings.lua"
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608

(I stopped a 8MB string, but I believe, you even could try more)

And as I dont believe, you will enter this lot of binary data by hand, but will use some loaded or generated data as source, why dont you put these into a own files?
Example:

Generating the file

local lua=io.open("bindata.lua","w")
lua:write("return \"")
-- whatever you do to generate the binary data, this is just an example
for i=1,1000 do
    lua:write(string.format("\\%03d",(math.sin(i/1000)+1)*127))
end
lua:write("\"\n")
lua:close()

The file then looks like (trunkcated here in the mail(

return "\127\127\127\127\127\127 ....\233\233\233\233\233\233\233\233\233"

then you can use it with

local bindata=require"bindata"
print(#bindata)
for i=1,#bindata do
    print(i,string.byte(bindata,i))
end

There will be no need to modify bindata.lua in the editor.

Hope, this gives you an idea
Regards Jørgrn