library/src/utility/BP-StringUtilities.cc
// Standard find and replace.  Returns a newly allocated string which
// must be bpfreed.
Standard find and replace.  Returns a newly allocated string which
must be bpfreed.
Standard find and replace . Returns a newly allocated string which must be bpfreed .
char * BP_FindReplace(char *in_buff, char *replace_this, char *replace_with, size_t replace_size, size_t max_replacements)
{
	if(!in_buff || !replace_this || !replace_with)
		return NULL;
	if(!bpstrlen(in_buff))
		return NULL;
	 if(!bpstrlen(replace_this))
		 return NULL;
	 if(!bpstrlen(replace_with))
		 return NULL;
	if(bpstrlen(in_buff) < bpstrlen(replace_this))
		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 = bpstrlen(replace_this);
	unsigned char test_char = 0x00;
	char *write_buff = NULL;
	do
	{
		if((unsigned char) in_buff[read_idx] == (unsigned char) replace_this[0])
		{
			if(memcmp(&in_buff[read_idx], replace_this, replace_this_len) == 0)
			{
				write_idx+=replace_size;
				write_buff = (char *) bprealloc(write_buff, write_idx+2);
				memset(&write_buff[write_idx-(replace_size)], 0x00, replace_size+1);
				memcpy(&write_buff[prev_write_index], &in_buff[prev_read_index], read_idx-prev_read_index);
				 since the buffer was reallocated, we can copy in data
				 from the base read index (aka prev_read_index) to the
				 write buffers base (aka.  start of new alloc'ed mem)
				memcpy
				(
						&write_buff[write_idx-(replace_size)],
						replace_with,
						replace_size
				);
				 move the read index up
				read_idx+=replace_this_len;
				 set the previous read and write indexes
				prev_read_index  = read_idx;
				prev_write_index = write_idx;
			}
			else
			{
				 read and write indexes
				read_idx++;
				write_idx++;
			}
		}
		else
		{
			 increment read and write indexes
			read_idx++;
			write_idx++;
		}
	} while(read_idx < in_len);
	if(prev_read_index)
	if(bpstrlen(&in_buff[prev_read_index]))
	{
		 calculate size for the remainder
		size_t final_realloc = bpstrlen(&in_buff[prev_read_index]);
		 perform final reallocation
		write_buff = (char *) bprealloc(write_buff, prev_write_index+final_realloc+10);
		 initialize the memory
		memset(&write_buff[prev_write_index], 0x00, final_realloc+1);
		 copy in the remainder of the string
		strncat(&write_buff[prev_write_index-1], &in_buff[prev_read_index], final_realloc);
	}
	 return the generated buffer
	return write_buff;
}
char * BP_FindReplace ( char * in_buff , char * replace_this , char * replace_with , size_t replace_size , size_t max_replacements ) { if ( ! in_buff || ! replace_this || ! replace_with ) return NULL ; if ( ! bpstrlen ( in_buff ) ) return NULL ; if ( ! bpstrlen ( replace_this ) ) return NULL ; if ( ! bpstrlen ( replace_with ) ) return NULL ; if ( bpstrlen ( in_buff ) < bpstrlen ( replace_this ) ) 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 = bpstrlen ( replace_this ) ; unsigned char test_char = 0x00 ; char * write_buff = NULL ; do { if ( ( unsigned char ) in_buff [ read_idx ] == ( unsigned char ) replace_this [ 0 ] ) { if ( memcmp ( & in_buff [ read_idx ] , replace_this , replace_this_len ) == 0 ) { write_idx += replace_size ; write_buff = ( char * ) bprealloc ( write_buff , write_idx + 2 ) ; memset ( & write_buff [ write_idx - ( replace_size ) ] , 0x00 , replace_size + 1 ) ; memcpy ( & write_buff [ prev_write_index ] , & in_buff [ prev_read_index ] , read_idx - prev_read_index ) ; memcpy ( & write_buff [ write_idx - ( replace_size ) ] , replace_with , replace_size ) ; read_idx += replace_this_len ; 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 ; }
Standard find and replace.
Standard find and replace .
// ensure we have valid pointers // ensure that pointers point to strings // 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], replace_this[0],  &in_buff[read_idx]); // printf("\n write: %u, read: %u", write_idx, read_idx); // move the write index up // printf("\n write after: %u", write_idx); // the write_idx variable currently points to the end of the read // zero out new memory // copy in data here // // since the buffer was reallocated, we can copy in data // from the base read index (aka prev_read_index) to the // write buffers base (aka.  start of new alloc'ed mem) // // move the read index up // set the previous read and write indexes // read and write indexes // increment read and write indexes // calculate size for the remainder // perform final reallocation // initialize the memory // copy in the remainder of the string // return the generated buffer