aboutsummaryrefslogtreecommitdiff
path: root/lib/sv.h
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2025-05-14 21:12:58 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2025-05-15 22:25:45 +0100
commit12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa (patch)
tree0434141f2bfd24207a2864f613a1c2e3ee7181fc /lib/sv.h
parentba5c0a4579ece5d53c009a14d00e683e70b982f4 (diff)
downloadoats-12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa.tar.gz
oats-12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa.tar.bz2
oats-12de1e8db90bccd5a0eefd21075f07c7b7e3dfaa.zip
Refactor for cleanliness
Move files into separate folders for ease of reading, include source directory so we can use angle bracket includes, adjust build system to make directories for objects
Diffstat (limited to 'lib/sv.h')
-rw-r--r--lib/sv.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/sv.h b/lib/sv.h
new file mode 100644
index 0000000..3293b1c
--- /dev/null
+++ b/lib/sv.h
@@ -0,0 +1,70 @@
+/* Copyright (C) 2025 Aryadev Chavali
+
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
+ * details.
+
+ * You may distribute and modify this code under the terms of the GNU General
+ * Public License Version 2, which you should have received a copy of along with
+ * this program. If not, please go to <https://www.gnu.org/licenses/>.
+
+ * Created: 2025-04-14
+ * Description: String Views
+ */
+
+#ifndef SV_H
+#define SV_H
+
+#include <lib/arena.h>
+
+typedef struct SV
+{
+ u64 size;
+ char *data;
+} sv_t;
+
+#define SV(DATA, SIZE) ((const sv_t){.size = (SIZE), .data = (DATA)})
+#define SV_FMT(SV) (int)(SV).size, (SV).data
+#define PR_SV "%.*s"
+
+sv_t sv_make(arena_t *allocator, const char *data, u64 size);
+sv_t sv_copy(arena_t *allocator, sv_t sv);
+sv_t sv_append(arena_t *allocator, sv_t sv, const char *data, u64 size);
+sv_t sv_prepend(arena_t *allocator, sv_t sv, const char *data, u64 size);
+
+/**
+ * @brief Concatenate two string views, returning that concatenation.
+
+ * Allocates memory.
+ */
+sv_t sv_concat(arena_t *allocator, sv_t a, sv_t b);
+
+/**
+ * @brief Allocates a string view with the given `printf` format.
+ */
+sv_t sv_fmt(arena_t *allocator, char *fmt, ...);
+
+/**
+ * @brief Constructs a new string view at a different offset. Does not allocate
+ * new memory.
+ */
+sv_t sv_substr(sv_t sv, u64 index, u64 size);
+/**
+ * @brief Return a string view INDEX characters ahead (i.e. cut the string from
+ * the left).
+ */
+sv_t sv_cut(sv_t sv, u64 index);
+/**
+ * @brief Return a string view with SIZE (i.e. chopping the string from the
+ * right)
+ */
+sv_t sv_chop(sv_t sv, u64 size);
+
+i64 sv_find_substr(const sv_t sv, const sv_t substr);
+i64 sv_find_subcstr(const sv_t sv, const char *substr, u64 size);
+i64 sv_find_any(const sv_t sv, const char *bag);
+u64 sv_while(const sv_t sv, bool (*pred)(char));
+u64 sv_till(const sv_t sv, bool (*pred)(char));
+
+#endif