factor out matched branch

This commit is contained in:
Kyryl Melekhin
2021-11-25 16:54:00 +00:00
parent d6b335fdb8
commit e715d8cceb
2 changed files with 31 additions and 23 deletions

39
pike.c
View File

@@ -79,6 +79,7 @@ enum
RSPLIT, RSPLIT,
// Other (special) instructions // Other (special) instructions
SAVE, SAVE,
MATCHCONT,
}; };
// Return codes for re_sizecode() and re_comp() // Return codes for re_sizecode() and re_comp()
@@ -418,7 +419,7 @@ syntax_error:
int re_sizecode(const char *re, int *nsub) int re_sizecode(const char *re, int *nsub)
{ {
rcode dummyprog; rcode dummyprog;
dummyprog.unilen = 3; dummyprog.unilen = 4;
dummyprog.sub = 0; dummyprog.sub = 0;
int res = _compilecode(&re, &dummyprog, /*sizecode*/1); 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++] = SAVE;
prog->insts[prog->unilen++] = prog->sub + 1; prog->insts[prog->unilen++] = prog->sub + 1;
prog->insts[prog->unilen++] = MATCH; prog->insts[prog->unilen++] = MATCH;
prog->insts[prog->unilen] = MATCHCONT;
prog->len += 2; prog->len += 2;
return RE_SUCCESS; return RE_SUCCESS;
@@ -562,6 +564,12 @@ if (spc == SPLIT) { \
} inst##list(nn) \ } inst##list(nn) \
deccheck(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 re_pikevm(rcode *prog, const char *s, const char **subp, int nsubp)
{ {
int rsubsize = sizeof(rsub)+(sizeof(char*)*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; suboff = 0;
} }
matched = nsub; 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) decref(nsub)
} }
break_for:
if (sp == _sp) if (sp == _sp)
break; break;
tmp = clist; swaplist()
clist = nlist;
nlist = tmp;
clistidx = nlistidx;
if (!matched) {
jmp_start: jmp_start:
newsub(memset(s1->sub, 0, osubp);, /*nop*/) newsub(memset(s1->sub, 0, osubp);, /*nop*/)
s1->ref = 1; s1->ref = 1;
s1->sub[0] = _sp; s1->sub[0] = _sp;
nsub = s1; npc = insts; nsub = s1; npc = insts;
addthread(1, clist, clistidx) addthread(1, clist, clistidx)
} else if (!clistidx) _continue:;
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;
} }
return 0; return 0;
} }

View File

@@ -154,6 +154,7 @@ aaaaa(aa)aa(aa(a)a)?aa
((.a|.ab)(bc.|c.)|abc.) ((.a|.ab)(bc.|c.)|abc.)
\\\\\\\\< \\\\\\\\<
\\\\\\\\\\\\\\\\< \\\\\\\\\\\\\\\\<
[^a]*b
" "
input="\ input="\
abcdef abcdef
@@ -309,6 +310,7 @@ aa
xabcx xabcx
\\< \\<
\\\\\\\\< \\\\\\\\<
hhagbdbdbjsjjjda..b
" "
expect="\ expect="\
(0,3) (0,3)
@@ -464,6 +466,7 @@ expect="\
(0,5)(0,5)(0,2)(2,5) (0,5)(0,5)(0,2)(2,5)
(0,2) (0,2)
(2,5) (2,5)
(3,9)
(0,0) (0,0)
" "