aboutsummaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2014-06-25 12:23:36 -0700
committerJunio C Hamano <gitster@pobox.com>2014-06-25 12:23:36 -0700
commit8d87e35babb0f9d8657ef530dd9d77eed0bfec9a (patch)
tree577d90c8deb2cfcf1e8ec49a34bc1d43cd38dfc8 /builtin
parent35869f4c626619c2574068752aef6d3130c0ae02 (diff)
parent60d85e110b520ee5c6839380e5bb3d6e38571a74 (diff)
downloadgit-8d87e35babb0f9d8657ef530dd9d77eed0bfec9a.tar.gz
git-8d87e35babb0f9d8657ef530dd9d77eed0bfec9a.tar.xz
Merge branch 'rs/blame-refactor'
* rs/blame-refactor: blame: simplify prepare_lines() blame: factor out get_next_line()
Diffstat (limited to 'builtin')
-rw-r--r--builtin/blame.c42
1 files changed, 14 insertions, 28 deletions
diff --git a/builtin/blame.c b/builtin/blame.c
index a52a27914..662e3fec4 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 : end;
+}
+
/*
* To allow quick access to the contents of nth line in the
* final image, prepare an index in the scoreboard.
@@ -2019,39 +2025,19 @@ static int prepare_lines(struct scoreboard *sb)
const char *end = buf + len;
const char *p;
int *lineno;
- int num = 0, incomplete = 0;
-
- for (p = buf;;) {
- p = memchr(p, '\n', end - p);
- if (p) {
- p++;
- num++;
- continue;
- }
- break;
- }
+ int num = 0;
- if (len && end[-1] != '\n')
- incomplete++; /* incomplete line at the end */
+ for (p = buf; p < end; p = get_next_line(p, end))
+ num++;
- sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
- lineno = sb->lineno;
+ sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
- *lineno++ = 0;
- for (p = buf;;) {
- p = memchr(p, '\n', end - p);
- if (p) {
- p++;
- *lineno++ = p - buf;
- continue;
- }
- break;
- }
+ 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;
}