code-experiments/src/coco_string.c
/**
 * @brief Trims the given string (removes any leading and trailing spaces).
 *
 * If the string contains any leading spaces, the contents are shifted so that if it was dynamically
 * allocated, it can be still freed on the returned pointer.
 */
@brief Trims the given string (removes any leading and trailing spaces).
If the string contains any leading spaces, the contents are shifted so that if it was dynamically
allocated, it can be still freed on the returned pointer.
@brief Trims the given string ( removes any leading and trailing spaces ) . If the string contains any leading spaces the contents are shifted so that if it was dynamically allocated it can be still freed on the returned pointer .
static char *coco_string_trim(char *string) {
	size_t i, len = 0;
	int all_whitespaces = 1;
	char *frontp = string;
	char *endp = NULL;
	if (string == NULL) {
		return NULL;
	}
	if (string[0] == '\0') {
		return string;
	}
	len = strlen(string);
	endp = string + len;
	for (i = 0; ((i < len) && all_whitespaces); i++)
		all_whitespaces = all_whitespaces && isspace(string[i]);
	if (all_whitespaces) {
	  string[0] = '\0';
		return string;
	}
	while (isspace((unsigned char) *frontp)) {
		++frontp;
	}
	if (endp != frontp) {
		while (isspace((unsigned char) *(--endp)) && endp != frontp) {
		}
	}
	if (string + len - 1 != endp)
		*(endp + 1) = '\0';
	else if (frontp != string && endp == frontp)
		*string = '\0';
	endp = string;
	if (frontp != string) {
		while (*frontp) {
			*endp++ = *frontp++;
		}
		*endp = '\0';
	}
	return string;
}
static char * coco_string_trim ( char * string ) { size_t i , len = 0 ; int all_whitespaces = 1 ; char * frontp = string ; char * endp = NULL ; if ( string == NULL ) { return NULL ; } if ( string [ 0 ] == ' \0 ' ) { return string ; } len = strlen ( string ) ; endp = string + len ; for ( i = 0 ; ( ( i < len ) && all_whitespaces ) ; i ++ ) all_whitespaces = all_whitespaces && isspace ( string [ i ] ) ; if ( all_whitespaces ) { string [ 0 ] = ' \0 ' ; return string ; } while ( isspace ( ( unsigned char ) * frontp ) ) { ++ frontp ; } if ( endp != frontp ) { while ( isspace ( ( unsigned char ) * ( -- endp ) ) && endp != frontp ) { } } if ( string + len - 1 != endp ) * ( endp + 1 ) = ' \0 ' ; else if ( frontp != string && endp == frontp ) * string = ' \0 ' ; endp = string ; if ( frontp != string ) { while ( * frontp ) { * endp ++ = * frontp ++ ; } * endp = ' \0 ' ; } return string ; }
@brief Trims the given string (removes any leading and trailing spaces).
@brief Trims the given string ( removes any leading and trailing spaces ) .
/* Move the front and back pointers to address the first non-whitespace characters from each end. */ /* Shift the string. Note the reuse of endp to mean the front of the string buffer now. */