summaryrefslogtreecommitdiff
path: root/src/str.h
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2024-07-13 12:51:05 +0200
committerLizzy Fleckenstein <lizzy@vlhl.dev>2024-07-13 12:51:05 +0200
commit591232eba1809643df1f4a6075ff9581b8bb2f7a (patch)
tree92e2c464158d03ef9c83f153ba5b88c39581d9f8 /src/str.h
parent95283d70eee749d984b2ca07c0b61d39f9bb350d (diff)
move headers to src
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src/str.h')
-rw-r--r--src/str.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/str.h b/src/str.h
new file mode 100644
index 0000000..5b3e04c
--- /dev/null
+++ b/src/str.h
@@ -0,0 +1,61 @@
+// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+#ifndef STR_H
+#define STR_H
+
+#include <stddef.h>
+#include <stdbool.h>
+#include "array.h"
+
+// string library taken from cuddlesOS:
+// https://github.com/cuddlesOS/cuddles/blob/master/stage3/string.c
+
+typedef array(char) str;
+#define NILS ((str) { 0, NULL })
+#define S(X) ((str) { len(X)-1, X })
+#define PSTR(X) (int) (X).len, (X).data
+
+typedef arraybuf(char) strbuf;
+#define NILSBUF ((strbuf) { 0, 0, NULL })
+
+// return if two strings are equal
+bool str_eq(str s1, str s2);
+
+// compares two strings by length and ASCII values. return value:
+// < 0 if s1 < s2
+// = 0 if s1 = s2
+// > 0 if s1 > s2
+int str_cmp(str s1, str s2);
+
+// returns index of first of occurrence in s of any of the chars in tokens
+// returns length of s if not found
+size_t str_find(str s, str tokens);
+
+// this is a splitting function
+// returns the next non-empty substring of *s that is delimited by the tokens in sep
+// the returned string does not contain any of the separators
+// returns an emtpy string when end is reached
+// advances s to after the substring plus first delimiting token
+str str_walk(str *s, str sep);
+
+// advances the string while its first token matches any of the chars in tokens
+// this can be used to consume whitespace, for example
+str str_eat(str s, str tokens);
+
+// advances the string s by x chars, increasing the data pointer and decreasing the length
+// note: this is not bounds checked
+str str_advance(str s, size_t x);
+
+// returns true if s starts with start
+bool str_start(str s, str start);
+
+// construct a str from a \0 terminated string at runtime
+// avoid this for literals: use the S macro instead without a runtime cost
+str str_intro(char *c);
+
+// copy a string to the heap
+str str_clone(str s);
+
+#endif