From 29aa0b2061712c83ec5eb5c1556436b1218035ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Fri, 13 Jun 2014 21:53:03 +0200 Subject: [PATCH 1/2] 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 Signed-off-by: Junio C Hamano --- builtin/blame.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 88cb799727..f685b38a2f 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -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. @@ -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 */ @@ -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; From 60d85e110b520ee5c6839380e5bb3d6e38571a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Fri, 13 Jun 2014 21:54:59 +0200 Subject: [PATCH 2/2] 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 Signed-off-by: Junio C Hamano --- builtin/blame.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index f685b38a2f..269c64873e 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -1744,7 +1744,7 @@ 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; + return nl ? nl + 1 : end; } /* @@ -1758,25 +1758,19 @@ static int prepare_lines(struct scoreboard *sb) const char *end = buf + len; const char *p; int *lineno; - int num = 0, incomplete = 0; + int num = 0; - for (p = get_next_line(buf, end); p; p = get_next_line(p, end)) + for (p = buf; p < end; p = get_next_line(p, end)) num++; - if (len && end[-1] != '\n') - incomplete++; /* incomplete line at the end */ + sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1)); - sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1)); - lineno = sb->lineno; - - *lineno++ = 0; - for (p = get_next_line(buf, end); p; p = get_next_line(p, end)) + for (p = buf; p < end; p = get_next_line(p, end)) *lineno++ = p - buf; - if (incomplete) - *lineno++ = len; + *lineno = len; - sb->num_lines = num + incomplete; + sb->num_lines = num; return sb->num_lines; }