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: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
parent
e56aab98ac
commit
d0cbec4a19
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user