From e715d8ccebe47d8f1048e44ec00775d1b010ed10 Mon Sep 17 00:00:00 2001 From: Kyryl Melekhin Date: Thu, 25 Nov 2021 16:54:00 +0000 Subject: [PATCH] factor out matched branch --- pike.c | 51 ++++++++++++++++++++++++++++----------------------- test.sh | 3 +++ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/pike.c b/pike.c index 9e155fa..7d5567d 100644 --- a/pike.c +++ b/pike.c @@ -79,6 +79,7 @@ enum RSPLIT, // Other (special) instructions SAVE, + MATCHCONT, }; // Return codes for re_sizecode() and re_comp() @@ -418,7 +419,7 @@ syntax_error: int re_sizecode(const char *re, int *nsub) { rcode dummyprog; - dummyprog.unilen = 3; + dummyprog.unilen = 4; dummyprog.sub = 0; int res = _compilecode(&re, &dummyprog, /*sizecode*/1); @@ -445,6 +446,7 @@ int re_comp(rcode *prog, const char *re, int nsubs) prog->insts[prog->unilen++] = SAVE; prog->insts[prog->unilen++] = prog->sub + 1; prog->insts[prog->unilen++] = MATCH; + prog->insts[prog->unilen] = MATCHCONT; prog->len += 2; return RE_SUCCESS; @@ -562,6 +564,12 @@ if (spc == SPLIT) { \ } inst##list(nn) \ deccheck(nn) \ +#define swaplist() \ +tmp = clist; \ +clist = nlist; \ +nlist = tmp; \ +clistidx = nlistidx; \ + int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) { int rsubsize = sizeof(rsub)+(sizeof(char*)*nsubp); @@ -604,33 +612,30 @@ int re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp) suboff = 0; } matched = nsub; - goto break_for; + case MATCHCONT: + if (sp == _sp || !nlistidx) { + for (i = 0, j = i; i < nsubp; i+=2, j++) { + subp[i] = matched->sub[j]; + subp[i+1] = matched->sub[nsubp / 2 + j]; + } + return 1; + } + nlist[nlistidx++].pc = &insts[prog->unilen]; + swaplist() + goto _continue; } decref(nsub) } - break_for: if (sp == _sp) break; - tmp = clist; - clist = nlist; - nlist = tmp; - clistidx = nlistidx; - if (!matched) { - jmp_start: - newsub(memset(s1->sub, 0, osubp);, /*nop*/) - s1->ref = 1; - s1->sub[0] = _sp; - nsub = s1; npc = insts; - addthread(1, clist, clistidx) - } else if (!clistidx) - break; - } - if (matched) { - for (i = 0, j = i; i < nsubp; i+=2, j++) { - subp[i] = matched->sub[j]; - subp[i+1] = matched->sub[nsubp / 2 + j]; - } - return 1; + swaplist() + jmp_start: + newsub(memset(s1->sub, 0, osubp);, /*nop*/) + s1->ref = 1; + s1->sub[0] = _sp; + nsub = s1; npc = insts; + addthread(1, clist, clistidx) + _continue:; } return 0; } diff --git a/test.sh b/test.sh index 86efadd..5d79c98 100755 --- a/test.sh +++ b/test.sh @@ -154,6 +154,7 @@ aaaaa(aa)aa(aa(a)a)?aa ((.a|.ab)(bc.|c.)|abc.) \\\\\\\\< \\\\\\\\\\\\\\\\< +[^a]*b " input="\ abcdef @@ -309,6 +310,7 @@ aa xabcx \\< \\\\\\\\< +hhagbdbdbjsjjjda..b " expect="\ (0,3) @@ -464,6 +466,7 @@ expect="\ (0,5)(0,5)(0,2)(2,5) (0,2) (2,5) +(3,9) (0,0) "