[PATCH 1/2] blame: factor out get_next_line()

2014-06-13 Thread René Scharfe
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@web.de
---
 builtin/blame.c | 28 ++--
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/builtin/blame.c b/builtin/blame.c
index a52a279..ad37edc 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2008,6 +2008,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.
@@ -2021,15 +2027,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 */
@@ -2038,15 +2037,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;
-- 
2.0.0

--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/2] blame: factor out get_next_line()

2014-06-13 Thread Jonathan Nieder
René Scharfe wrote:

 Signed-off-by: Rene Scharfe l@web.de
 ---
  builtin/blame.c | 28 ++--
  1 file changed, 10 insertions(+), 18 deletions(-)

Reviewed-by: Jonathan Nieder jrnie...@gmail.com

[...]
 + for (p = get_next_line(buf, end); p; p = get_next_line(p, end))
 + num++;

This could say something like

for (p = buf; p = get_next_line(buf, end); ) {
num++;

or

p = buf;
while ((p = memchr(p, '\n', end - p))) {
p++;
num++;
}

but what you have seems more readable.

Thanks,
Jonathan
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html