summary refs log tree commit diff
path: root/str.h
diff options
context:
space:
mode:
authorwrmr2024-11-10 16:10:49 -0500
committerwrmr2024-11-10 16:10:49 -0500
commitb1ada414e265a429fcd538efdd26efb8c9c6b697 (patch)
tree542452ad7a13f57eae1831571d5f4bd8bd82e06e /str.h
parent9a513348057d8c62a65e08214206dae77b766b83 (diff)
use _Generic for strv() HEAD master
Diffstat (limited to 'str.h')
-rw-r--r--str.h23
1 files changed, 16 insertions, 7 deletions
diff --git a/str.h b/str.h
index a6324f0..6cdae7b 100644
--- a/str.h
+++ b/str.h
@@ -3,24 +3,33 @@
 
 #include <stddef.h>
 
-/* string views */
+typedef struct buf {
+	size_t sz, cap;
+	char *buf;
+} str_t;
 
 typedef struct {
 	const char *s;
 	size_t n;
 } strv_t;
 
-strv_t strv(const char *s);
+/* string views */
+
+#define strv(s) _Generic((s),\
+		char *: strv_cstr, const char *: strv_cstr,\
+		str_t *: strv_pstr, const str_t *: strv_pstr,\
+		str_t: strv_str\
+	)(s)
+
+strv_t strv_cstr(const char *s);
+static inline strv_t strv_pstr(const str_t *s) { return (strv_t) { s->buf, s->sz }; }
+static inline strv_t strv_str(str_t s) { return (strv_t) { s.buf, s.sz }; }
+
 char strv_next(strv_t *s);
 int strv_split(strv_t *src, int chr, strv_t *dest);
 
 /* strings */
 
-typedef struct buf {
-	size_t sz, cap;
-	char *buf;
-} str_t;
-
 void str_init(str_t *, size_t);
 void str_grow(str_t *, size_t);
 void str_free(str_t *);