From 5ecb8aafa496e9dfff51378be50eb29858b5465a Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 15 Feb 2023 16:35:40 +0100 Subject: add quote parsing to smash --- lib/cstr/cstr.c | 35 +++++++++++++++++++++++++++++++++++ lib/cstr/cstr.h | 1 + 2 files changed, 36 insertions(+) (limited to 'lib') 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); -- cgit v1.2.3-70-g09d2