static char * edit(const char *str, int len, const char *rep, int nmat, const int *ovec, char *res) { int i, slen, rlen; const int *mvec = ovec; int replen[MAXCAPTURE]; const char *repstr[MAXCAPTURE]; nmat--; ovec += 2; for (i = 0; i < nmat; i++) { replen[i] = ovec[i * 2 + 1] - ovec[i * 2]; repstr[i] = &str[ovec[i * 2]]; #ifdef REGEX_DEBUG printf(">>>%d %d %.*s\n", i, replen[i], replen[i], repstr[i]); #endif /* REGEX_DEBUG */ } slen = len; len -= mvec[1] - mvec[0]; len += rlen = findreplen(rep, nmat, replen); #ifdef REGEX_DEBUG printf("resulting length %d (srclen=%d)\n", len, slen); #endif /* REGEX_DEBUG */ if (mvec[0] > 0) { strncpy(cp, str, (mvec[0] > ep - cp - 1)? ep - cp - 1 :mvec[0]); cp += mvec[0]; } if(ep - cp < rlen) return NULL; doreplace(cp, rep, nmat, replen, repstr); cp += rlen; return res; }
static char * edit(const char *str, int len, const char *rep, int nmat, const int *ovec) { int i, slen, rlen; const int *mvec = ovec; char *res, *cp; int replen[MAXCAPTURE]; const char *repstr[MAXCAPTURE]; nmat--; ovec += 2; for (i = 0; i < nmat; i++) { replen[i] = ovec[i * 2 + 1] - ovec[i * 2]; repstr[i] = &str[ovec[i * 2]]; #ifdef DEBUG_PCRE_SUBST printf(">>>%d %d %.*s\n", i, replen[i], replen[i], repstr[i]); #endif } slen = len; len -= mvec[1] - mvec[0]; len += rlen = findreplen(rep, nmat, replen); #ifdef DEBUG_PCRE_SUBST printf("resulting length %d (srclen=%d)\n", len, slen); #endif cp = res = pcre_malloc(len + 1); if (mvec[0] > 0) { strncpy(cp, str, mvec[0]); cp += mvec[0]; } doreplace(cp, rep, nmat, replen, repstr); cp += rlen; if (mvec[1] < slen) strcpy(cp, &str[mvec[1]]); res[len] = 0; return res; }