summary refs log tree commit diff
path: root/str.h
diff options
context:
space:
mode:
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 *);