commit 775f00734828229ef2eca0ad8b84f36a5d70427c
parent 414a1ecc8d91304fa988ba2a63705213a346998b
Author: Benno Schulenberg <bensberg@justemail.net>
Date: Sat, 21 Jan 2017 18:18:34 +0100
tweaks: use a cheaper way to detect an end-of-line
There is no need to compute the line length: just avoid overstepping
the terminating NUL byte when being forced to advance the index.
Diffstat:
2 files changed, 13 insertions(+), 18 deletions(-)
diff --git a/src/color.c b/src/color.c
@@ -414,7 +414,6 @@ void precalc_multicolorinfo(void)
for (line = openfile->fileage; line != NULL; line = line->next) {
int index = 0;
- int linelen = strlen(line->data);
alloc_multidata_if_needed(line);
/* Assume nothing applies until proven otherwise below. */
@@ -424,11 +423,9 @@ void precalc_multicolorinfo(void)
* found, mark all the lines that are affected. */
while (regexec(ink->start, line->data + index, 1,
&startmatch, (index == 0) ? 0 : REG_NOTBOL) == 0) {
+ /* Begin looking for an end match after the start match. */
index += startmatch.rm_eo;
- if (index > linelen)
- break;
-
/* If there is an end match on this line, mark the line, but
* continue looking for other starts after it. */
if (regexec(ink->end, line->data + index, 1,
@@ -437,8 +434,12 @@ void precalc_multicolorinfo(void)
index += endmatch.rm_eo;
/* If both start and end are mere anchors, step ahead. */
if (startmatch.rm_so == startmatch.rm_eo &&
- endmatch.rm_so == endmatch.rm_eo)
- index += 1;
+ endmatch.rm_so == endmatch.rm_eo) {
+ /* When at end-of-line, we're done. */
+ if (line->data[index] == '\0')
+ break;
+ index = move_mbright(line->data, index);
+ }
continue;
}
diff --git a/src/winio.c b/src/winio.c
@@ -2419,8 +2419,6 @@ void edit_draw(filestruct *fileptr, const char *converted,
/* The line that matches 'end'. */
regmatch_t startmatch, endmatch;
/* The match positions of the start and end regexes. */
- int linelen;
- /* The length of the line we are currently looking at. */
/* First see if the multidata was maybe already calculated. */
if (fileptr->multidata[varnish->id] == CNONE)
@@ -2476,8 +2474,6 @@ void edit_draw(filestruct *fileptr, const char *converted,
start_line->multidata[varnish->id] == CSTARTENDHERE))
goto step_two;
- linelen = strlen(start_line->data);
-
/* Now start_line is the first line before fileptr containing
* a start match. Is there a start on that line not followed
* by an end on that line? */
@@ -2486,9 +2482,9 @@ void edit_draw(filestruct *fileptr, const char *converted,
index += startmatch.rm_eo;
/* If the start match is of length zero, step ahead. */
if (startmatch.rm_so == startmatch.rm_eo) {
- index = move_mbright(start_line->data, index);
- if (index > linelen)
+ if (start_line->data[index] == '\0')
break;
+ index = move_mbright(start_line->data, index);
}
/* If there is no end after this last start, good. */
if (regexec(varnish->end, start_line->data + index,
@@ -2498,9 +2494,9 @@ void edit_draw(filestruct *fileptr, const char *converted,
index += endmatch.rm_eo;
/* If the end match is of length zero, step ahead. */
if (endmatch.rm_so == endmatch.rm_eo) {
- index = move_mbright(start_line->data, index);
- if (index > linelen)
+ if (start_line->data[index] == '\0')
break;
+ index = move_mbright(start_line->data, index);
}
/* If there is no later start on this line, next step. */
if (regexec(varnish->start, start_line->data + index,
@@ -2545,8 +2541,6 @@ void edit_draw(filestruct *fileptr, const char *converted,
* looking only after an end match, if there is one. */
index = (paintlen == 0) ? 0 : endmatch.rm_eo;
- linelen = strlen(fileptr->data);
-
while (regexec(varnish->start, fileptr->data + index,
1, &startmatch, (index == 0) ?
0 : REG_NOTBOL) == 0) {
@@ -2586,9 +2580,9 @@ void edit_draw(filestruct *fileptr, const char *converted,
index = endmatch.rm_eo;
/* If the end match is of length zero, step ahead. */
if (endmatch.rm_so == endmatch.rm_eo) {
- index = move_mbright(fileptr->data, index);
- if (index > linelen)
+ if (fileptr->data[index] == '\0')
break;
+ index = move_mbright(fileptr->data, index);
}
continue;
}