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. --- ed.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/ed.c b/ed.c index 9dc6fda..0ec0574 100644 --- a/ed.c +++ b/ed.c @@ -63,6 +63,7 @@ static char *rhs; static char *lastmatch; static struct undo udata; static int newcmd; +int eol, bol; @@ -360,11 +361,15 @@ compile(int delim) if (!isgraph(delim)) error("invalid pattern delimiter"); - bracket = siz = 0; + eol = bol = bracket = siz = 0; for (n = 0;; ++n) { if ((c = input()) == delim && !bracket) break; - if (c == '\n' || c == EOF) { + if (c == '^') { + bol = 1; + } else if (c == '$') { + eol = 1; + } else if (c == '\n' || c == EOF) { back(c); break; } @@ -1007,9 +1012,11 @@ subline(int num, int nth) static size_t siz, cap; 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); changed |= addsub(&s, &cap, &siz, nth, ++i); + if (eol || bol) + break; } if (!changed) return; -- 2.1.4