aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/cstr/cstr.c35
-rw-r--r--lib/cstr/cstr.h1
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/cstr/cstr.c b/lib/cstr/cstr.c
index 9f4ad6f..e30a774 100644
--- a/lib/cstr/cstr.c
+++ b/lib/cstr/cstr.c
@@ -83,6 +83,41 @@ u64 cstr_split(char *cstr, char split)
return splits;
}
+/** DOC
+ * @type function
+ * @name cstr_split
+ *
+ * @param char *cstr
+ * @param char split
+ * @return u64
+ *
+ * @description
+ * Splits a string on every `split` char.
+ * Returns the number of splits.
+ * ``WARNING`` This function does replace all `split` char with `0`.
+ * The old string does not exist after calling this function.
+ */
+u64 cstr_split_with_cancel(char *cstr, char split, char cancel)
+{
+ u64 splits = 1;
+ u8 is_in_cancel = 0;
+
+ while (*cstr) {
+ if (*cstr == cancel) {
+ is_in_cancel = !is_in_cancel;
+ }
+
+ if (*cstr == split && !is_in_cancel) {
+ *cstr = 0;
+ ++splits;
+ }
+
+ ++cstr;
+ }
+
+ return splits;
+}
+
/** DOC
* @type function
diff --git a/lib/cstr/cstr.h b/lib/cstr/cstr.h
index ce27eb0..049c867 100644
--- a/lib/cstr/cstr.h
+++ b/lib/cstr/cstr.h
@@ -11,6 +11,7 @@ typedef struct {
u64 cstr_length(const char *);
i8 cstr_compare(const char *a, const char *b);
u64 cstr_split(char *cstr, char split);
+u64 cstr_split_with_cancel(char *cstr, char split, char cancel);
const char *next_split(const char *previous);
u64 strip_front(char *cstr, char strip);
u64 strip_back(char *cstr, char strip);