#ifndef _GAPBUFFER_H
#define _GAPBUFFER_H

#include <arena.h>
#include <stddef.h>

struct gapbuffer {
  char* buffer;
  size_t size;
  size_t gap_start;
  size_t gap_end;
};

typedef void* (*gb_malloc_func_t) (void*, size_t);

typedef void* (*gb_realloc_func_t) (void*, void*, size_t, size_t);

typedef void (*gb_free_func_t) (void*, void*);

void gapbuffer_init (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t capacity);

void gapbuffer_fini (gb_free_func_t freefn, void* ctx, struct gapbuffer* gb);

void gapbuffer_move (struct gapbuffer* gb, size_t pos);

void gapbuffer_insert (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb, char c);

void gapbuffer_backspace (struct gapbuffer* gb);

void gapbuffer_grow (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* gb);

size_t gapbuffer_length (struct gapbuffer* gb);

char* gapbuffer_string_at (gb_malloc_func_t mallocfn, void* ctx, struct gapbuffer* gb, size_t pos);

void gapbuffer_concat (gb_realloc_func_t reallocfn, void* ctx, struct gapbuffer* dest,
                       struct gapbuffer* src);

#endif // _GAPBUFFER_H
