summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/my_strtok_r
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf/my_strtok_r
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf/my_strtok_r')
-rw-r--r--rushs/tinyprintf/my_strtok_r/my_strtok_r.c51
-rw-r--r--rushs/tinyprintf/my_strtok_r/my_strtok_r.h6
2 files changed, 57 insertions, 0 deletions
diff --git a/rushs/tinyprintf/my_strtok_r/my_strtok_r.c b/rushs/tinyprintf/my_strtok_r/my_strtok_r.c
new file mode 100644
index 0000000..ec052b7
--- /dev/null
+++ b/rushs/tinyprintf/my_strtok_r/my_strtok_r.c
@@ -0,0 +1,51 @@
+#include "my_strtok_r.h"
+
+#include <stddef.h>
+
+static int is_delim(char c, const char *delims)
+{
+ for (const char *d = delims; *d; d++)
+ {
+ if (*d == c)
+ return 1;
+ }
+ return 0;
+}
+
+char *my_strtok_r(char *str, const char *delim, char **saveptr)
+{
+ if (str == NULL)
+ {
+ if (*saveptr == NULL)
+ {
+ return NULL;
+ }
+ str = *saveptr;
+ }
+
+ size_t i = 0;
+ while (str[i] != '\0' && is_delim(str[i], delim))
+ {
+ i++;
+ }
+ if (str[i] == '\0')
+ {
+ *saveptr = NULL;
+ return NULL;
+ }
+
+ char *res = str + i;
+
+ while (str[i] != '\0' && !is_delim(str[i], delim))
+ {
+ i++;
+ }
+ if (str[i] == '\0')
+ {
+ *saveptr = NULL;
+ return res;
+ }
+ *saveptr = str + i + 1;
+ str[i] = '\0';
+ return res;
+}
diff --git a/rushs/tinyprintf/my_strtok_r/my_strtok_r.h b/rushs/tinyprintf/my_strtok_r/my_strtok_r.h
new file mode 100644
index 0000000..5603729
--- /dev/null
+++ b/rushs/tinyprintf/my_strtok_r/my_strtok_r.h
@@ -0,0 +1,6 @@
+#ifndef MY_STRTOK_R_H
+#define MY_STRTOK_R_H
+
+char *my_strtok_r(char *str, const char *delim, char **saveptr);
+
+#endif /* ! MY_STRTOK_R_H */