common: remove buf_init(), add BUF_INIT and buf_move()

Add a BUF_INIT macro, which makes it easier to initialize a struct buf
to an empty string (without a heap allocation).

Add buf_move() to move the contents of one struct buf to another (the
source is reset to BUF_INIT, analogous to C++ move-assignment).

Use buf_reset() instead of directly calling `free(s->buf)` since the
internal buf may not always be allocated by malloc() now.
This commit is contained in:
John Lindgren 2024-04-14 14:20:57 -04:00 committed by Johan Malm
parent 343918dee0
commit 0573f16693
12 changed files with 122 additions and 90 deletions

View file

@ -189,9 +189,7 @@ field_set_custom(struct buf *buf, struct view *view, const char *format)
char fmt[LAB_FIELD_SINGLE_FMT_MAX_LEN];
unsigned char fmt_position = 0;
struct buf field_result;
buf_init(&field_result);
struct buf field_result = BUF_INIT;
char converted_field[4096];
for (const char *p = format; *p; p++) {
@ -255,7 +253,7 @@ reset_format:
buf_clear(&field_result);
fmt_position = 0;
}
free(field_result.buf);
buf_reset(&field_result);
}
static const struct field_converter field_converter[LAB_FIELD_COUNT] = {