library/src/utility/BP-StringUtilities.cc
// Counts the number of instances a given token is found within a buffer.  This function
// is binary safe.  The primary purpose of this routine is for determining the size of
// storage arrays to generate when exploding or splitting strings/buffers.
Counts the number of instances a given token is found within a buffer.  This function
is binary safe.  The primary purpose of this routine is for determining the size of
storage arrays to generate when exploding or splitting strings/buffers.
Counts the number of instances a given token is found within a buffer . This function is binary safe . The primary purpose of this routine is for determining the size of storage arrays to generate when exploding or splitting strings / buffers .
size_t BP_CountTokensInBuffer(char *buffer, size_t buffer_len, char *token, size_t token_len)
{
	if(!buffer || !buffer_len || !token || !token_len)
		return 0;
	size_t num_instances = 0;
	size_t n = 0;
	for(; n < buffer_len; n++)
	{
		if((buffer_len - n) < token_len)
			break;
		if(memcmp(&buffer[n], token, token_len) == 0)
		{
			num_instances++;
			n += ( token_len - 1 );
		}
	}
	return num_instances;
}
size_t BP_CountTokensInBuffer ( char * buffer , size_t buffer_len , char * token , size_t token_len ) { if ( ! buffer || ! buffer_len || ! token || ! token_len ) return 0 ; size_t num_instances = 0 ; size_t n = 0 ; for ( ; n < buffer_len ; n ++ ) { if ( ( buffer_len - n ) < token_len ) break ; if ( memcmp ( & buffer [ n ] , token , token_len ) == 0 ) { num_instances ++ ; n += ( token_len - 1 ) ; } } return num_instances ; }
Counts the number of instances a given token is found within a buffer.
Counts the number of instances a given token is found within a buffer .
// Ensure that the passed in parameters are set.  They must all be set or the routine // is being used improperly. // number of instances // iterate through the characters in a string and run compares to determine the token count // If the buffer length remaining is less than the token length // then there can be no more comparison matching. // since we have a proper sized buffer to match from, we can // run the compare here. // increment the number of instances discovered // move past the token (minus one because the loop increments // the value) // return the number of counted instances