[hackers] [sbase] Do not try to rematch patterns with ^ or $ || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Sat, 23 Jan 2016 09:38:09 +0100 (CET)

commit cfc37be4862574ac68f424a42763cf5681243812
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sat Jan 23 09:11:33 2016 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Sat Jan 23 08:38:03 2016 +0000

    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.

diff --git a/ed.c b/ed.c
index 4a872ed..ce19cf7 100644
--- a/ed.c
+++ b/ed.c
_AT_@ -63,6 +63,7 @@ static char *rhs;
 static char *lastmatch;
 static struct undo udata;
 static int newcmd;
+int eol, bol;
 
 static void
 discard(void)
_AT_@ -358,11 +359,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;
                 }
_AT_@ -1005,9 +1010,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;
Received on Sat Jan 23 2016 - 09:38:09 CET

This archive was generated by hypermail 2.3.0 : Sat Jan 23 2016 - 09:48:14 CET