tools lib: Adopt strim() from the kernel

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 <adrian.hunter@intel.com>
Cc: André Goddard Rosa <andre.goddard@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lkml.kernel.org/n/tip-zqy1zdu2ok17qvi0ytk8z13c@git.kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 32858480
...@@ -31,4 +31,6 @@ static inline bool strstarts(const char *str, const char *prefix) ...@@ -31,4 +31,6 @@ static inline bool strstarts(const char *str, const char *prefix)
extern char * __must_check skip_spaces(const char *); extern char * __must_check skip_spaces(const char *);
extern char *strim(char *);
#endif /* _TOOLS_LINUX_STRING_H_ */ #endif /* _TOOLS_LINUX_STRING_H_ */
...@@ -120,3 +120,28 @@ char *skip_spaces(const char *str) ...@@ -120,3 +120,28 @@ char *skip_spaces(const char *str)
++str; ++str;
return (char *)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);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment