aboutsummaryrefslogtreecommitdiff
path: root/src/stringutils.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-07-13 16:13:06 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-13 16:13:06 +0200
commit9566646cd54a34c0dfe2dbdd89ee3858372a6c28 (patch)
tree1aa3942cb421b2fc90426b8e0984120511128a9d /src/stringutils.c
parentae0520e57267dbd866fc8cd25f66f4e6af2ac118 (diff)
Move string utils to their own file
Diffstat (limited to 'src/stringutils.c')
-rw-r--r--src/stringutils.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/stringutils.c b/src/stringutils.c
new file mode 100644
index 0000000..172d40d
--- /dev/null
+++ b/src/stringutils.c
@@ -0,0 +1,47 @@
+#include "stringutils.h"
+#include <string.h>
+
+void string_replace(char *str, char old_char, char new_char) {
+ for(;;) {
+ char c = *str;
+ if(c == old_char)
+ *str = new_char;
+ else if(c == '\0')
+ break;
+ ++str;
+ }
+}
+
+char* lstrip(char *str) {
+ for(;;) {
+ char c = *str;
+ if(c != ' ' && c != '\t' && c != '\n')
+ break;
+ else if(c == '\0')
+ break;
+ ++str;
+ }
+ return str;
+}
+
+void rstrip(char *str) {
+ int len = strlen(str);
+ if(len == 0)
+ return;
+
+ char *p = str + len - 1;
+ while(p != str) {
+ char c = *p;
+ if(c != ' ' && c != '\t' && c != '\n')
+ break;
+ --p;
+ }
+
+ p[1] = '\0';
+}
+
+char* strip(char *str) {
+ str = lstrip(str);
+ rstrip(str);
+ return str;
+} \ No newline at end of file