Add wapp_str8_from_bytes

This commit is contained in:
Abdelrahman Said
2025-09-14 21:11:31 +01:00
parent 0dd9f94a65
commit b4eb8d2760
7 changed files with 46 additions and 4 deletions

View File

@@ -124,7 +124,7 @@ void wapp_str8_push_back(Str8 *str, c8 c) {
wapp_str8_set(str, index, 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) { if (s1->size != s2->size) {
return false; return false;
} }
@@ -132,7 +132,7 @@ bool wapp_str8_equal(Str8RO *s1, Str8RO *s2) {
return wapp_str8_equal_to_count(s1, s2, s1->size); 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) { if (!s1 || !s2) {
return false; 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) { i64 wapp_str8_find(Str8RO *str, Str8RO substr) {
if (!str || substr.size > str->size) { if (!str || substr.size > str->size) {
return -1; return -1;

View File

@@ -6,6 +6,7 @@
#include "../../../common/aliases/aliases.h" #include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h" #include "../../../common/assert/assert.h"
#include "../../../common/platform/platform.h" #include "../../../common/platform/platform.h"
#include "../../../primitives/array/array.h"
#include "../../../primitives/dbl_list/dbl_list.h" #include "../../../primitives/dbl_list/dbl_list.h"
#include "../../mem_allocator/mem_allocator.h" #include "../../mem_allocator/mem_allocator.h"
#include <string.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); c8 wapp_str8_get(Str8RO *str, u64 index);
void wapp_str8_set(Str8 *str, u64 index, c8 c); void wapp_str8_set(Str8 *str, u64 index, c8 c);
void wapp_str8_push_back(Str8 *str, c8 c); void wapp_str8_push_back(Str8 *str, c8 c);
bool wapp_str8_equal(Str8RO *s1, Str8RO *s2); b32 wapp_str8_equal(Str8RO *s1, Str8RO *s2);
bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count); b32 wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count);
Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end); Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end);
void wapp_str8_concat_capped(Str8 *dst, Str8RO *src); void wapp_str8_concat_capped(Str8 *dst, Str8RO *src);
void wapp_str8_copy_cstr_capped(Str8 *dst, const char *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_format(Str8 *dst, const char *format, ...);
void wapp_str8_to_lower(Str8 *dst, Str8RO *src); void wapp_str8_to_lower(Str8 *dst, Str8RO *src);
void wapp_str8_to_upper(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 * Str8 find functions

View File

@@ -612,3 +612,16 @@ TestFuncResult test_str8_join(void) {
return wapp_tester_result(result); 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); 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(void);
TestFuncResult test_str8_rsplit_with_max(void); TestFuncResult test_str8_rsplit_with_max(void);
TestFuncResult test_str8_join(void); TestFuncResult test_str8_join(void);
TestFuncResult test_str8_from_bytes(void);
#endif // !TEST_STR8_H #endif // !TEST_STR8_H

View File

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

View File

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