From: Brendan Hansen Date: Mon, 4 May 2020 03:21:53 +0000 (-0500) Subject: Working on string library X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fccf4cabb48d1bcda26db17e3f6df1eafd140a95;p=onyx.git Working on string library --- diff --git a/bh.h b/bh.h index 24afa216..c0b2a23f 100644 --- a/bh.h +++ b/bh.h @@ -3,7 +3,8 @@ #include #include -#include // TODO: Replace with neede functions +#include +#include // TODO: Replace with needed functions //------------------------------------------------------------------------------------- // Better types @@ -60,6 +61,29 @@ int bh_string_delete(bh_string* str) { return 1; } +int bh_string_ensure_capacity(bh_string* str, u64 cap) { + if (str->capacity >= cap) return 1; + + //TODO: This could fail + str->data = (u8*) realloc((void*) str->data, sizeof(u8) * cap); + str->capacity = cap; + + return 1; +} + +void bh_string_append(bh_string* str1, bh_string* str2) { + if (!bh_string_ensure_capacity(str1, str1->length + str2->length)) return; + + //TODO: Replace with custom memory management + memcpy(str1->data + str1->length, str2->data, str2->length); + str1->length += str2->length; +} + +// TEMP +void bh_string_print(bh_string* str) { + write(STDOUT_FILENO, str->data, str->length); +} + //------------------------------------------------------------------------------------- diff --git a/onyx b/onyx index 8853feca..d4295645 100755 Binary files a/onyx and b/onyx differ diff --git a/onyx.c b/onyx.c index ea8e8b04..4bb216c6 100644 --- a/onyx.c +++ b/onyx.c @@ -22,9 +22,12 @@ // int main(int argc, char *argv[]) { - bh_string test_str = bh_string_new("Hello World!"); + bh_string test_str1 = bh_string_new("Hello "); + bh_string test_str2 = bh_string_new("World!"); - + bh_string_append(&test_str1, &test_str2); + bh_string_print(&test_str1); - bh_string_delete(&test_str); + bh_string_delete(&test_str1); + bh_string_delete(&test_str2); } \ No newline at end of file