Merge branch 'jn/bisect-coding-style'
[gitweb.git] / builtin / blame.c
index 9566a5ea4eabcdf83af46032283fa6ede426bca0..e5b5d71bad8653f4b9d19044c3c36dc2ab8b42f1 100644 (file)
@@ -923,7 +923,6 @@ static void find_copy_in_blob(struct scoreboard *sb,
                              mmfile_t *file_p)
 {
        const char *cp;
-       int cnt;
        mmfile_t file_o;
        struct handle_split_cb_data d;
 
@@ -934,13 +933,7 @@ static void find_copy_in_blob(struct scoreboard *sb,
         */
        cp = nth_line(sb, ent->lno);
        file_o.ptr = (char *) cp;
-       cnt = ent->num_lines;
-
-       while (cnt && cp < sb->final_buf + sb->final_buf_size) {
-               if (*cp++ == '\n')
-                       cnt--;
-       }
-       file_o.size = cp - file_o.ptr;
+       file_o.size = nth_line(sb, ent->lno + ent->num_lines) - cp;
 
        /*
         * file_o is a part of final image we are annotating.
@@ -1564,14 +1557,14 @@ static const char *format_time(unsigned long time, const char *tz_str,
                               int show_raw_time)
 {
        static char time_buf[128];
-       const char *time_str;
-       int time_len;
-       int tz;
 
        if (show_raw_time) {
                snprintf(time_buf, sizeof(time_buf), "%lu %s", time, tz_str);
        }
        else {
+               const char *time_str;
+               int time_len;
+               int tz;
                tz = atoi(tz_str);
                time_str = show_date(time, tz, blame_date_mode);
                time_len = strlen(time_str);
@@ -1756,25 +1749,41 @@ static int prepare_lines(struct scoreboard *sb)
 {
        const char *buf = sb->final_buf;
        unsigned long len = sb->final_buf_size;
-       int num = 0, incomplete = 0, bol = 1;
+       const char *end = buf + len;
+       const char *p;
+       int *lineno;
+       int num = 0, incomplete = 0;
 
-       if (len && buf[len-1] != '\n')
-               incomplete++; /* incomplete line at the end */
-       while (len--) {
-               if (bol) {
-                       sb->lineno = xrealloc(sb->lineno,
-                                             sizeof(int) * (num + 1));
-                       sb->lineno[num] = buf - sb->final_buf;
-                       bol = 0;
-               }
-               if (*buf++ == '\n') {
+       for (p = buf;;) {
+               p = memchr(p, '\n', end - p);
+               if (p) {
+                       p++;
                        num++;
-                       bol = 1;
+                       continue;
+               }
+               break;
+       }
+
+       if (len && end[-1] != '\n')
+               incomplete++; /* incomplete line at the end */
+
+       sb->lineno = xmalloc(sizeof(*sb->lineno) * (num + incomplete + 1));
+       lineno = sb->lineno;
+
+       *lineno++ = 0;
+       for (p = buf;;) {
+               p = memchr(p, '\n', end - p);
+               if (p) {
+                       p++;
+                       *lineno++ = p - buf;
+                       continue;
                }
+               break;
        }
-       sb->lineno = xrealloc(sb->lineno,
-                             sizeof(int) * (num + incomplete + 1));
-       sb->lineno[num + incomplete] = buf - sb->final_buf;
+
+       if (incomplete)
+               *lineno++ = len;
+
        sb->num_lines = num + incomplete;
        return sb->num_lines;
 }