library/src/utility/BP-StringUtilities.cc
// Returns the index within the provided buffer of the first instance of
// character.  This is used for finding offsets into buffers.  Returns -1
// if an offset cannot be found (or error)
Returns the index within the provided buffer of the first instance of
character.  This is used for finding offsets into buffers.  Returns -1
if an offset cannot be found (or error)
Returns the index within the provided buffer of the first instance of character . This is used for finding offsets into buffers . Returns - 1 if an offset cannot be found ( or error )
int BP_LookupFirstCharacterOffsetInBuffer
(
	char * buffer,
	size_t buffer_len,
	char   lookup_char,
	BP_STRING_OFFSET_LOOKUP_DIRECTION lookup_direction
)
{
	if(!buffer)
		return -1;
	if(!buffer_len)
		return -1;
	if
	(
		lookup_direction != BP_STRING_OFFSET_LOOKUP_DIRECTION_FORWARD &&
		lookup_direction != BP_STRING_OFFSET_LOOKUP_DIRECTION_REVERSE
	)
	{
		return -1;
	}
	if(lookup_direction == BP_STRING_OFFSET_LOOKUP_DIRECTION_FORWARD)
	{
		size_t n = 0;
		for(; n < buffer_len; n++)
		{
			if(buffer[n] == lookup_char)
				return n;
		}
		return -1;
	}
	if(lookup_direction == BP_STRING_OFFSET_LOOKUP_DIRECTION_REVERSE)
	{
		size_t n = buffer_len-1;
		for(; n >= 0; n--)
		{
			if(buffer[n] == lookup_char)
				return n;
		}
		return -1;
	}
	return -1;
}
int BP_LookupFirstCharacterOffsetInBuffer ( char * buffer , size_t buffer_len , char lookup_char , BP_STRING_OFFSET_LOOKUP_DIRECTION lookup_direction ) { if ( ! buffer ) return -1 ; if ( ! buffer_len ) return -1 ; if ( lookup_direction != BP_STRING_OFFSET_LOOKUP_DIRECTION_FORWARD && lookup_direction != BP_STRING_OFFSET_LOOKUP_DIRECTION_REVERSE ) { return -1 ; } if ( lookup_direction == BP_STRING_OFFSET_LOOKUP_DIRECTION_FORWARD ) { size_t n = 0 ; for ( ; n < buffer_len ; n ++ ) { if ( buffer [ n ] == lookup_char ) return n ; } return -1 ; } if ( lookup_direction == BP_STRING_OFFSET_LOOKUP_DIRECTION_REVERSE ) { size_t n = buffer_len - 1 ; for ( ; n >= 0 ; n -- ) { if ( buffer [ n ] == lookup_char ) return n ; } return -1 ; } return -1 ; }
Returns the index within the provided buffer of the first instance of
character.
Returns the index within the provided buffer of the first instance of character .
// run basic checks (do not check lookup_char because any char is valid) // ensure a direction marker is set // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Forward Lookup %%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // return error if we couldn't find a lookup // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Reverse Lookup %%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set index position // iterate and run check // return error if we couldn't find a lookup // return -1 if no checks matched (should never get here)