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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jun 13, 2014 at 21:54 UTC
60d85e110b520ee5c6839380e5bb3d6e38571a74
1 file changed
+7
-13
builtin/blame.c
+7
-13
@@ -1744,7 +1744,7 @@ static void output(struct scoreboard *sb, int option)
1744
static const char *get_next_line(const char *start, const char *end)
1745
{
1746
const char *nl = memchr(start, '\n', end - start);
1747
- return nl ? nl + 1 : NULL;
1747
+ return nl ? nl + 1 : end;
1748
}
1749
1750
/*
@@ -1758,25 +1758,19 @@ static int prepare_lines(struct scoreboard *sb)
1758
const char *end = buf + len;
1759
const char *p;
1760
int *lineno;
1761
- int num = 0, incomplete = 0;
1761
+ int num = 0;
1762
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))
1764
num++;
1765
1766
- if (len && end[-1] != '\n')
1767
- incomplete++; /* incomplete line at the end */
1766
+ sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
1767
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))
1769
*lineno++ = p - buf;
1770
1776
- if (incomplete)
1777
- *lineno++ = len;
1771
+ *lineno = len;
1772
1779
- sb->num_lines = num + incomplete;
1773
+ sb->num_lines = num;
1774
return sb->num_lines;
1775
}
1776