From f8dc21f52bfb47de5bcfd3fb536f99a5bd8e5ca5 Mon Sep 17 00:00:00 2001 From: spl3g Date: Sun, 16 Mar 2025 19:41:18 +0300 Subject: Add cs_try_chop_delim --- lib/const_strings.c | 11 +++++++++++ lib/const_strings.h | 1 + 2 files changed, 12 insertions(+) diff --git a/lib/const_strings.c b/lib/const_strings.c index 09d2c5f..0c0da41 100644 --- a/lib/const_strings.c +++ b/lib/const_strings.c @@ -55,6 +55,17 @@ const_string cs_chop_delim(const_string *str, char delim) { return result; } +bool cs_try_chop_delim(const_string *str, char delim, const_string *dst) { + const_string str_copy = *str; + const_string tmp = cs_chop_delim(&str_copy, delim); + if (str_copy.len == 0) { + return false; + } + *str = str_copy; + *dst = tmp; + return true; +} + int cs_find_delim(const_string str, char delim) { int n; for (n = 0; n < str.len && str.data[n] != delim; n++) {} diff --git a/lib/const_strings.h b/lib/const_strings.h index 987c6f6..bac629e 100644 --- a/lib/const_strings.h +++ b/lib/const_strings.h @@ -15,6 +15,7 @@ const_string cs_from_parts(char* data, int len); const_string cs_from_cstr(char* cstr); const_string cs_slice(const_string src, int from, int to); const_string cs_chop_delim(const_string *src, char delim); +bool cs_try_chop_delim(const_string *str, char delim, const_string *dst); int cs_find_delim(const_string str, char delim); bool cs_eq(const_string str1, const_string str2); void cs_print(char *format, const_string str); -- cgit v1.2.3