Working on string library
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 May 2020 03:21:53 +0000 (22:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 May 2020 03:21:53 +0000 (22:21 -0500)
bh.h
onyx
onyx.c

diff --git a/bh.h b/bh.h
index 24afa2165389f824390e8461ecc8b00069b9bb0b..c0b2a23f366c0134659b0c9a565b80d03c2131db 100644 (file)
--- a/bh.h
+++ b/bh.h
@@ -3,7 +3,8 @@
 #include <fcntl.h>
 #include <errno.h>
 
-#include <string.h> // TODO: Replace with neede functions
+#include <stdlib.h>
+#include <string.h> // 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 8853feca6175d062c3fed89324cf3fc5f652b1dc..d4295645e1a50997b5f9704ea3fd40f6121210f5 100755 (executable)
Binary files a/onyx and b/onyx differ
diff --git a/onyx.c b/onyx.c
index ea8e8b041c272b79ac27d84f15fc1149b258e883..4bb216c67d4a99a2bf9e20105dbc5bf6ee21e11f 100644 (file)
--- a/onyx.c
+++ b/onyx.c
 // 
 
 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