tweaks: rename two variables to be just one as they play the same role

The renamed variable 'index' is not the start of a match (as some comment
mistakenly said), but from where in the line we start looking for a next
match.

Also, use one more goto to allow unindenting a big piece of code, and
shortcircuit two while loops for two more small unindents.
master
Benno Schulenberg 2017-01-08 22:04:43 +01:00
parent 26683a9e5f
commit 38d7362f20
1 changed files with 45 additions and 35 deletions

View File

@ -2348,6 +2348,8 @@ void edit_draw(filestruct *fileptr, const char *converted, int
/* Iterate through all the coloring regexes. */ /* Iterate through all the coloring regexes. */
for (; varnish != NULL; varnish = varnish->next) { for (; varnish != NULL; varnish = varnish->next) {
size_t index = 0;
/* Where in the line we currently begin looking for a match. */
int start_col; int start_col;
/* The starting column of a piece to paint. Zero-based. */ /* The starting column of a piece to paint. Zero-based. */
int paintlen = 0; int paintlen = 0;
@ -2365,32 +2367,38 @@ void edit_draw(filestruct *fileptr, const char *converted, int
/* First case: varnish is a single-line expression. */ /* First case: varnish is a single-line expression. */
if (varnish->end == NULL) { if (varnish->end == NULL) {
size_t k = 0; /* We increment index by rm_eo, to move past the end of the
/* We increment k by rm_eo, to move past the end of the
* last match. Even though two matches may overlap, we * last match. Even though two matches may overlap, we
* want to ignore them, so that we can highlight e.g. C * want to ignore them, so that we can highlight e.g. C
* strings correctly. */ * strings correctly. */
while (k < till_x) { while (index < till_x) {
/* Note the fifth parameter to regexec(). It says /* Note the fifth parameter to regexec(). It says
* not to match the beginning-of-line character * not to match the beginning-of-line character
* unless k is zero. If regexec() returns * unless index is zero. If regexec() returns
* REG_NOMATCH, there are no more matches in the * REG_NOMATCH, there are no more matches in the
* line. */ * line. */
if (regexec(varnish->start, &fileptr->data[k], 1, if (regexec(varnish->start, &fileptr->data[index], 1,
&startmatch, (k == 0) ? 0 : REG_NOTBOL) == &startmatch, (index == 0) ? 0 : REG_NOTBOL) ==
REG_NOMATCH) REG_NOMATCH)
break; break;
/* Translate the match to the beginning of the line. */
startmatch.rm_so += k;
startmatch.rm_eo += k;
/* Skip over a zero-length regex match. */ /* Skip over a zero-length regex match. */
if (startmatch.rm_so == startmatch.rm_eo) if (startmatch.rm_so == startmatch.rm_eo) {
startmatch.rm_eo++; index += startmatch.rm_eo + 1;
else if (startmatch.rm_so < till_x && continue;
startmatch.rm_eo > from_x) { }
/* Translate the match to the beginning of the line. */
startmatch.rm_so += index;
startmatch.rm_eo += index;
index = startmatch.rm_eo;
/* If the matching piece is not visible, skip it. */
if (startmatch.rm_so >= till_x ||
startmatch.rm_eo <= from_x)
continue;
{
start_col = (startmatch.rm_so <= from_x) ? start_col = (startmatch.rm_so <= from_x) ?
0 : strnlenpt(fileptr->data, 0 : strnlenpt(fileptr->data,
startmatch.rm_so) - from_col; startmatch.rm_so) - from_col;
@ -2403,14 +2411,15 @@ void edit_draw(filestruct *fileptr, const char *converted, int
mvwaddnstr(edit, line, margin + start_col, mvwaddnstr(edit, line, margin + start_col,
thetext, paintlen); thetext, paintlen);
} }
k = startmatch.rm_eo;
} }
} else { /* Second case: varnish is a multiline expression. */ goto tail_of_loop;
}
/* Second case: varnish is a multiline expression. */
{
const filestruct *start_line = fileptr->prev; const filestruct *start_line = fileptr->prev;
/* The first line before fileptr that matches 'start'. */ /* The first line before fileptr that matches 'start'. */
size_t start_x; const filestruct *end_line = fileptr;
/* Where the match starts in that line. */
const filestruct *end_line;
/* The line that matches 'end'. */ /* The line that matches 'end'. */
/* First see if the multidata was maybe already calculated. */ /* First see if the multidata was maybe already calculated. */
@ -2474,18 +2483,17 @@ void edit_draw(filestruct *fileptr, const char *converted, int
/* Now start_line is the first line before fileptr containing /* Now start_line is the first line before fileptr containing
* a start match. Is there a start on that line not followed * a start match. Is there a start on that line not followed
* by an end on that line? */ * by an end on that line? */
start_x = 0;
while (TRUE) { while (TRUE) {
start_x += startmatch.rm_so; index += startmatch.rm_so;
startmatch.rm_eo -= startmatch.rm_so; startmatch.rm_eo -= startmatch.rm_so;
if (regexec(varnish->end, start_line->data + if (regexec(varnish->end, start_line->data +
start_x + startmatch.rm_eo, 0, NULL, index + startmatch.rm_eo, 0, NULL,
(start_x + startmatch.rm_eo == 0) ? (index + startmatch.rm_eo == 0) ?
0 : REG_NOTBOL) == REG_NOMATCH) 0 : REG_NOTBOL) == REG_NOMATCH)
/* No end found after this start. */ /* No end found after this start. */
break; break;
start_x++; index++;
if (regexec(varnish->start, start_line->data + start_x, if (regexec(varnish->start, start_line->data + index,
1, &startmatch, REG_NOTBOL) == REG_NOMATCH) 1, &startmatch, REG_NOTBOL) == REG_NOMATCH)
/* No later start on this line. */ /* No later start on this line. */
goto step_two; goto step_two;
@ -2495,7 +2503,6 @@ void edit_draw(filestruct *fileptr, const char *converted, int
/* We've already checked that there is no end before fileptr /* We've already checked that there is no end before fileptr
* and after the start. But is there an end after the start * and after the start. But is there an end after the start
* at all? We don't paint unterminated starts. */ * at all? We don't paint unterminated starts. */
end_line = fileptr;
while (end_line != NULL && regexec(varnish->end, while (end_line != NULL && regexec(varnish->end,
end_line->data, 1, &endmatch, 0) == REG_NOMATCH) end_line->data, 1, &endmatch, 0) == REG_NOMATCH)
end_line = end_line->next; end_line = end_line->next;
@ -2530,15 +2537,15 @@ void edit_draw(filestruct *fileptr, const char *converted, int
step_two: step_two:
/* Second step: look for starts on this line, but begin /* Second step: look for starts on this line, but begin
* looking only after an end match, if there is one. */ * looking only after an end match, if there is one. */
start_x = (paintlen == 0) ? 0 : endmatch.rm_eo; index = (paintlen == 0) ? 0 : endmatch.rm_eo;
while (regexec(varnish->start, fileptr->data + start_x, while (regexec(varnish->start, fileptr->data + index,
1, &startmatch, (start_x == 0) ? 1, &startmatch, (index == 0) ?
0 : REG_NOTBOL) == 0) { 0 : REG_NOTBOL) == 0) {
/* Translate the match to be relative to the /* Translate the match to be relative to the
* beginning of the line. */ * beginning of the line. */
startmatch.rm_so += start_x; startmatch.rm_so += index;
startmatch.rm_eo += start_x; startmatch.rm_eo += index;
start_col = (startmatch.rm_so <= from_x) ? start_col = (startmatch.rm_so <= from_x) ?
0 : strnlenpt(fileptr->data, 0 : strnlenpt(fileptr->data,
@ -2570,11 +2577,14 @@ void edit_draw(filestruct *fileptr, const char *converted, int
fprintf(stderr, " Marking for id %i line %i as CSTARTENDHERE\n", varnish->id, line); fprintf(stderr, " Marking for id %i line %i as CSTARTENDHERE\n", varnish->id, line);
#endif #endif
} }
start_x = endmatch.rm_eo; index = endmatch.rm_eo;
/* Skip over a zero-length match. */ /* Skip over a zero-length match. */
if (endmatch.rm_so == endmatch.rm_eo) if (endmatch.rm_so == endmatch.rm_eo)
start_x += 1; index += 1;
} else { continue;
}
{
/* There is no end on this line. But we haven't yet /* There is no end on this line. But we haven't yet
* looked for one on later lines. */ * looked for one on later lines. */
end_line = fileptr->next; end_line = fileptr->next;