}
}
+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.
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;
}