#include #include #include "str.h" void str_buf_clear(Str_Buf *buf) { buf->length = 0; } void str_buf_delete(Str_Buf *buf) { if(buf->contents) { free(buf->contents); buf->contents = 0; } buf->length = buf->capacity = 0; } void str_buf_push_char(Str_Buf *buf, char ch) { if(buf->length == buf->capacity) { uintptr new_capacity = buf->capacity ? buf->capacity * 2 : 8; char *new_contents = malloc(new_capacity); memcpy(new_contents, buf->contents, buf->length); if(buf->contents) free(buf->contents); buf->contents = new_contents; buf->capacity = new_capacity; } buf->contents[buf->length++] = ch; } void str_buf_append_cstr(Str_Buf *buf, const char *str) { while(*str) str_buf_push_char(buf, *(str++)); } char *str_buf_to_cstr(Str_Buf *buf) { str_buf_push_char(buf, 0); buf->length--; return buf->contents; }