Skip to content

Commit

Permalink
blame: simplify prepare_lines()
Browse files Browse the repository at this point in the history
Changing get_next_line() to return the end pointer instead of NULL in
case no newline character is found treats allows us to treat complete
and incomplete lines the same, simplifying the code.  Switching to
counting lines instead of EOLs allows us to start counting at the
first character, instead of having to call get_next_line() first.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Jun 13, 2014
1 parent 29aa0b2 commit 60d85e1
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ static void output(struct scoreboard *sb, int option)
static const char *get_next_line(const char *start, const char *end)
{
const char *nl = memchr(start, '\n', end - start);
return nl ? nl + 1 : NULL;
return nl ? nl + 1 : end;
}

/*
Expand All @@ -1758,25 +1758,19 @@ static int prepare_lines(struct scoreboard *sb)
const char *end = buf + len;
const char *p;
int *lineno;
int num = 0, incomplete = 0;
int num = 0;

for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
for (p = buf; p < end; p = get_next_line(p, end))
num++;

if (len && end[-1] != '\n')
incomplete++; /* incomplete line at the end */
sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));

sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
lineno = sb->lineno;

*lineno++ = 0;
for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
for (p = buf; p < end; p = get_next_line(p, end))
*lineno++ = p - buf;

if (incomplete)
*lineno++ = len;
*lineno = len;

sb->num_lines = num + incomplete;
sb->num_lines = num;
return sb->num_lines;
}

Expand Down

0 comments on commit 60d85e1

Please sign in to comment.