rewrite the device subsystem interfaces (#48)
[lunaix-os.git] / lunaix-os / libs / klibc / string / trim.c
1 #include <klibc/string.h>
2 #include <lunaix/compiler.h>
3
4 #define WS_CHAR(c)                                                             \
5     (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r')
6
7 void _weak
8 strrtrim(char* str)
9 {
10     unsigned long l = strlen(str);
11     while (l < (unsigned long)-1) {
12         char c = str[l];
13         if (!c || WS_CHAR(c)) {
14             l--;
15             continue;
16         }
17         break;
18     }
19     str[l + 1] = '\0';
20 }
21
22 char* _weak
23 strltrim_safe(char* str)
24 {
25     unsigned long l = 0;
26     char c = 0;
27     while ((c = str[l]) && WS_CHAR(c)) {
28         l++;
29     }
30
31     if (l)
32         strcpy(str, str + l);
33     return str;
34 }