library/src/utility/BP-StringUtilities.cc
// This will attempt to find and remove patterns in a string.  Binary safe so long as
// provided lengths are given.  This is primarily to allow FindReplace the ability to
// remove strings and replace them with null strings.
This will attempt to find and remove patterns in a string.  Binary safe so long as
provided lengths are given.  This is primarily to allow FindReplace the ability to
remove strings and replace them with null strings.
This will attempt to find and remove patterns in a string . Binary safe so long as provided lengths are given . This is primarily to allow FindReplace the ability to remove strings and replace them with null strings .
char * BP_FindRemoveBin
(
		char * in_buff,
		size_t in_buff_size,
		char * remove_this,
		size_t remove_this_size,
		size_t max_removals
)
{
	if(!in_buff)
		return NULL;
	if(!in_buff_size)
		return NULL;
	 if(!remove_this)
		 return NULL;
	 if(!remove_this_size)
		 return NULL;
	if(in_buff_size < remove_this_size)
		return NULL;
	size_t has_matches = 0;
	size_t read_idx  = 0;
	size_t write_idx = 0;
	size_t prev_read_index  = 0;
	size_t prev_write_index = 0;
	size_t in_len           = bpstrlen(in_buff);
	size_t replace_this_len = remove_this_size;
	unsigned char test_char = 0x00;
	char *write_buff = NULL;
	do
	{
		if((unsigned char) in_buff[read_idx] == (unsigned char) remove_this[0])
		{
			if(memcmp(&in_buff[read_idx], remove_this, remove_this_size) == 0)
			{
				write_buff = (char *) bprealloc(write_buff, write_idx+1);
				memcpy(&write_buff[prev_write_index], &in_buff[prev_read_index], read_idx-prev_read_index);
				read_idx += remove_this_size;
				prev_read_index  = read_idx;
				prev_write_index = write_idx;
			}
			else
			{
				read_idx++;
				write_idx++;
			}
		}
		else
		{
			read_idx++;
			write_idx++;
		}
	} while(read_idx < in_len);
	if(prev_read_index)
	if(bpstrlen(&in_buff[prev_read_index]))
	{
		size_t final_realloc = bpstrlen(&in_buff[prev_read_index]);
		write_buff = (char *) bprealloc(write_buff, prev_write_index+final_realloc+10);
		memset(&write_buff[prev_write_index], 0x00, final_realloc+1);
		strncat(&write_buff[prev_write_index-1], &in_buff[prev_read_index], final_realloc);
	}
	return write_buff;
}
char * BP_FindRemoveBin ( char * in_buff , size_t in_buff_size , char * remove_this , size_t remove_this_size , size_t max_removals ) { if ( ! in_buff ) return NULL ; if ( ! in_buff_size ) return NULL ; if ( ! remove_this ) return NULL ; if ( ! remove_this_size ) return NULL ; if ( in_buff_size < remove_this_size ) return NULL ; size_t has_matches = 0 ; size_t read_idx = 0 ; size_t write_idx = 0 ; size_t prev_read_index = 0 ; size_t prev_write_index = 0 ; size_t in_len = bpstrlen ( in_buff ) ; size_t replace_this_len = remove_this_size ; unsigned char test_char = 0x00 ; char * write_buff = NULL ; do { if ( ( unsigned char ) in_buff [ read_idx ] == ( unsigned char ) remove_this [ 0 ] ) { if ( memcmp ( & in_buff [ read_idx ] , remove_this , remove_this_size ) == 0 ) { write_buff = ( char * ) bprealloc ( write_buff , write_idx + 1 ) ; memcpy ( & write_buff [ prev_write_index ] , & in_buff [ prev_read_index ] , read_idx - prev_read_index ) ; read_idx += remove_this_size ; prev_read_index = read_idx ; prev_write_index = write_idx ; } else { read_idx ++ ; write_idx ++ ; } } else { read_idx ++ ; write_idx ++ ; } } while ( read_idx < in_len ) ; if ( prev_read_index ) if ( bpstrlen ( & in_buff [ prev_read_index ] ) ) { size_t final_realloc = bpstrlen ( & in_buff [ prev_read_index ] ) ; write_buff = ( char * ) bprealloc ( write_buff , prev_write_index + final_realloc + 10 ) ; memset ( & write_buff [ prev_write_index ] , 0x00 , final_realloc + 1 ) ; strncat ( & write_buff [ prev_write_index - 1 ] , & in_buff [ prev_read_index ] , final_realloc ) ; } return write_buff ; }
This will attempt to find and remove patterns in a string.
This will attempt to find and remove patterns in a string .
// ensure we have valid pointers // ensurein_buff_size // if the string to be replaced in is shorter than match check // exit. // first calculate the number of matches in the string // set the read index and write index // previous read index (used for calculating lengths) // calculate length // character used for byte by byte checking // buffer which will be reallocated and rewritten // walk index items and calculate lengths // simple binary check to see if we have encountered the first char of our string // if we found the first char, compare memory. // display the current write/read indexes // printf("\n Found Match At (%c - %c) : %s",in_buff[read_idx], remove_this[0],  &in_buff[read_idx]); // printf("\n write: %u, read: %u", write_idx, read_idx); // move the write index down //write_idx -= remove_this_size; // the write_idx variable currently points to the end of the read // copy in data here // printf("\n Writebuff?: %s - %s - %s", write_buff, (char *) &in_buff[prev_read_index], (char *) &in_buff[prev_read_index]); // move the read index up // set the previous read and write indexes // increment the read position // increment the read and write position // calculate size for the remainder // perform final reallocation // initialize the memory // copy in the remainder of the string // return the generated buffer