library/src/utility/BP-StringUtilities.cc
// Regexp find and replace all occurances of replace_this in in_buff.  Returns new pointer with replacements made.
// This function does not modify in_buff directly.  Returns null if no replacements are made.
Regexp find and replace all occurances of replace_this in in_buff.  Returns new pointer with replacements made.
This function does not modify in_buff directly.  Returns null if no replacements are made.
Regexp find and replace all occurances of replace_this in in_buff . Returns new pointer with replacements made . This function does not modify in_buff directly . Returns null if no replacements are made .
char * BP_RegFindReplace(char *in_buff, char *replace_this, char *replace_with, size_t replace_size, size_t max_replacements, size_t regexp_flags)
{
    P_BP_REGEX_MATCH_INFO match_info = (P_BP_REGEX_MATCH_INFO) bpcalloc(sizeof(BP_REGEX_MATCH_INFO), 1);
    BP_ERROR_T match = BP_MatchRegularExpression(in_buff, bpstrlen(in_buff), replace_this, regexp_flags, max_replacements, match_info );
    BP_DestroyRegexpMatchInfo(match_info);
	bpfree(match_info);
	match_info = NULL;
    if(match == ERR_FAILURE)
    {
    	return NULL;
    }
    char *out_buff = NULL;
    char *tmp = in_buff;
    size_t string_length = 0;
    size_t replacement_count = 0;
    char * str_remainder = NULL;
    match_info = (P_BP_REGEX_MATCH_INFO) bpcalloc(sizeof(BP_REGEX_MATCH_INFO), 1);
    while(BP_MatchRegularExpression(tmp, bpstrlen(tmp), replace_this, regexp_flags, max_replacements, match_info ) == ERR_SUCCESS)
    {
    	string_length += match_info->matches[0].rm_so + replace_size;
    	out_buff = (char *) bprealloc
    				(
						out_buff,
						string_length + 100
					);
    	size_t start_offs = string_length - (match_info->matches[0].rm_so + replace_size);
    	memset(&out_buff[start_offs], 0x00, (match_info->matches[0].rm_so + replace_size + 100));
    	strncat(out_buff, tmp, match_info->matches[0].rm_so);
    	if(replace_with)
    		strncat(out_buff, replace_with, replace_size );
    	replacement_count++;
    	if(!out_buff)
    	{
    		BP_DestroyRegexpMatchInfo(match_info);
    		bpfree(match_info);
			return NULL;
    	}
    	tmp = &tmp[match_info->matches[0].rm_eo];
    	str_remainder = tmp;
    	BP_DestroyRegexpMatchInfo(match_info);
    	bpfree(match_info);
    	match_info = (P_BP_REGEX_MATCH_INFO) bpcalloc(sizeof(BP_REGEX_MATCH_INFO), 1);
    }
    if(out_buff)
    {
    	size_t remainder_len = bpstrlen(str_remainder);
    	out_buff = (char *) bprealloc(out_buff, bpstrlen(out_buff) + remainder_len + 1);
    	memset(&out_buff[bpstrlen(out_buff)], 0x00, remainder_len+1);
    	strncat(out_buff, str_remainder, remainder_len);
    }
    if(match_info)
    {
    	BP_DestroyRegexpMatchInfo(match_info);
    	bpfree(match_info);
    }
    return out_buff;
}
char * BP_RegFindReplace ( char * in_buff , char * replace_this , char * replace_with , size_t replace_size , size_t max_replacements , size_t regexp_flags ) { P_BP_REGEX_MATCH_INFO match_info = ( P_BP_REGEX_MATCH_INFO ) bpcalloc ( sizeof ( BP_REGEX_MATCH_INFO ) , 1 ) ; BP_ERROR_T match = BP_MatchRegularExpression ( in_buff , bpstrlen ( in_buff ) , replace_this , regexp_flags , max_replacements , match_info ) ; BP_DestroyRegexpMatchInfo ( match_info ) ; bpfree ( match_info ) ; match_info = NULL ; if ( match == ERR_FAILURE ) { return NULL ; } char * out_buff = NULL ; char * tmp = in_buff ; size_t string_length = 0 ; size_t replacement_count = 0 ; char * str_remainder = NULL ; match_info = ( P_BP_REGEX_MATCH_INFO ) bpcalloc ( sizeof ( BP_REGEX_MATCH_INFO ) , 1 ) ; while ( BP_MatchRegularExpression ( tmp , bpstrlen ( tmp ) , replace_this , regexp_flags , max_replacements , match_info ) == ERR_SUCCESS ) { string_length += match_info -> matches [ 0 ] . rm_so + replace_size ; out_buff = ( char * ) bprealloc ( out_buff , string_length + 100 ) ; size_t start_offs = string_length - ( match_info -> matches [ 0 ] . rm_so + replace_size ) ; memset ( & out_buff [ start_offs ] , 0x00 , ( match_info -> matches [ 0 ] . rm_so + replace_size + 100 ) ) ; strncat ( out_buff , tmp , match_info -> matches [ 0 ] . rm_so ) ; if ( replace_with ) strncat ( out_buff , replace_with , replace_size ) ; replacement_count ++ ; if ( ! out_buff ) { BP_DestroyRegexpMatchInfo ( match_info ) ; bpfree ( match_info ) ; return NULL ; } tmp = & tmp [ match_info -> matches [ 0 ] . rm_eo ] ; str_remainder = tmp ; BP_DestroyRegexpMatchInfo ( match_info ) ; bpfree ( match_info ) ; match_info = ( P_BP_REGEX_MATCH_INFO ) bpcalloc ( sizeof ( BP_REGEX_MATCH_INFO ) , 1 ) ; } if ( out_buff ) { size_t remainder_len = bpstrlen ( str_remainder ) ; out_buff = ( char * ) bprealloc ( out_buff , bpstrlen ( out_buff ) + remainder_len + 1 ) ; memset ( & out_buff [ bpstrlen ( out_buff ) ] , 0x00 , remainder_len + 1 ) ; strncat ( out_buff , str_remainder , remainder_len ) ; } if ( match_info ) { BP_DestroyRegexpMatchInfo ( match_info ) ; bpfree ( match_info ) ; } return out_buff ; }
Regexp find and replace all occurances of replace_this in in_buff.
Regexp find and replace all occurances of replace_this in in_buff .
// temporary match info structure // attempt to make a match // destroy match info immediately // no match made // buffer which will contain replacments, returned to user. // set temp pointer to current position // for monitoring string length // replacement count // used for storage of the remainder of the string // recreate match info // as long as we match regular expressions, make replacements // increase string length by start offset + replacement size + 1 // bpreallocate buffer on loop // this is the write start offset used for writing into the existing outbuff // zero out new memory (start of new data) // reproduce string in buffer // only replace if NON null, if its null omit the replacement but still record the deletion // replacement count /*
    	printf("\n Write Start Offset: %u", start_offs);
    	printf("\n Got outbuff after alloc: %s - %u", out_buff, replace_size);
    	printf("\n Replacing with: %s", replace_with);
    	printf("\n-");
		*/ // if oom, return null // matches end of string (moves match forward) // set remainder in the case we're at the loop end // destroy match info on loop // copy the remainder of the data into the outbuff // get the string remainder // bpreallocate output buffer // concatenate remainder // destroy match information if found // return the new pointer