File utilities and datatype implementation for a C-based code generator #5

Merged
abdelrahman merged 18 commits from ccodegen into main 2025-09-20 13:48:08 +00:00
7 changed files with 46 additions and 4 deletions
Showing only changes of commit b4eb8d2760 - Show all commits

View File

@@ -124,7 +124,7 @@ void wapp_str8_push_back(Str8 *str, c8 c) {
wapp_str8_set(str, index, c);
}
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
b32 wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
if (s1->size != s2->size) {
return false;
}
@@ -132,7 +132,7 @@ bool wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
return wapp_str8_equal_to_count(s1, s2, s1->size);
}
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count) {
b32 wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count) {
if (!s1 || !s2) {
return false;
}
@@ -261,6 +261,16 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) {
}
}
void wapp_str8_from_bytes(Str8 *dst, const U8Array *src) {
u64 size = src->count * src->item_size;
wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL");
wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity");
dst->size = size;
memcpy(dst->buf, src->items, size);
}
i64 wapp_str8_find(Str8RO *str, Str8RO substr) {
if (!str || substr.size > str->size) {
return -1;

View File

@@ -6,6 +6,7 @@
#include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h"
#include "../../../common/platform/platform.h"
#include "../../../primitives/array/array.h"
#include "../../../primitives/dbl_list/dbl_list.h"
#include "../../mem_allocator/mem_allocator.h"
#include <string.h>
@@ -89,8 +90,8 @@ void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str);
c8 wapp_str8_get(Str8RO *str, u64 index);
void wapp_str8_set(Str8 *str, u64 index, c8 c);
void wapp_str8_push_back(Str8 *str, c8 c);
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2);
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
b32 wapp_str8_equal(Str8RO *s1, Str8RO *s2);
b32 wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src);
@@ -99,6 +100,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
void wapp_str8_format(Str8 *dst, const char *format, ...);
void wapp_str8_to_lower(Str8 *dst, Str8RO *src);
void wapp_str8_to_upper(Str8 *dst, Str8RO *src);
void wapp_str8_from_bytes(Str8 *dst, const U8Array *src);
/**
* Str8 find functions

View File

@@ -612,3 +612,16 @@ TestFuncResult test_str8_join(void) {
return wapp_tester_result(result);
}
TestFuncResult test_str8_from_bytes(void) {
bool result;
Str8 str = wapp_str8_buf(1024);
U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P');
wapp_str8_from_bytes(&str, &bytes);
result = str.size == bytes.count * bytes.item_size;
result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP"));
return wapp_tester_result(result);
}

View File

@@ -612,3 +612,17 @@ TestFuncResult test_str8_join(void) {
return wapp_tester_result(result);
}
TestFuncResult test_str8_from_bytes(void) {
bool result;
Str8 str = wapp_str8_buf(1024);
Str8 expected = wapp_str8_lit_ro("WAPP");
U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P');
wapp_str8_from_bytes(&str, &bytes);
result = str.size == bytes.count * bytes.item_size;
result = result && wapp_str8_equal(&str, &expected);
return wapp_tester_result(result);
}

View File

@@ -28,5 +28,6 @@ TestFuncResult test_str8_split_with_max(void);
TestFuncResult test_str8_rsplit(void);
TestFuncResult test_str8_rsplit_with_max(void);
TestFuncResult test_str8_join(void);
TestFuncResult test_str8_from_bytes(void);
#endif // !TEST_STR8_H

View File

@@ -56,6 +56,7 @@ int main(void) {
test_str8_rsplit,
test_str8_rsplit_with_max,
test_str8_join,
test_str8_from_bytes,
test_str8_list_get,
test_str8_list_push_front,
test_str8_list_push_back,

View File

@@ -56,6 +56,7 @@ int main(void) {
test_str8_rsplit,
test_str8_rsplit_with_max,
test_str8_join,
test_str8_from_bytes,
test_str8_list_get,
test_str8_list_push_front,
test_str8_list_push_back,