blame: factor out get_next_line()
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>
René Scharfe committed
Jun 13, 2014 at 21:53 UTC
29aa0b2061712c83ec5eb5c1556436b1218035ba
1 file changed
+10
-18
builtin/blame.c
+10
-18
@@ -1741,6 +1741,12 @@ static void output(struct scoreboard *sb, int option)
1741
}
1742
}
1743
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;
1748
+}
1749
+
1750
/*
1751
* To allow quick access to the contents of nth line in the
1752
* final image, prepare an index in the scoreboard.
@@ -1754,15 +1760,8 @@ static int prepare_lines(struct scoreboard *sb)
1760
int *lineno;
1761
int num = 0, incomplete = 0;
1762
1757
- for (p = buf;;) {
1758
- p = memchr(p, '\n', end - p);
1759
- if (p) {
1760
- p++;
1761
- num++;
1762
- continue;
1763
- }
1764
- break;
1765
- }
1763
+ for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
1764
+ num++;
1765
1766
if (len && end[-1] != '\n')
1767
incomplete++; /* incomplete line at the end */
@@ -1771,15 +1770,8 @@ static int prepare_lines(struct scoreboard *sb)
1770
lineno = sb->lineno;
1771
1772
*lineno++ = 0;
1774
- for (p = buf;;) {
1775
- p = memchr(p, '\n', end - p);
1776
- if (p) {
1777
- p++;
1778
- *lineno++ = p - buf;
1779
- continue;
1780
- }
1781
- break;
1782
- }
1773
+ for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
1774
+ *lineno++ = p - buf;
1775
1776
if (incomplete)
1777
*lineno++ = len;