Re: [PATCH] blame.c: prepare_lines should not call xrealloc for every line
- From
David Kastrup <dak@gnu.org>
- Date
- Feb 4, 2014, 21:48 UTC
- Message-ID
- <87zjm6v99y.fsf@fencepost.gnu.org>
- In-Reply-To
- <xmqqr47i7dt4.fsf@gitster.dls.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 14 quoted lines
> David Kastrup <dak@gnu.org> writes:
>
>> Ok, I now wrote
>>
>> for (p = buf;; num++, p++) {
>> p = memchr(p, '\n', end - p);
>> if (!p)
>> break;
>> }
>
> Looks still wrong (perhaps this is a taste issue).
>
> num++ is not "loop control", but the real action of this
> loop to count lines. It is better left inside.Ok.
> p++ is "loop control", and belongs to the third part of > for(;;).
No, it isn't. The "real" loop control is the p = memchr line. p++ only skips over the newline.
> Isn't the normal continuation condition "p < end"?
memchr returns NULL when not finding anything any more.
Show 10 quoted lines
> so something like
>
> for (p = buf; p < end; p++) {
> p = find the end of this line
> if (!p)
> break;
> num++;
> }
>
> perhaps?Would crash on incomplete last line.
-- David Kastrup