libregexp: update from Plan 9
R=rsc https://codereview.appspot.com/10690044
This commit is contained in:
parent
da3ed55e4e
commit
3d31240bfd
5 changed files with 25 additions and 24 deletions
|
|
@ -15,6 +15,12 @@ struct Node
|
||||||
Reinst* last;
|
Reinst* last;
|
||||||
}Node;
|
}Node;
|
||||||
|
|
||||||
|
/* max character classes per program is nelem(reprog->class) */
|
||||||
|
static Reprog *reprog;
|
||||||
|
|
||||||
|
/* max rune ranges per character class is nelem(classp->spans)/2 */
|
||||||
|
#define NCCRUNE nelem(classp->spans)
|
||||||
|
|
||||||
#define NSTACK 20
|
#define NSTACK 20
|
||||||
static Node andstack[NSTACK];
|
static Node andstack[NSTACK];
|
||||||
static Node *andp;
|
static Node *andp;
|
||||||
|
|
@ -321,8 +327,8 @@ dump(Reprog *pp)
|
||||||
static Reclass*
|
static Reclass*
|
||||||
newclass(void)
|
newclass(void)
|
||||||
{
|
{
|
||||||
if(nclass >= NCLASS)
|
if(nclass >= nelem(reprog->class))
|
||||||
regerr2("too many character classes; limit", NCLASS+'0');
|
rcerror("too many character classes; increase Reprog.class size");
|
||||||
return &(classp[nclass++]);
|
return &(classp[nclass++]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -407,7 +413,7 @@ bldcclass(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* parse class into a set of spans */
|
/* parse class into a set of spans */
|
||||||
for(; ep<&r[NCCRUNE];){
|
while(ep < &r[NCCRUNE-1]){
|
||||||
if(rune == 0){
|
if(rune == 0){
|
||||||
rcerror("malformed '[]'");
|
rcerror("malformed '[]'");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -431,6 +437,10 @@ bldcclass(void)
|
||||||
}
|
}
|
||||||
quoted = nextc(&rune);
|
quoted = nextc(&rune);
|
||||||
}
|
}
|
||||||
|
if(ep >= &r[NCCRUNE-1]) {
|
||||||
|
rcerror("char class too large; increase Reclass.spans size");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* sort on span start */
|
/* sort on span start */
|
||||||
for(p = r; p < ep; p += 2){
|
for(p = r; p < ep; p += 2){
|
||||||
|
|
@ -454,9 +464,10 @@ bldcclass(void)
|
||||||
np[0] = *p++;
|
np[0] = *p++;
|
||||||
np[1] = *p++;
|
np[1] = *p++;
|
||||||
for(; p < ep; p += 2)
|
for(; p < ep; p += 2)
|
||||||
if(p[0] <= np[1]){
|
/* overlapping or adjacent ranges? */
|
||||||
if(p[1] > np[1])
|
if(p[0] <= np[1] + 1){
|
||||||
np[1] = p[1];
|
if(p[1] >= np[1])
|
||||||
|
np[1] = p[1]; /* coalesce */
|
||||||
} else {
|
} else {
|
||||||
np += 2;
|
np += 2;
|
||||||
np[0] = p[0];
|
np[0] = p[0];
|
||||||
|
|
|
||||||
|
|
@ -12,13 +12,6 @@ struct Resublist
|
||||||
Resub m[NSUBEXP];
|
Resub m[NSUBEXP];
|
||||||
};
|
};
|
||||||
|
|
||||||
/* max character classes per program */
|
|
||||||
extern Reprog RePrOg;
|
|
||||||
#define NCLASS (sizeof(RePrOg.class)/sizeof(Reclass))
|
|
||||||
|
|
||||||
/* max rune ranges per character class */
|
|
||||||
#define NCCRUNE (sizeof(Reclass)/sizeof(Rune))
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Actions and Tokens (Reinst types)
|
* Actions and Tokens (Reinst types)
|
||||||
*
|
*
|
||||||
|
|
@ -48,7 +41,7 @@ extern Reprog RePrOg;
|
||||||
* regexec execution lists
|
* regexec execution lists
|
||||||
*/
|
*/
|
||||||
#define LISTSIZE 10
|
#define LISTSIZE 10
|
||||||
#define BIGLISTSIZE (10*LISTSIZE)
|
#define BIGLISTSIZE (25*LISTSIZE)
|
||||||
typedef struct Relist Relist;
|
typedef struct Relist Relist;
|
||||||
struct Relist
|
struct Relist
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ regsub(char *sp, /* source string */
|
||||||
case '8':
|
case '8':
|
||||||
case '9':
|
case '9':
|
||||||
i = *sp-'0';
|
i = *sp-'0';
|
||||||
if(mp[i].s.sp != 0 && mp!=0 && ms>i)
|
if(mp!=0 && mp[i].s.sp != 0 && ms>i)
|
||||||
for(ssp = mp[i].s.sp;
|
for(ssp = mp[i].s.sp;
|
||||||
ssp < mp[i].e.ep;
|
ssp < mp[i].e.ep;
|
||||||
ssp++)
|
ssp++)
|
||||||
|
|
@ -46,9 +46,8 @@ regsub(char *sp, /* source string */
|
||||||
*dp++ = *sp;
|
*dp++ = *sp;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}else if(*sp == '&'){
|
}else if(*sp == '&'){
|
||||||
if(mp[0].s.sp != 0 && mp!=0 && ms>0)
|
if(mp!=0 && mp[0].s.sp != 0 && ms>0)
|
||||||
if(mp[0].s.sp != 0)
|
|
||||||
for(ssp = mp[0].s.sp;
|
for(ssp = mp[0].s.sp;
|
||||||
ssp < mp[0].e.ep; ssp++)
|
ssp < mp[0].e.ep; ssp++)
|
||||||
if(dp < ep)
|
if(dp < ep)
|
||||||
|
|
|
||||||
|
|
@ -22,17 +22,16 @@ struct x t[] = {
|
||||||
{ 0, 0, 0 },
|
{ 0, 0, 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int
|
||||||
main(int ac, char **av)
|
main(int ac, char **av)
|
||||||
{
|
{
|
||||||
Resub rs[10];
|
Resub rs[10];
|
||||||
char dst[128];
|
char dst[128];
|
||||||
int n;
|
|
||||||
struct x *tp;
|
struct x *tp;
|
||||||
|
|
||||||
for(tp = t; tp->re; tp++)
|
for(tp = t; tp->re; tp++)
|
||||||
tp->p = regcomp(tp->re);
|
tp->p = regcomp(tp->re);
|
||||||
|
|
||||||
|
|
||||||
for(tp = t; tp->re; tp++){
|
for(tp = t; tp->re; tp++){
|
||||||
print("%s VIA %s", av[1], tp->re);
|
print("%s VIA %s", av[1], tp->re);
|
||||||
memset(rs, 0, sizeof rs);
|
memset(rs, 0, sizeof rs);
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,19 @@
|
||||||
#include "lib9.h"
|
#include "lib9.h"
|
||||||
#include <regexp9.h>
|
#include <regexp9.h>
|
||||||
|
|
||||||
|
int
|
||||||
main(int ac, char **av)
|
main(int ac, char **av)
|
||||||
{
|
{
|
||||||
Resub rs[10];
|
Resub rs[10];
|
||||||
Reprog *p;
|
Reprog *p;
|
||||||
char *s;
|
char *s;
|
||||||
int i;
|
|
||||||
|
|
||||||
p = regcomp("[^a-z]");
|
p = regcomp("[^a-z]");
|
||||||
s = "\n";
|
s = "\n";
|
||||||
if(regexec(p, s, rs, 10))
|
if(regexec(p, s, rs, 10))
|
||||||
print("%s %lux %lux %lux\n", s, s, rs[0].sp, rs[0].ep);
|
print("%s %lux %lux %lux\n", s, s, rs[0].s.sp, rs[0].e.ep);
|
||||||
s = "0";
|
s = "0";
|
||||||
if(regexec(p, s, rs, 10))
|
if(regexec(p, s, rs, 10))
|
||||||
print("%s %lux %lux %lux\n", s, s, rs[0].sp, rs[0].ep);
|
print("%s %lux %lux %lux\n", s, s, rs[0].s.sp, rs[0].e.ep);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue