diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..52ccad7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "dstring"] + path = dstring + url = git@git.thewizardapprentice.com:abdelrahman/dstring diff --git a/dstring b/dstring new file mode 160000 index 0000000..c1ef7af --- /dev/null +++ b/dstring @@ -0,0 +1 @@ +Subproject commit c1ef7afcac21d7bc9536ca2dc309dd9dd57169d2 diff --git a/include/dstring/dstring.h b/include/dstring/dstring.h deleted file mode 100644 index ce8d7d6..0000000 --- a/include/dstring/dstring.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef DSTRING_H -#define DSTRING_H - -#include "aliases.h" - -typedef struct dstring dstr_t; - -dstr_t *dstr_with_capacity(u64 capacity); -dstr_t *dstr_from_string(const char *str); -void dstr_update(dstr_t **dst, const char *src); -void dstr_free(dstr_t **str); -void dstr_concat(dstr_t **dst, const char *src); -void dstr_append(dstr_t **dst, char c); -void dstr_resize(dstr_t **str); -void dstr_clear(dstr_t *str); -void dstr_print(const dstr_t *str); -i64 dstr_find(const dstr_t *str, const char *substr); -u64 dstr_length(const dstr_t *str); -u64 dstr_capacity(const dstr_t *str); -const char *dstr_to_cstr(const dstr_t *str); - -#endif // !DSTRING_H diff --git a/include/dstring/dstring.h b/include/dstring/dstring.h new file mode 120000 index 0000000..dfdf31f --- /dev/null +++ b/include/dstring/dstring.h @@ -0,0 +1 @@ +../../dstring/include/dstring.h \ No newline at end of file diff --git a/src/dstring/dstring.c b/src/dstring/dstring.c deleted file mode 100644 index f6dd3d7..0000000 --- a/src/dstring/dstring.c +++ /dev/null @@ -1,211 +0,0 @@ -#include "dstring.h" -#include "aliases.h" -#include -#include -#include - -// Use this scalar to allocate extra memory in order to avoid having to -// constantly reallocate -#define CAPACITY_SCALAR 8 - -struct dstring { - u64 capacity; - u64 size; - char buf[]; -}; - -dstr_t *dstr_with_capacity(u64 capacity) { - dstr_t *out = (dstr_t *)malloc(sizeof(dstr_t) + capacity + 1); - - if (!out) { - return NULL; - } - - out->capacity = capacity; - out->size = 0; - memset(out->buf, 0, capacity + 1); - - return out; -} - -dstr_t *dstr_from_string(const char *str) { - u64 length = strlen(str); - - u64 capacity = length * CAPACITY_SCALAR; - - dstr_t *out = dstr_with_capacity(capacity); - - if (!out) { - return NULL; - } - - out->size = length; - strncpy(out->buf, str, length); - - return out; -} - -void dstr_update(dstr_t **dst, const char *src) { - if (!(*dst)) { - return; - } - - u64 length = strlen(src); - - dstr_t *str = *dst; - - if (length <= str->capacity) { - memset(str->buf, 0, str->capacity); - - str->size = length; - - strncpy(str->buf, src, length); - } else { - u64 capacity = length * CAPACITY_SCALAR; - - dstr_t *tmp = (dstr_t *)realloc(*dst, sizeof(dstr_t) + capacity + 1); - - if (!tmp) { - return; - } - - tmp->capacity = capacity; - tmp->size = length; - strncpy(tmp->buf, src, length); - - *dst = tmp; - } -} - -void dstr_free(dstr_t **str) { - if (!(*str)) { - return; - } - - free(*str); - *str = NULL; -} - -void dstr_concat(dstr_t **dst, const char *src) { - if (!(*dst)) { - return; - } - - u64 src_length = strlen(src); - - if (src_length == 0) { - return; - } - - u64 new_length = (*dst)->size + src_length; - - char str[new_length + 1]; - memset(str, 0, new_length + 1); - - strncpy(str, (*dst)->buf, (*dst)->size); - strncat(str, src, src_length); - - dstr_update(dst, str); -} - -void dstr_append(dstr_t **dst, char c) { - if (!(*dst)) { - return; - } - - u64 new_length = (*dst)->size + 1; - - char str[new_length + 1]; - memset(str, 0, new_length + 1); - - strncpy(str, (*dst)->buf, (*dst)->size); - str[(*dst)->size] = c; - - dstr_update(dst, str); -} - -void dstr_resize(dstr_t **str) { - u64 capacity = (*str)->size; - - dstr_t *tmp = (dstr_t *)realloc(*str, sizeof(dstr_t) + capacity + 1); - - if (!tmp) { - return; - } - - tmp->capacity = capacity; - - *str = tmp; -} - -void dstr_clear(dstr_t *str) { - if (!str || str->size == 0) { - return; - } - - memset(str->buf, 0, str->capacity); - str->size = 0; -} - -void dstr_print(const dstr_t *str) { - if (!str) { - return; - } - - printf("%s\n", str->buf); -} - -i64 dstr_find(const dstr_t *str, const char *substr) { - if (!str || !substr) { - return -1; - } - - u64 substr_length = strlen(substr); - - if (substr_length == 0 || substr_length > str->size) { - return -1; - } - - char buf[substr_length + 1]; - memset(buf, 0, substr_length + 1); - - for (i64 i = 0; i < str->size; ++i) { - if (i + substr_length >= str->size) { - break; - } - - for (u64 j = 0; j < substr_length; ++j) { - buf[j] = str->buf[i + j]; - } - - if (strcmp(buf, substr) == 0) { - return i; - } - } - - return -1; -} - -u64 dstr_length(const dstr_t *str) { - if (!str) { - return 0; - } - - return str->size; -} - -u64 dstr_capacity(const dstr_t *str) { - if (!str) { - return 0; - } - - return str->capacity; -} - -const char *dstr_to_cstr(const dstr_t *str) { - if (!str) { - return ""; - } - - return str->buf; -} diff --git a/src/dstring/dstring.c b/src/dstring/dstring.c new file mode 120000 index 0000000..f932b3b --- /dev/null +++ b/src/dstring/dstring.c @@ -0,0 +1 @@ +../../dstring/src/dstring.c \ No newline at end of file