Do not try to rematch patterns with ^ or $

It is impossible to rematch a pattern which has one (or both)
of these operators, so the simplest solucion is detect them
while we are compiling the regular expression and break the
match loop after the first iteration.
This commit is contained in:
Roberto E. Vargas Caballero 2016-01-23 09:11:33 +01:00 committed by sin
parent 0d955d3f23
commit cfc37be486

13
ed.c
View File

@ -63,6 +63,7 @@ static char *rhs;
static char *lastmatch; static char *lastmatch;
static struct undo udata; static struct undo udata;
static int newcmd; static int newcmd;
int eol, bol;
static void static void
discard(void) discard(void)
@ -358,11 +359,15 @@ compile(int delim)
if (!isgraph(delim)) if (!isgraph(delim))
error("invalid pattern delimiter"); error("invalid pattern delimiter");
bracket = siz = 0; eol = bol = bracket = siz = 0;
for (n = 0;; ++n) { for (n = 0;; ++n) {
if ((c = input()) == delim && !bracket) if ((c = input()) == delim && !bracket)
break; break;
if (c == '\n' || c == EOF) { if (c == '^') {
bol = 1;
} else if (c == '$') {
eol = 1;
} else if (c == '\n' || c == EOF) {
back(c); back(c);
break; break;
} }
@ -1005,9 +1010,11 @@ subline(int num, int nth)
static size_t siz, cap; static size_t siz, cap;
i = changed = siz = 0; i = changed = siz = 0;
for (m = match(num); m && *lastmatch != '\n'; m = rematch(num)) { for (m = match(num); m; m = rematch(num)) {
addpre(&s, &cap, &siz); addpre(&s, &cap, &siz);
changed |= addsub(&s, &cap, &siz, nth, ++i); changed |= addsub(&s, &cap, &siz, nth, ++i);
if (eol || bol)
break;
} }
if (!changed) if (!changed)
return; return;