/* Search for a match to one of the two characters in bracket_set. If * reverse is TRUE, search backwards for the leftmost bracket. * Otherwise, search forwards for the rightmost bracket. Return TRUE if * we found a match, and FALSE otherwise. */ bool find_bracket_match(bool reverse, const char *bracket_set) { linestruct *fileptr = openfile->current; const char *rev_start = NULL, *found = NULL; ssize_t current_y_find = openfile->current_y; assert(mbstrlen(bracket_set) == 2); /* rev_start might end up 1 character before the start or after the * end of the line. This won't be a problem because we'll skip over * it below in that case, and rev_start will be properly set when * the search continues on the previous or next line. */ rev_start = reverse ? fileptr->data + (openfile->current_x - 1) : fileptr->data + (openfile->current_x + 1); /* Look for either of the two characters in bracket_set. rev_start * can be 1 character before the start or after the end of the line. * In either case, just act as though no match is found. */ while (TRUE) { found = ((rev_start > fileptr->data && *(rev_start - 1) == '\0') || rev_start < fileptr->data) ? NULL : (reverse ? mbrevstrpbrk(fileptr->data, bracket_set, rev_start) : mbstrpbrk(rev_start, bracket_set)); if (found != NULL) /* We've found a potential match. */ break; if (reverse) { fileptr = fileptr->prev; current_y_find--; } else { fileptr = fileptr->next; current_y_find++; } if (fileptr == NULL) /* We've reached the start or end of the buffer, so get out. */ return FALSE; rev_start = fileptr->data; if (reverse) rev_start += strlen(fileptr->data); } /* We've definitely found something. */ openfile->current = fileptr; openfile->current_x = found - fileptr->data; openfile->placewewant = xplustabs(); openfile->current_y = current_y_find; return TRUE; }
/* Search for a match to one of the two characters in bracket_set. If * reverse is TRUE, search backwards for the leftmost bracket. * Otherwise, search forwards for the rightmost bracket. Return TRUE if * we found a match, and FALSE otherwise. */ bool find_statusbar_bracket_match(bool reverse, const char *bracket_set) { const char *rev_start = NULL, *found = NULL; assert(mbstrlen(bracket_set) == 2); /* rev_start might end up 1 character before the start or after the * end of the line. This won't be a problem because we'll skip over * it below in that case. */ rev_start = reverse ? answer + (statusbar_x - 1) : answer + (statusbar_x + 1); while (TRUE) { /* Look for either of the two characters in bracket_set. * rev_start can be 1 character before the start or after the * end of the line. In either case, just act as though no match * is found. */ found = ((rev_start > answer && *(rev_start - 1) == '\0') || rev_start < answer) ? NULL : (reverse ? mbrevstrpbrk(answer, bracket_set, rev_start) : mbstrpbrk(rev_start, bracket_set)); /* We've found a potential match. */ if (found != NULL) break; /* We've reached the start or end of the statusbar text, so * get out. */ return FALSE; } /* We've definitely found something. */ statusbar_x = found - answer; statusbar_pww = statusbar_xplustabs(); return TRUE; }