30 lines
806 B
C
30 lines
806 B
C
#include "strbuf.h"
|
|
#include "arena_alloc.h"
|
|
#include <arena.h>
|
|
#include <stddef.h>
|
|
|
|
void strbuf_append(struct strbuf* strbuf, char c) {
|
|
if (strbuf->items == NULL) {
|
|
strbuf->capacity = 256;
|
|
strbuf->count = 0;
|
|
strbuf->items = arena_malloc(&arena, strbuf->capacity);
|
|
} else {
|
|
if (strbuf->count == strbuf->capacity) {
|
|
size_t prev_capacity = strbuf->capacity;
|
|
strbuf->capacity *= 2;
|
|
strbuf->items = arena_realloc(&arena, strbuf->items, prev_capacity, strbuf->capacity);
|
|
}
|
|
}
|
|
strbuf->items[strbuf->count++] = c;
|
|
}
|
|
|
|
void strbuf_append_str(struct strbuf* strbuf, const char* s) {
|
|
while (*s)
|
|
strbuf_append(strbuf, *s++);
|
|
}
|
|
|
|
void strbuf_append_n(struct strbuf* strbuf, const char* s, size_t n) {
|
|
for (size_t i = 0; i < n; i++)
|
|
strbuf_append(strbuf, s[i]);
|
|
}
|