Commit d0cbec4a authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

lib/util: add spdk_str_trim()



Function to trim leading and trailing whitespace from a string.

Originally based on code imported from istgt.

Change-Id: I87abe584130bdf4930098fadb8e57291f18eda7f
Signed-off-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent e56aab98
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -67,6 +67,13 @@ char *spdk_strlwr(char *s);
 */
char *spdk_strsepq(char **stringp, const char *delim);

/**
 * Trim whitespace from a string in place.
 *
 * \param s String to trim.
 */
char *spdk_str_trim(char *s);

#ifdef __cplusplus
}
#endif
+34 −0
Original line number Diff line number Diff line
@@ -166,3 +166,37 @@ spdk_strsepq(char **stringp, const char *delim)

	return p;
}

char *
spdk_str_trim(char *s)
{
	char *p, *q;

	if (s == NULL) {
		return NULL;
	}

	/* remove header */
	p = s;
	while (*p != '\0' && isspace(*p)) {
		p++;
	}

	/* remove tailer */
	q = p + strlen(p);
	while (q - 1 >= p && isspace(*(q - 1))) {
		q--;
		*q = '\0';
	}

	/* if remove header, move */
	if (p != s) {
		q = s;
		while (*p != '\0') {
			*q++ = *p++;
		}
		*q = '\0';
	}

	return s;
}