[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: io:lines() and \0
- From: Coda Highland <chighland@...>
- Date: Mon, 17 Feb 2014 09:46:10 -0800
On Mon, Feb 17, 2014 at 9:10 AM, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> I just noticed that io:lines() does not cope with \0 in the lines, and thus just returns truncated lines (lua-5.2.3, but legacy 5.1 likewise).
>>
>> May I suggest replacing the call to fgets in src/liolib.c so that we can read lines with \0 data?
>
> At least in Mac OS X and Linux fgets works just fine: it reads bytes
> until it sees \n, as promised in its man page. Unfortunately, fgets does
> not tell you how many bytes it has read and you're left with having to
> call strlen to find this out. I guess we could avoid strlen and use
> memchr instead.
memchr doesn't work if fgets hits EOF before it hits \n, though.
getline(3) looks line it would work, but it's POSIX, not ISO C99, so I
don't think it's supported by MSVC.
I think the only possible implementation that complies with strict ISO
C99, doesn't lose data, and doesn't walk off the end of the buffer is
to do it all by hand, either buffering blocks of the file in memory,
or reading one character at a time.
/s/ Adam