Skip to content

Commit 60d85e1

Browse files
rscharfegitster
authored andcommitted
blame: simplify prepare_lines()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 29aa0b2 commit 60d85e1

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

builtin/blame.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ static void output(struct scoreboard *sb, int option)
17441744
static const char *get_next_line(const char *start, const char *end)
17451745
{
17461746
const char *nl = memchr(start, '\n', end - start);
1747-
return nl ? nl + 1 : NULL;
1747+
return nl ? nl + 1 : end;
17481748
}
17491749

17501750
/*
@@ -1758,25 +1758,19 @@ static int prepare_lines(struct scoreboard *sb)
17581758
const char *end = buf + len;
17591759
const char *p;
17601760
int *lineno;
1761-
int num = 0, incomplete = 0;
1761+
int num = 0;
17621762

1763-
for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
1763+
for (p = buf; p < end; p = get_next_line(p, end))
17641764
num++;
17651765

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

1769-
sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
1770-
lineno = sb->lineno;
1771-
1772-
*lineno++ = 0;
1773-
for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
1768+
for (p = buf; p < end; p = get_next_line(p, end))
17741769
*lineno++ = p - buf;
17751770

1776-
if (incomplete)
1777-
*lineno++ = len;
1771+
*lineno = len;
17781772

1779-
sb->num_lines = num + incomplete;
1773+
sb->num_lines = num;
17801774
return sb->num_lines;
17811775
}
17821776

0 commit comments

Comments
 (0)