aboutsummaryrefslogtreecommitdiff
path: root/src/sb.h
blob: 070897b4ef7d52990868aaedf958191b1634064c (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
// sb.h --- string builder
#ifndef INCLUDED_SB_H
#define INCLUDED_SB_H

#include "utils.h"
#include "da.h"

typedef struct sb {
    char   *items;
    size_t  count;
    size_t  capacity;
} *SB;

#define SB_APPEND_BUF(SB, BUF, SIZE) DA_APPEND_MANY(SB, BUF, SIZE)

#define SB_APPEND_CSTR(SB, CSTR)        \
  ({                                    \
    const char *s = (CSTR);             \
    size_t      n = strlen (s);         \
    DA_APPEND_MANY(SB, s, n);           \
  })

#define SB_APPEND_CSTRS(SB, ...) sb_append_cstrs (&(SB), __VA_ARGS__, NULL)

#define SB_APPEND_NULL(SB) DA_APPEND(SB, '\0')

#define SB_RESET(SB) DA_RESET(SB)

#define SB_FREE(SB) DA_FREE(SB)

extern void sb_append_cstrs (SB sb,                     ...);
extern void sb_printf       (SB sb, const char *format, ...);

#endif