summary refs log tree commit diff
path: root/str.h
blob: 6cdae7b4e5ea956fc8a2d691be4f82c2180ff955 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef BUF_H
#define BUF_H

#include <stddef.h>

typedef struct buf {
	size_t sz, cap;
	char *buf;
} str_t;

typedef struct {
	const char *s;
	size_t n;
} strv_t;

/* 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 */

void str_init(str_t *, size_t);
void str_grow(str_t *, size_t);
void str_free(str_t *);

void str_cat(str_t *b, strv_t s);
void str_catc(str_t *b, char c);

void str_fmt(str_t *b, const char *fmt, ...);

#endif