Skip to content

Commit

Permalink
blame: factor out get_next_line()
Browse files Browse the repository at this point in the history
Move the code for finding the start of the next line into a helper
function in order to reduce duplication.

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 e156455 commit 29aa0b2
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,12 @@ 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;
}

/*
* To allow quick access to the contents of nth line in the
* final image, prepare an index in the scoreboard.
Expand All @@ -1754,15 +1760,8 @@ static int prepare_lines(struct scoreboard *sb)
int *lineno;
int num = 0, incomplete = 0;

for (p = buf;;) {
p = memchr(p, '\n', end - p);
if (p) {
p++;
num++;
continue;
}
break;
}
for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
num++;

if (len && end[-1] != '\n')
incomplete++; /* incomplete line at the end */
Expand All @@ -1771,15 +1770,8 @@ static int prepare_lines(struct scoreboard *sb)
lineno = sb->lineno;

*lineno++ = 0;
for (p = buf;;) {
p = memchr(p, '\n', end - p);
if (p) {
p++;
*lineno++ = p - buf;
continue;
}
break;
}
for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
*lineno++ = p - buf;

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

0 comments on commit 29aa0b2

Please sign in to comment.