[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: io:lines() and \0
- From: Craig Barnes <craigbarnes85@...>
- Date: Mon, 17 Feb 2014 17:08:18 +0000
> 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):
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
...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.