From b1ada414e265a429fcd538efdd26efb8c9c6b697 Mon Sep 17 00:00:00 2001 From: wrmr Date: Sun, 10 Nov 2024 16:10:49 -0500 Subject: use _Generic for strv() --- str.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'str.h') diff --git a/str.h b/str.h index a6324f0..6cdae7b 100644 --- a/str.h +++ b/str.h @@ -3,24 +3,33 @@ #include -/* 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 *); -- cgit 1.4.1-2-gfad0