aboutsummaryrefslogtreecommitdiff
path: root/lib/cstr/cstr.c
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-02-15 16:35:40 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-02-15 16:35:40 +0100
commit5ecb8aafa496e9dfff51378be50eb29858b5465a (patch)
tree312a64f45148d03464c08f309af4a9a46a3f4ec8 /lib/cstr/cstr.c
parent0d9c643920a9431870983c896fb0b45d87d88afe (diff)
add quote parsing to smash
Diffstat (limited to 'lib/cstr/cstr.c')
-rw-r--r--lib/cstr/cstr.c35
1 files changed, 35 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