From 45bfd0ac7bd2afa83600df9c1286a1642bb15c55 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Wed, 26 Jun 2019 11:50:16 -0300 Subject: tools lib: Adopt strim() from the kernel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we're working on moving stuff out of tools/perf/util/ to tools/lib/, take the opportunity to adopt routines from the kernel that are equivalent, so that tools/ code look more like the kernel. Cc: Adrian Hunter Cc: André Goddard Rosa Cc: Jiri Olsa Cc: Namhyung Kim Link: https://lkml.kernel.org/n/tip-zqy1zdu2ok17qvi0ytk8z13c@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/lib/string.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tools/lib/string.c') diff --git a/tools/lib/string.c b/tools/lib/string.c index 50d400822bb3..80472e6b3829 100644 --- a/tools/lib/string.c +++ b/tools/lib/string.c @@ -120,3 +120,28 @@ char *skip_spaces(const char *str) ++str; return (char *)str; } + +/** + * strim - Removes leading and trailing whitespace from @s. + * @s: The string to be stripped. + * + * Note that the first trailing whitespace is replaced with a %NUL-terminator + * in the given string @s. Returns a pointer to the first non-whitespace + * character in @s. + */ +char *strim(char *s) +{ + size_t size; + char *end; + + size = strlen(s); + if (!size) + return s; + + end = s + size - 1; + while (end >= s && isspace(*end)) + end--; + *(end + 1) = '\0'; + + return skip_spaces(s); +} -- cgit v1.2.3