9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

regaux.c (2089B)


      1 #include "lib9.h"
      2 #include "regexp9.h"
      3 #include "regcomp.h"
      4 
      5 
      6 /*
      7  *  save a new match in mp
      8  */
      9 extern void
     10 _renewmatch(Resub *mp, int ms, Resublist *sp)
     11 {
     12 	int i;
     13 
     14 	if(mp==0 || ms<=0)
     15 		return;
     16 	if(mp[0].s.sp==0 || sp->m[0].s.sp<mp[0].s.sp ||
     17 	   (sp->m[0].s.sp==mp[0].s.sp && sp->m[0].e.ep>mp[0].e.ep)){
     18 		for(i=0; i<ms && i<NSUBEXP; i++)
     19 			mp[i] = sp->m[i];
     20 		for(; i<ms; i++)
     21 			mp[i].s.sp = mp[i].e.ep = 0;
     22 	}
     23 }
     24 
     25 /*
     26  * Note optimization in _renewthread:
     27  * 	*lp must be pending when _renewthread called; if *l has been looked
     28  *		at already, the optimization is a bug.
     29  */
     30 extern Relist*
     31 _renewthread(Relist *lp,	/* _relist to add to */
     32 	Reinst *ip,		/* instruction to add */
     33 	int ms,
     34 	Resublist *sep)		/* pointers to subexpressions */
     35 {
     36 	Relist *p;
     37 
     38 	for(p=lp; p->inst; p++){
     39 		if(p->inst == ip){
     40 			if(sep->m[0].s.sp < p->se.m[0].s.sp){
     41 				if(ms > 1)
     42 					p->se = *sep;
     43 				else
     44 					p->se.m[0] = sep->m[0];
     45 			}
     46 			return 0;
     47 		}
     48 	}
     49 	p->inst = ip;
     50 	if(ms > 1)
     51 		p->se = *sep;
     52 	else
     53 		p->se.m[0] = sep->m[0];
     54 	(++p)->inst = 0;
     55 	return p;
     56 }
     57 
     58 /*
     59  * same as renewthread, but called with
     60  * initial empty start pointer.
     61  */
     62 extern Relist*
     63 _renewemptythread(Relist *lp,	/* _relist to add to */
     64 	Reinst *ip,		/* instruction to add */
     65 	int ms,
     66 	char *sp)		/* pointers to subexpressions */
     67 {
     68 	Relist *p;
     69 
     70 	for(p=lp; p->inst; p++){
     71 		if(p->inst == ip){
     72 			if(sp < p->se.m[0].s.sp) {
     73 				if(ms > 1)
     74 					memset(&p->se, 0, sizeof(p->se));
     75 				p->se.m[0].s.sp = sp;
     76 			}
     77 			return 0;
     78 		}
     79 	}
     80 	p->inst = ip;
     81 	if(ms > 1)
     82 		memset(&p->se, 0, sizeof(p->se));
     83 	p->se.m[0].s.sp = sp;
     84 	(++p)->inst = 0;
     85 	return p;
     86 }
     87 
     88 extern Relist*
     89 _rrenewemptythread(Relist *lp,	/* _relist to add to */
     90 	Reinst *ip,		/* instruction to add */
     91 	int ms,
     92 	Rune *rsp)		/* pointers to subexpressions */
     93 {
     94 	Relist *p;
     95 
     96 	for(p=lp; p->inst; p++){
     97 		if(p->inst == ip){
     98 			if(rsp < p->se.m[0].s.rsp) {
     99 				if(ms > 1)
    100 					memset(&p->se, 0, sizeof(p->se));
    101 				p->se.m[0].s.rsp = rsp;
    102 			}
    103 			return 0;
    104 		}
    105 	}
    106 	p->inst = ip;
    107 	if(ms > 1)
    108 		memset(&p->se, 0, sizeof(p->se));
    109 	p->se.m[0].s.rsp = rsp;
    110 	(++p)->inst = 0;
    111 	return p;
    112 }