refactor: send the command with retry and error detection
[lunaix-os.git] / lunaix-os / libs / klibc / string / trim.c
diff --git a/lunaix-os/libs/klibc/string/trim.c b/lunaix-os/libs/klibc/string/trim.c
new file mode 100644 (file)
index 0000000..82f29bd
--- /dev/null
@@ -0,0 +1,29 @@
+#include <klibc/string.h>
+
+#define WS_CHAR(c)                                                             \
+    (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r')
+
+void
+strrtrim(char* str)
+{
+    size_t l = strlen(str);
+    while (l < (size_t)-1) {
+        char c = str[l];
+        if (!c || WS_CHAR(c)) {
+            l--;
+            continue;
+        }
+        break;
+    }
+    str[l + 1] = '\0';
+}
+
+void*
+strltrim_safe(char* str)
+{
+    size_t l = 0;
+    char c = 0;
+    while ((c = str[l++]) && WS_CHAR(c))
+        ;
+    return strcpy(str, str + l);
+}
\ No newline at end of file