#include "string.h" #include #include "utils.h" #include StringPool string_pool_create(Arena* a) { assert(a != NULL); assert(a->mem != NULL); StringPool sp; sp.a = a; LOG_DEBUG("String pool initialized on arena with block @ %p.", sp.a->mem); return sp; } StringView string_create(StringPool* sp, const char* s, size_t len) { assert(sp != NULL); if(s == NULL) DIE("Invalid string supplied!"); char* buf = (char*)arena_alloc(sp->a, len + 1); memcpy(buf, s, len); buf[len] = '\0'; LOG_DEBUG("A string was created @ %p with data: \"%s\"", buf, buf); return (StringView){.buf = buf, .len=len}; } StringView string_concat_ss(StringPool* sp, StringView* a, StringView* b) { if(a == NULL) DIE("a must be a valid C-style string!"); if(b == NULL) DIE("b must be a valid C-style string!"); if(a->len <= 0 || b->len <= 0) DIE("size must be valid! a: %d, b: %d", a->len, b->len); size_t len = a->len + b->len; char* buf = (char*)arena_alloc(sp->a, len + 1); memcpy(buf, a->buf, a->len); char* bbuf_off = buf + a->len; memcpy(bbuf_off, b->buf, b->len); buf[len] = '\0'; LOG_DEBUG("A string was created @ %p with data: \"%s\"", buf, buf); return (StringView){.buf = buf, .len = len}; }