commit 580fd13424728a2213ad27f686276a2138939301
parent 790ce3791a71574945e066a2356a089d841e4269
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Sat, 6 Apr 2019 21:05:02 +0200
tweaks: do a check up front instead of every time round the loop
Instead of doing a check every time round the loop when it is useful
only the first time, duplicate a bit a code and do the check and its
related action beforehand.
Also, improve some comments.
Diffstat:
1 file changed, 14 insertions(+), 11 deletions(-)
diff --git a/src/search.c b/src/search.c
@@ -862,26 +862,29 @@ void do_gotolinecolumn_void(void)
}
#ifndef NANO_TINY
-/* Search, startting from the current position, for any of the two characters
+/* Search, starting from the current position, for any of the two characters
* in bracket_set. If reverse is TRUE, search backwards, otherwise forwards.
- * Return TRUE when a match was found, and FALSE otherwise. */
+ * Return TRUE when one of the brackets was found, and FALSE otherwise. */
bool find_bracket_match(bool reverse, const char *bracket_set)
{
linestruct *line = openfile->current;
const char *pointer = line->data + openfile->current_x;
const char *found = NULL;
- /* The pointer might end up one character before the start of the line.
- * This is not a problem: it will be skipped over below in the loop. */
- if (reverse)
- --pointer;
- else
+ /* Step away from the current bracket, either backwards or forwards. */
+ if (reverse) {
+ if (--pointer < line->data) {
+ line = line->prev;
+ if (line == NULL)
+ return FALSE;
+ pointer = line->data + strlen(line->data);
+ }
+ } else
++pointer;
+ /* Now seek for any of the two brackets, either backwards or forwards. */
while (TRUE) {
- if (pointer < line->data)
- found = NULL;
- else if (reverse)
+ if (reverse)
found = mbrevstrpbrk(line->data, bracket_set, pointer);
else
found = mbstrpbrk(pointer, bracket_set);
@@ -903,7 +906,7 @@ bool find_bracket_match(bool reverse, const char *bracket_set)
pointer += strlen(line->data);
}
- /* Set the current position to the found matching bracket. */
+ /* Set the current position to the found bracket. */
openfile->current = line;
openfile->current_x = found - line->data;