6926
6906
icase - flag, if set to 1 the match is case insensitive
6928
6908
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6929
char *replace, char *string, int icase)
6909
char *replace, char *in_string, int icase)
6932
my_regmatch_t *subs;
6936
int buf_len, need_buf_len;
6937
int cflags= REG_EXTENDED;
6939
char *res_p,*str_p,*str_end;
6941
buf_len= *buf_len_p;
6942
len= strlen(string);
6943
str_end= string + len;
6945
/* start with a buffer of a reasonable size that hopefully will not
6946
need to be reallocated
6948
need_buf_len= len * 2 + 1;
6911
string string_to_match(in_string);
6912
pcrecpp::RE_Options opt;
6956
if ((err_code= my_regcomp(&r,pattern,cflags,&my_charset_latin1)))
6958
check_regerr(&r,err_code);
6962
subs= (my_regmatch_t*)my_malloc(sizeof(my_regmatch_t) * (r.re_nsub+1),
6963
MYF(MY_WME+MY_FAE));
6967
replace_end= replace + strlen(replace);
6969
/* for each pattern match instance perform a replacement */
6972
/* find the match */
6973
err_code= my_regexec(&r,str_p, r.re_nsub+1, subs,
6974
(str_p == string) ? REG_NOTBOL : 0);
6976
/* if regular expression error (eg. bad syntax, or out of memory) */
6977
if (err_code && err_code != REG_NOMATCH)
6979
check_regerr(&r,err_code);
6984
/* if match found */
6987
char* expr_p= replace;
6991
we need at least what we have so far in the buffer + the part
6994
need_buf_len= (res_p - buf) + (int) subs[0].rm_so;
6996
/* on this pass, calculate the memory for the result buffer */
6997
while (expr_p < replace_end)
6999
int back_ref_num= -1;
7002
if (c == '\\' && expr_p + 1 < replace_end)
7004
back_ref_num= (int) (expr_p[1] - '0');
7007
/* found a valid back_ref (eg. \1)*/
7008
if (back_ref_num >= 0 && back_ref_num <= (int)r.re_nsub)
7010
regoff_t start_off, end_off;
7011
if ((start_off=subs[back_ref_num].rm_so) > -1 &&
7012
(end_off=subs[back_ref_num].rm_eo) > -1)
7014
need_buf_len += (int) (end_off - start_off);
7026
now that we know the size of the buffer,
7027
make sure it is big enough
7031
/* copy the pre-match part */
7034
memcpy(res_p, str_p, (size_t) subs[0].rm_so);
7035
res_p+= subs[0].rm_so;
7040
/* copy the match and expand back_refs */
7041
while (expr_p < replace_end)
7043
int back_ref_num= -1;
7046
if (c == '\\' && expr_p + 1 < replace_end)
7048
back_ref_num= expr_p[1] - '0';
7051
if (back_ref_num >= 0 && back_ref_num <= (int)r.re_nsub)
7053
regoff_t start_off, end_off;
7054
if ((start_off=subs[back_ref_num].rm_so) > -1 &&
7055
(end_off=subs[back_ref_num].rm_eo) > -1)
7057
int block_len= (int) (end_off - start_off);
7058
memcpy(res_p,str_p + start_off, block_len);
7065
*res_p++ = *expr_p++;
7069
/* handle the post-match part */
7070
if (subs[0].rm_so == subs[0].rm_eo)
7072
if (str_p + subs[0].rm_so >= str_end)
7074
str_p += subs[0].rm_eo ;
7075
*res_p++ = *str_p++;
7079
str_p += subs[0].rm_eo;
7082
else /* no match this time, just copy the string as is */
7084
int left_in_str= str_end-str_p;
7085
need_buf_len= (res_p-buf) + left_in_str;
7087
memcpy(res_p,str_p,left_in_str);
7088
res_p += left_in_str;
7092
my_free(subs, MYF(0));
7096
*buf_len_p= buf_len;
6915
opt.set_caseless(true);
6917
if (!pcrecpp::RE(pattern, opt).Replace(replace,&string_to_match)){
6921
const char * new_str= string_to_match.c_str();
6922
*buf_len_p= strlen(new_str);
6923
char * new_buf = (char *)malloc(*buf_len_p+1);
6924
if (new_buf == NULL)
6928
strcpy(new_buf, new_str);