From 6f64d63ca8f16886872e270c805415b9e4f1c914 Mon Sep 17 00:00:00 2001 From: iWas-Coder <wasymatieh01@gmail.com> Date: Sat, 7 Dec 2024 21:39:56 +0100 Subject: [PATCH] Make `split_cstr` func work with multi-char delimiters --- src/carbon_strlist.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/carbon_strlist.c b/src/carbon_strlist.c index f5d168e..856a981 100644 --- a/src/carbon_strlist.c +++ b/src/carbon_strlist.c @@ -29,11 +29,15 @@ CBN_StrList carbon_strlist_create(u8 unique) { CBN_StrList carbon_strlist_from_splitted_cstr(const char *s, const char *delim) { CBN_StrList sl = carbon_strlist_create(false); char *s_copy = carbon_string_dup(s); - char *sub = strtok(s_copy, delim); - while (sub) { - carbon_strlist_push(&sl, sub); - sub = strtok(0, delim); + usz delim_len = strlen(delim); + char *start = s_copy; + char *found; + while ((found = strstr(start, delim))) { + *found = 0; + carbon_strlist_push(&sl, start); + start = found + delim_len; } + if (*start) carbon_strlist_push(&sl, start); CARBON_FREE(s_copy); return sl; }