Skip to content

Commit 8d87e35

Browse files
committed
Merge branch 'rs/blame-refactor'
* rs/blame-refactor: blame: simplify prepare_lines() blame: factor out get_next_line()
2 parents 35869f4 + 60d85e1 commit 8d87e35

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

builtin/blame.c

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,12 @@ static void output(struct scoreboard *sb, int option)
20082008
}
20092009
}
20102010

2011+
static const char *get_next_line(const char *start, const char *end)
2012+
{
2013+
const char *nl = memchr(start, '\n', end - start);
2014+
return nl ? nl + 1 : end;
2015+
}
2016+
20112017
/*
20122018
* To allow quick access to the contents of nth line in the
20132019
* final image, prepare an index in the scoreboard.
@@ -2019,39 +2025,19 @@ static int prepare_lines(struct scoreboard *sb)
20192025
const char *end = buf + len;
20202026
const char *p;
20212027
int *lineno;
2022-
int num = 0, incomplete = 0;
2023-
2024-
for (p = buf;;) {
2025-
p = memchr(p, '\n', end - p);
2026-
if (p) {
2027-
p++;
2028-
num++;
2029-
continue;
2030-
}
2031-
break;
2032-
}
2028+
int num = 0;
20332029

2034-
if (len && end[-1] != '\n')
2035-
incomplete++; /* incomplete line at the end */
2030+
for (p = buf; p < end; p = get_next_line(p, end))
2031+
num++;
20362032

2037-
sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
2038-
lineno = sb->lineno;
2033+
sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
20392034

2040-
*lineno++ = 0;
2041-
for (p = buf;;) {
2042-
p = memchr(p, '\n', end - p);
2043-
if (p) {
2044-
p++;
2045-
*lineno++ = p - buf;
2046-
continue;
2047-
}
2048-
break;
2049-
}
2035+
for (p = buf; p < end; p = get_next_line(p, end))
2036+
*lineno++ = p - buf;
20502037

2051-
if (incomplete)
2052-
*lineno++ = len;
2038+
*lineno = len;
20532039

2054-
sb->num_lines = num + incomplete;
2040+
sb->num_lines = num;
20552041
return sb->num_lines;
20562042
}
20572043

0 commit comments

Comments
 (0)