Commit b0b24d9c authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

utils: slightly speed up the calculation of the size of a buffer

Achieve this by eliding two conditions from the inner loop,
which is possible because 'end' will never be NULL.
Showing with 9 additions and 21 deletions
+9 -21
...@@ -599,32 +599,20 @@ void mark_order(const filestruct **top, size_t *top_x, const filestruct ...@@ -599,32 +599,20 @@ void mark_order(const filestruct **top, size_t *top_x, const filestruct
} }
#endif /* !NANO_TINY */ #endif /* !NANO_TINY */
/* Calculate the number of characters between begin and end, and return /* Count the number of characters from begin to end, and return it. */
* it. */
size_t get_totsize(const filestruct *begin, const filestruct *end) size_t get_totsize(const filestruct *begin, const filestruct *end)
{ {
const filestruct *line;
size_t totsize = 0; size_t totsize = 0;
const filestruct *f;
/* Go through the lines from begin to end->prev, if we can. */ /* Sum the number of characters (plus a newline) in each line. */
for (f = begin; f != end && f != NULL; f = f->next) { for (line = begin; line != end->next; line = line->next)
/* Count the number of characters on this line. */ totsize += mbstrlen(line->data) + 1;
totsize += mbstrlen(f->data);
/* Count the newline if we have one. */ /* The last line of a file doesn't have a newline -- otherwise it
if (f->next != NULL) * wouldn't be the last line -- so subtract 1 when at EOF. */
totsize++; if (line == NULL)
} totsize--;
/* Go through the line at end, if we can. */
if (f != NULL) {
/* Count the number of characters on this line. */
totsize += mbstrlen(f->data);
/* Count the newline if we have one. */
if (f->next != NULL)
totsize++;
}
return totsize; return totsize;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment