Skip to content

Commit 45bfd0a

Browse files
committed
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.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 3285848 commit 45bfd0a

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎tools/include/linux/string.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ static inline bool strstarts(const char *str, const char *prefix)
3131

3232
extern char * __must_check skip_spaces(const char *);
3333

34+
extern char *strim(char *);
35+
3436
#endif /* _TOOLS_LINUX_STRING_H_ */

‎tools/lib/string.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,28 @@ char *skip_spaces(const char *str)
120120
++str;
121121
return (char *)str;
122122
}
123+
124+
/**
125+
* strim - Removes leading and trailing whitespace from @s.
126+
* @s: The string to be stripped.
127+
*
128+
* Note that the first trailing whitespace is replaced with a %NUL-terminator
129+
* in the given string @s. Returns a pointer to the first non-whitespace
130+
* character in @s.
131+
*/
132+
char *strim(char *s)
133+
{
134+
size_t size;
135+
char *end;
136+
137+
size = strlen(s);
138+
if (!size)
139+
return s;
140+
141+
end = s + size - 1;
142+
while (end >= s && isspace(*end))
143+
end--;
144+
*(end + 1) = '\0';
145+
146+
return skip_spaces(s);
147+
}

0 commit comments

Comments
 (0)