summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--parse.c4
-rw-r--r--str.c2
-rw-r--r--str.h23
3 files changed, 19 insertions, 10 deletions
diff --git a/parse.c b/parse.c
index ab59fb2..07e8da0 100644
--- a/parse.c
+++ b/parse.c
@@ -22,7 +22,7 @@ void parse_plain_url(struct doc *d, strv_t ln, size_t i) {
 
 int parse_plain(struct doc *d, const str_t *b) {
 	doc_init(d);
-	strv_t ln, buf = (strv_t) { b->buf, b->sz };
+	strv_t ln, buf = strv(b);
 	while (strv_split(&buf, '\n', &ln)) {
 		if (ln.n > 0 && ln.s[ln.n - 1] == '\r') ln.n--;
 		for (size_t j = 1; j + 2 < ln.n; j++) {
@@ -62,7 +62,7 @@ int parse_gophermap(struct doc *d, const str_t *b) {
 		switch (bits.item_type) {
 		default:
 			str_fmt(&url, "%s:%s/%c%s", bits.host, bits.port, bits.item_type, bits.sel);
-			doc_set_link(d, doc_add_link(d, (strv_t) { url.buf, url.sz }));
+			doc_set_link(d, doc_add_link(d, strv(url)));
 		case 'i':
 			doc_add_text(d, bits.dstr);
 			doc_new_line(d);
diff --git a/str.c b/str.c
index 8d1806e..8e60ed7 100644
--- a/str.c
+++ b/str.c
@@ -45,7 +45,7 @@ void str_free(str_t *b) {
 
 /* string views */
 
-strv_t strv(const char *s) {
+strv_t strv_cstr(const char *s) {
 	return (strv_t) { s, strlen(s) };
 }
 
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 *);