]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/libregexp/rregexec.c
libregex: fix sed regression (thans spew)
[plan9front.git] / sys / src / libregexp / rregexec.c
index 5727187331e7f686d49206e0e6f0fa8e2f69a8c2..b1320de4bfe50dd16b154066c64efc3404cb937e 100644 (file)
@@ -14,10 +14,10 @@ int
 rregexec(Reprog *prog, Rune *str, Resub *sem, int msize)
 {
        RethreadQ lists[2], *clist, *nlist, *tmp;
-       Rethread *t, *nextthr, **availthr;
+       Rethread *t, *next, *pooltop, *avail;
        Reinst *curinst;
        Rune *rsp, *rep, endr, last;
-       int i, match, first, gen, pri, matchpri;
+       int match, first, gen, pri, matchpri;
 
        if(msize > NSUBEXPM)
                msize = NSUBEXPM;
@@ -34,9 +34,8 @@ rregexec(Reprog *prog, Rune *str, Resub *sem, int msize)
        nlist->head = nil;
        nlist->tail = &nlist->head;
 
-       for(i = 0; i < prog->nthr; i++)
-               prog->thrpool[i] = prog->threads + i;
-       availthr = prog->thrpool + prog->nthr;
+       pooltop = prog->threads + prog->nthr;
+       avail = nil;
 
        pri = matchpri = gen = match = 0;
        rsp = str;
@@ -69,15 +68,14 @@ Again:
                        if(*rsp != curinst->r)
                                goto Done;
                case OANY: /* fallthrough */
-               Any:
-                       nextthr = t->next;
+                       next = t->next;
                        t->pc = curinst + 1;
                        t->next = nil;
                        *nlist->tail = t;
                        nlist->tail = &t->next;
-                       if(nextthr == nil)
+                       if(next == nil)
                                break;
-                       t = nextthr;
+                       t = next;
                        curinst = t->pc;
                        goto Again;
                case OCLASS:
@@ -88,14 +86,14 @@ Again:
                                curinst++;
                                goto Class;
                        }
-                       nextthr = t->next;
+                       next = t->next;
                        t->pc = curinst->a;
                        t->next = nil;
                        *nlist->tail = t;
                        nlist->tail = &t->next;
-                       if(nextthr == nil)
+                       if(next == nil)
                                break;
-                       t = nextthr;
+                       t = next;
                        curinst = t->pc;
                        goto Again;
                case ONOTNL:
@@ -111,24 +109,27 @@ Again:
                        }
                        goto Done;
                case OEOL:
-                       if(*rsp == L'\0' && rep == nil) {
+                       if(*rsp == '\n' || *rsp == L'\0' && rep == nil) {
                                curinst++;
                                goto Again;
                        }
-                       if(*rsp == '\n')
-                               goto Any;
                        goto Done;
                case OJMP:
                        curinst = curinst->a;
                        goto Again;
                case OSPLIT:
-                       nextthr = *--availthr;
-                       nextthr->pc = curinst->b;
+                       if(avail == nil)
+                               next = --pooltop;
+                       else {
+                               next = avail;
+                               avail = avail->next;
+                       }
+                       next->pc = curinst->b;
                        if(msize > 0)
-                               memcpy(nextthr->sem, t->sem, sizeof(Resub)*msize);
-                       nextthr->pri = t->pri;
-                       nextthr->next = t->next;
-                       t->next = nextthr;
+                               memcpy(next->sem, t->sem, sizeof(Resub)*msize);
+                       next->pri = t->pri;
+                       next->next = t->next;
+                       t->next = next;
                        curinst = curinst->a;
                        goto Again;
                case OSAVE:
@@ -154,10 +155,12 @@ Again:
                        curinst++;
                        goto Again;
                Done:
-                       *availthr++ = t;
-                       t = t->next;
-                       if(t == nil)
+                       next = t->next;
+                       t->next = avail;
+                       avail = t;
+                       if(next == nil)
                                break;
+                       t = next;
                        curinst = t->pc;
                        goto Again;
                }
@@ -165,7 +168,12 @@ Start:
                /* Start again once if we haven't found anything. */
                if(first == 1 && match == 0) {
                        first = 0;
-                       t = *--availthr;
+                       if(avail == nil)
+                               t = --pooltop;
+                       else {
+                               t = avail;
+                               avail = avail->next;
+                       }
                        if(msize > 0)
                                memset(t->sem, 0, sizeof(Resub)*msize);
                        /* "Lower" priority thread */