lua-users home
lua-l archive

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


> I just noticed that io:lines() does not cope with \0 in the lines, and thus
> just returns truncated lines
> ...
> in my option library foundations should just work, and not silently
> discard some bits and bytes. A line is a line, no matter how many \0 are in
> there until the next \n-newline. And the Lua manual points out Lua strings
> are \0-save.

This seems to be the definitive answer (from ~8 years ago):

    http://lua-users.org/lists/lua-l/2006-01/msg00641.html

I think that's a fair explanation, since it can be replaced with:

    local file = assert(io.open(filename))
    local text = assert(file:read("*a"))
    for line in text:gmatch "([^\n]*)\n" do
        -- print(line)
    end

...which handles embedded zeros at the cost of reading the entire
thing into memory.

It would be helpful if the limitation was mentioned in the
reference manual though. I spent quite a while trying to
work out what was happening the first time I encoutered it.