diff options
author | wrmr | 2024-11-10 16:10:49 -0500 |
---|---|---|
committer | wrmr | 2024-11-10 16:10:49 -0500 |
commit | b1ada414e265a429fcd538efdd26efb8c9c6b697 (patch) | |
tree | 542452ad7a13f57eae1831571d5f4bd8bd82e06e /str.h | |
parent | 9a513348057d8c62a65e08214206dae77b766b83 (diff) |
Diffstat (limited to 'str.h')
-rw-r--r-- | str.h | 23 |
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 *); |