wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

regsub.c (1130B)


      1 #include "plan9.h"
      2 #include "regexp9.h"
      3 
      4 /* substitute into one string using the matches from the last regexec() */
      5 extern	void
      6 regsub(char *sp,	/* source string */
      7 	char *dp,	/* destination string */
      8 	int dlen,
      9 	Resub *mp,	/* subexpression elements */
     10 	int ms)		/* number of elements pointed to by mp */
     11 {
     12 	char *ssp, *ep;
     13 	int i;
     14 
     15 	ep = dp+dlen-1;
     16 	while(*sp != '\0'){
     17 		if(*sp == '\\'){
     18 			switch(*++sp){
     19 			case '0':
     20 			case '1':
     21 			case '2':
     22 			case '3':
     23 			case '4':
     24 			case '5':
     25 			case '6':
     26 			case '7':
     27 			case '8':
     28 			case '9':
     29 				i = *sp-'0';
     30 				if(mp[i].s.sp != 0 && mp!=0 && ms>i)
     31 					for(ssp = mp[i].s.sp;
     32 					     ssp < mp[i].e.ep;
     33 					     ssp++)
     34 						if(dp < ep)
     35 							*dp++ = *ssp;
     36 				break;
     37 			case '\\':
     38 				if(dp < ep)
     39 					*dp++ = '\\';
     40 				break;
     41 			case '\0':
     42 				sp--;
     43 				break;
     44 			default:
     45 				if(dp < ep)
     46 					*dp++ = *sp;
     47 				break;
     48 			}
     49 		}else if(*sp == '&'){
     50 			if(mp[0].s.sp != 0 && mp!=0 && ms>0)
     51 			if(mp[0].s.sp != 0)
     52 				for(ssp = mp[0].s.sp;
     53 				     ssp < mp[0].e.ep; ssp++)
     54 					if(dp < ep)
     55 						*dp++ = *ssp;
     56 		}else{
     57 			if(dp < ep)
     58 				*dp++ = *sp;
     59 		}
     60 		sp++;
     61 	}
     62 	*dp = '\0';
     63 }