Add: Discovery phase added
Add: StringPool, StringView
This commit is contained in:
46
src/string.c
Normal file
46
src/string.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include "utils.h"
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void string_pool_reset(StringPool* ps)
|
||||
{
|
||||
// arena_reset(&ps->a);
|
||||
}
|
||||
|
||||
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_createcat_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};
|
||||
}
|
||||
Reference in New Issue
Block a user