diff --git a/include/spdk/string.h b/include/spdk/string.h index 67512ba34..ffcc8a96c 100644 --- a/include/spdk/string.h +++ b/include/spdk/string.h @@ -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 diff --git a/lib/util/string.c b/lib/util/string.c index 5355ffa2e..8aee731b3 100644 --- a/lib/util/string.c +++ b/lib/util/string.c @@ -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; +}