2001-03-17 04:17:14 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) many different people.
|
2003-07-15 02:51:08 +05:30
|
|
|
* If you wrote this, please acknowledge your work.
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2017-08-05 21:20:35 +05:30
|
|
|
char* FAST_FUNC trim(char *s)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
2005-09-01 15:53:57 +05:30
|
|
|
size_t len = strlen(s);
|
2017-08-05 21:20:35 +05:30
|
|
|
size_t old = len;
|
2001-04-23 22:34:41 +05:30
|
|
|
|
2001-03-17 04:17:14 +05:30
|
|
|
/* trim trailing whitespace */
|
2007-10-30 00:52:13 +05:30
|
|
|
while (len && isspace(s[len-1]))
|
|
|
|
--len;
|
2001-03-17 04:17:14 +05:30
|
|
|
|
|
|
|
/* trim leading whitespace */
|
2007-04-12 06:02:05 +05:30
|
|
|
if (len) {
|
2009-10-23 01:58:08 +05:30
|
|
|
char *nws = skip_whitespace(s);
|
|
|
|
if ((nws - s) != 0) {
|
|
|
|
len -= (nws - s);
|
|
|
|
memmove(s, nws, len);
|
2007-10-30 00:52:13 +05:30
|
|
|
}
|
2005-09-01 15:53:57 +05:30
|
|
|
}
|
2017-08-05 21:20:35 +05:30
|
|
|
|
|
|
|
s += len;
|
|
|
|
/* If it was a "const char*" which does not need trimming,
|
|
|
|
* avoid superfluous store */
|
|
|
|
if (old != len)
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
return s;
|
2001-03-17 04:17:14 +05:30
|
|
|
}
|