Add string-helpers.c

This commit is contained in:
Johan Malm 2020-10-09 19:46:59 +01:00
parent 51507df2e7
commit 7e55e2cd09
6 changed files with 47 additions and 54 deletions

View file

@ -5,4 +5,5 @@ labwc_sources += files(
'grab-file.c',
'log.c',
'spawn.c',
'string-helpers.c',
)

View file

@ -0,0 +1,27 @@
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static void
rtrim(char **s)
{
size_t len = strlen(*s);
if (!len) {
return;
}
char *end = *s + len - 1;
while (end >= *s && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
}
char *
string_strip(char *s)
{
rtrim(&s);
while (isspace(*s)) {
s++;
}
return s;
}