library/src/utility/BP-StringUtilities.cc
// perl compatible regular expression find and replace routine.
perl compatible regular expression find and replace routine.
perl compatible regular expression find and replace routine .
char * BP_RegFindReplacePCRE
(
		char * in_buff,
		size_t buff_len,
		char * replace_this,
		char * replace_with,
		size_t replace_size,
		size_t max_replacements,
		size_t regexp_flags
)
{
	if(!in_buff)
		return NULL;
	if(!buff_len)
		return NULL;
	if(!replace_this)
		return NULL;
	if(!replace_with)
		return NULL;
	if(!replace_size)
		return NULL;
	if(!max_replacements)
		return NULL;
	BP_REGEX_MATCH_INFO match_info;
	memset(&match_info, 0x00, sizeof(match_info));
	size_t matched = BP_MatchRegularExpressionPCRE
	(
			in_buff,
			buff_len,
			replace_this,
			regexp_flags,
			max_replacements,
			&match_info,
			true
	);
	if(!matched)
		return NULL;
	if(!match_info.preg_matches)
		return NULL;
	if(!match_info.pcre_num_matched)
		return NULL;
	char * out_str = NULL;
	size_t n = 0;
	size_t buff_len_sub = 0;
	size_t buff_len_add = 0;
	for(; n < match_info.pcre_num_matched; n++)
	{
		buff_len_sub += match_info.preg_matches[n].match_len;
		buff_len_add += replace_size+4;
	}
	size_t buff_new_len = 0;
	buff_new_len =  buff_len - buff_len_sub;
	buff_new_len += buff_len_add + 1;
	buff_new_len += buff_len_sub;
	out_str = (char * ) bpcalloc(buff_new_len, 1);
	size_t buff_write_index = 0;
	size_t buff_read_index  = 0;
	size_t buff_read_len    = 0;
	size_t outbuff_write_position = 0;
	size_t inbuff_read_position   = 0;
	for
	(
		n = 0;
		n < match_info.pcre_num_matched;
		n++
	)
	{
		if(!match_info.preg_matches[n].match_index)
		{
			memcpy((void *) &out_str[outbuff_write_position], replace_with, replace_size);
			outbuff_write_position += replace_size;
			inbuff_read_position   += match_info.preg_matches[n].match_len;
			size_t copy_distance_between_replace_and_next_index = 0;
			if((n+1) < match_info.pcre_num_matched)
			{
				copy_distance_between_replace_and_next_index = (match_info.preg_matches[n+1].match_index - match_info.preg_matches[n].match_index) - match_info.preg_matches[n+1].match_len;
				if(copy_distance_between_replace_and_next_index)
				{
					memcpy((void *) &out_str[outbuff_write_position], &in_buff[inbuff_read_position], copy_distance_between_replace_and_next_index);
					inbuff_read_position   += copy_distance_between_replace_and_next_index;
					outbuff_write_position += copy_distance_between_replace_and_next_index;
				}
			}
			continue;
		}
		if(match_info.preg_matches[n].match_index)
		{
			if(!outbuff_write_position)
			{
				memcpy((void *) &out_str[outbuff_write_position], &in_buff[inbuff_read_position], match_info.preg_matches[n].match_index);
				inbuff_read_position   += match_info.preg_matches[n].match_index;
				outbuff_write_position += match_info.preg_matches[n].match_index;
			}
			memcpy((void *) &out_str[outbuff_write_position], replace_with, replace_size);
			outbuff_write_position += replace_size;
			inbuff_read_position   += match_info.preg_matches[n].match_len;
			size_t copy_distance_between_replace_and_next_index = 0;
			if((n+1) < match_info.pcre_num_matched)
			{
				copy_distance_between_replace_and_next_index = (match_info.preg_matches[n+1].match_index - match_info.preg_matches[n].match_index) - match_info.preg_matches[n+1].match_len;
				if(copy_distance_between_replace_and_next_index)
				{
					memcpy((void *) &out_str[outbuff_write_position], &in_buff[inbuff_read_position], copy_distance_between_replace_and_next_index);
					inbuff_read_position   += copy_distance_between_replace_and_next_index;
					outbuff_write_position += copy_distance_between_replace_and_next_index;
				}
			}
			continue;
		}
	}
	if(bpstrlen(&in_buff[inbuff_read_position]))
	{
		memcpy((void *) &out_str[outbuff_write_position], &in_buff[inbuff_read_position], (buff_len - inbuff_read_position));
	}
	BP_DestroyRegexpMatchInfo(&match_info);
	return out_str;
}
char * BP_RegFindReplacePCRE ( char * in_buff , size_t buff_len , char * replace_this , char * replace_with , size_t replace_size , size_t max_replacements , size_t regexp_flags ) { if ( ! in_buff ) return NULL ; if ( ! buff_len ) return NULL ; if ( ! replace_this ) return NULL ; if ( ! replace_with ) return NULL ; if ( ! replace_size ) return NULL ; if ( ! max_replacements ) return NULL ; BP_REGEX_MATCH_INFO match_info ; memset ( & match_info , 0x00 , sizeof ( match_info ) ) ; size_t matched = BP_MatchRegularExpressionPCRE ( in_buff , buff_len , replace_this , regexp_flags , max_replacements , & match_info , true ) ; if ( ! matched ) return NULL ; if ( ! match_info . preg_matches ) return NULL ; if ( ! match_info . pcre_num_matched ) return NULL ; char * out_str = NULL ; size_t n = 0 ; size_t buff_len_sub = 0 ; size_t buff_len_add = 0 ; for ( ; n < match_info . pcre_num_matched ; n ++ ) { buff_len_sub += match_info . preg_matches [ n ] . match_len ; buff_len_add += replace_size + 4 ; } size_t buff_new_len = 0 ; buff_new_len = buff_len - buff_len_sub ; buff_new_len += buff_len_add + 1 ; buff_new_len += buff_len_sub ; out_str = ( char * ) bpcalloc ( buff_new_len , 1 ) ; size_t buff_write_index = 0 ; size_t buff_read_index = 0 ; size_t buff_read_len = 0 ; size_t outbuff_write_position = 0 ; size_t inbuff_read_position = 0 ; for ( n = 0 ; n < match_info . pcre_num_matched ; n ++ ) { if ( ! match_info . preg_matches [ n ] . match_index ) { memcpy ( ( void * ) & out_str [ outbuff_write_position ] , replace_with , replace_size ) ; outbuff_write_position += replace_size ; inbuff_read_position += match_info . preg_matches [ n ] . match_len ; size_t copy_distance_between_replace_and_next_index = 0 ; if ( ( n + 1 ) < match_info . pcre_num_matched ) { copy_distance_between_replace_and_next_index = ( match_info . preg_matches [ n + 1 ] . match_index - match_info . preg_matches [ n ] . match_index ) - match_info . preg_matches [ n + 1 ] . match_len ; if ( copy_distance_between_replace_and_next_index ) { memcpy ( ( void * ) & out_str [ outbuff_write_position ] , & in_buff [ inbuff_read_position ] , copy_distance_between_replace_and_next_index ) ; inbuff_read_position += copy_distance_between_replace_and_next_index ; outbuff_write_position += copy_distance_between_replace_and_next_index ; } } continue ; } if ( match_info . preg_matches [ n ] . match_index ) { if ( ! outbuff_write_position ) { memcpy ( ( void * ) & out_str [ outbuff_write_position ] , & in_buff [ inbuff_read_position ] , match_info . preg_matches [ n ] . match_index ) ; inbuff_read_position += match_info . preg_matches [ n ] . match_index ; outbuff_write_position += match_info . preg_matches [ n ] . match_index ; } memcpy ( ( void * ) & out_str [ outbuff_write_position ] , replace_with , replace_size ) ; outbuff_write_position += replace_size ; inbuff_read_position += match_info . preg_matches [ n ] . match_len ; size_t copy_distance_between_replace_and_next_index = 0 ; if ( ( n + 1 ) < match_info . pcre_num_matched ) { copy_distance_between_replace_and_next_index = ( match_info . preg_matches [ n + 1 ] . match_index - match_info . preg_matches [ n ] . match_index ) - match_info . preg_matches [ n + 1 ] . match_len ; if ( copy_distance_between_replace_and_next_index ) { memcpy ( ( void * ) & out_str [ outbuff_write_position ] , & in_buff [ inbuff_read_position ] , copy_distance_between_replace_and_next_index ) ; inbuff_read_position += copy_distance_between_replace_and_next_index ; outbuff_write_position += copy_distance_between_replace_and_next_index ; } } continue ; } } if ( bpstrlen ( & in_buff [ inbuff_read_position ] ) ) { memcpy ( ( void * ) & out_str [ outbuff_write_position ] , & in_buff [ inbuff_read_position ] , ( buff_len - inbuff_read_position ) ) ; } BP_DestroyRegexpMatchInfo ( & match_info ) ; return out_str ; }
perl compatible regular expression find and replace routine.
perl compatible regular expression find and replace routine .
// create new match info structure // attempt to match a regular expression (must save matches) // printf("\n Getting past initial checks: %u", matched); // if no matches are to be performed, exit with indication // ensure the matches have been set // ensure we have a match count // this string will be allocated and retrofitted // check num matched // subtract the match length // add the replace size // printf("\n totals?: %u - %u", match_info.pcre_num_matched, max_replacements); // set buffer length for bpreallocation // allocate out buffer // now create the new buffer // outbuffer write position // loop for recalculating/allocating substituion buffer // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Check 1: We've Matched at Zero Index %%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // if we've matched at the 0 position in the out buff, we // simply copy directly. // copy out data // move write position up past the replacement size // this is used to calculate copy distance // if we have a next match, calculate distance // run distance calculation // copy replacement data // copy data if necessary // move up the read position // continue here since all data was copied // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Check 2: Copy From Found Indexes %%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // if we have a match index, we can copy from position // copy in buffer front where necessary // copy data if necessary // move up the read position // copy out data // move write position up past the replacement size // this is used to calculate copy distance // if we have a next match, calculate distance // run distance calculation // copy replacement data // copy data if necessary // move up the read position // continue here since all data was copied // copy in the tail of the buffer // copy data if necessary // printf("\n Outstr: %s", out_str); // destroy the match info // return the newly allocated string