tweaks: elide a supporting variable, to make four loops slightly faster

At the cost of making the return statements slower.  But, all-in-all,
this should be slightly quicker.
master
Benno Schulenberg 2020-03-12 16:45:52 +01:00
parent 21ed79938e
commit 6b8b7c9d55
1 changed files with 29 additions and 41 deletions

View File

@ -1356,79 +1356,67 @@ bool do_wrap(void)
* no such blank, then we find the first blank. We then take the last * no such blank, then we find the first blank. We then take the last
* blank in that group of blanks. The terminating '\0' counts as a * blank in that group of blanks. The terminating '\0' counts as a
* blank, as does a '\n' if snap_at_nl is TRUE. */ * blank, as does a '\n' if snap_at_nl is TRUE. */
ssize_t break_line(const char *line, ssize_t goal, bool snap_at_nl) ssize_t break_line(const char *textstart, ssize_t goal, bool snap_at_nl)
{ {
ssize_t lastblank = -1; const char *lastblank = NULL;
/* The index of the last blank we found. */ /* The point where the last blank was found, if any. */
ssize_t index = 0; const char *pointer = textstart;
/* The index of the character we are looking at. */ /* An iterator through the given line of text. */
size_t column = 0; size_t column = 0;
/* The column position that corresponds with index. */ /* The column number that corresponds to the position of the pointer. */
int charlen = 0;
/* The length of the current character, in bytes. */
/* Skip over leading whitespace, where a line should never be broken. */ /* Skip over leading whitespace, where a line should never be broken. */
while (*line != '\0' && is_blank_char(line)) { while (*pointer != '\0' && is_blank_char(pointer))
charlen = advance_over(line, &column); pointer += advance_over(pointer, &column);
line += charlen;
index += charlen;
}
/* Find the last blank that does not overshoot the target column. */ /* Find the last blank that does not overshoot the target column. */
while (*line != '\0' && ((ssize_t)column <= goal)) { while (*pointer != '\0' && ((ssize_t)column <= goal)) {
if (is_blank_char(line)) if (is_blank_char(pointer))
lastblank = index; lastblank = pointer;
#ifdef ENABLE_HELP #ifdef ENABLE_HELP
else if (snap_at_nl && *line == '\n') { else if (snap_at_nl && *pointer == '\n') {
lastblank = index; lastblank = pointer;
break; break;
} }
#endif #endif
charlen = advance_over(line, &column); pointer += advance_over(pointer, &column);
line += charlen;
index += charlen;
} }
/* If the whole line displays shorter than goal, we're done. */ /* If the whole line displays shorter than goal, we're done. */
if ((ssize_t)column <= goal) if ((ssize_t)column <= goal)
return index; return (pointer - textstart);
#ifdef ENABLE_HELP #ifdef ENABLE_HELP
/* If we're wrapping a help text and no blank was found, or was /* If we're wrapping a help text and no blank was found, or was
* found only as the first character, force a line break. */ * found only as the first character, force a line break. */
if (snap_at_nl && lastblank < 1) if (snap_at_nl && lastblank == NULL)
return (index - charlen); return step_left(textstart, pointer - textstart);
#endif #endif
/* If no blank was found within the goal width, seek one after it. */ /* If no blank was found within the goal width, seek one after it. */
if (lastblank < 0) { if (lastblank == NULL) {
while (*line != '\0') { while (*pointer != '\0') {
if (is_blank_char(line)) if (is_blank_char(pointer))
lastblank = index; lastblank = pointer;
else if (lastblank > 0) else if (lastblank)
return lastblank; return (lastblank - textstart);
charlen = char_length(line); pointer += char_length(pointer);
line += charlen;
index += charlen;
} }
return -1; return -1;
} }
/* Move the pointer back to the last blank, and then step beyond it. */ /* Move the pointer back to the last blank, and then step beyond it. */
line = line - index + lastblank; pointer = lastblank + char_length(lastblank);
charlen = char_length(line);
line += charlen;
/* Skip any consecutive blanks after the last blank. */ /* Skip any consecutive blanks after the last blank. */
while (*line != '\0' && is_blank_char(line)) { while (*pointer != '\0' && is_blank_char(pointer)) {
lastblank += charlen; lastblank = pointer;
charlen = char_length(line); pointer += char_length(pointer);
line += charlen;
} }
return lastblank; return (lastblank - textstart);
} }
#endif /* ENABLE_HELP || ENABLED_WRAPORJUSTIFY */ #endif /* ENABLE_HELP || ENABLED_WRAPORJUSTIFY */