Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||||
3 | * Utility routines. | ||||
4 | * | ||||
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) many different people. |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * If you wrote this, please acknowledge your work. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
11 | |||||
Denys Vlasenko | 20077c1 | 2017-08-05 17:50:35 +0200 | [diff] [blame] | 12 | char* FAST_FUNC trim(char *s) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | { |
Rob Landley | 828548a | 2005-09-01 10:23:57 +0000 | [diff] [blame] | 14 | size_t len = strlen(s); |
Denys Vlasenko | 20077c1 | 2017-08-05 17:50:35 +0200 | [diff] [blame] | 15 | size_t old = len; |
Mark Whitley | 4b66dab | 2001-04-23 17:04:41 +0000 | [diff] [blame] | 16 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | /* trim trailing whitespace */ |
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame] | 18 | while (len && isspace(s[len-1])) |
19 | --len; | ||||
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | |
21 | /* trim leading whitespace */ | ||||
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 22 | if (len) { |
Denys Vlasenko | c0dab37 | 2009-10-22 22:28:08 +0200 | [diff] [blame] | 23 | char *nws = skip_whitespace(s); |
24 | if ((nws - s) != 0) { | ||||
25 | len -= (nws - s); | ||||
26 | memmove(s, nws, len); | ||||
Denis Vlasenko | 5a28a25 | 2007-10-29 19:22:13 +0000 | [diff] [blame] | 27 | } |
Rob Landley | 828548a | 2005-09-01 10:23:57 +0000 | [diff] [blame] | 28 | } |
Denys Vlasenko | 20077c1 | 2017-08-05 17:50:35 +0200 | [diff] [blame] | 29 | |
30 | s += len; | ||||
31 | /* If it was a "const char*" which does not need trimming, | ||||
32 | * avoid superfluous store */ | ||||
33 | if (old != len) | ||||
34 | *s = '\0'; | ||||
35 | |||||
36 | return s; | ||||
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | } |