#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++);
}
