diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index f8038aa..2261342 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -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; diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index 74321d3..325d36f 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -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 @@ -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 diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 053c1da..06d286c 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -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); +} diff --git a/tests/str8/test_str8.cc b/tests/str8/test_str8.cc index fc2a42f..c4c0aa7 100644 --- a/tests/str8/test_str8.cc +++ b/tests/str8/test_str8.cc @@ -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); +} diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h index 5ed90c0..f3a2221 100644 --- a/tests/str8/test_str8.h +++ b/tests/str8/test_str8.h @@ -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 diff --git a/tests/wapptest.c b/tests/wapptest.c index d75dc79..a134adf 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -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, diff --git a/tests/wapptest.cc b/tests/wapptest.cc index d75dc79..a134adf 100644 --- a/tests/wapptest.cc +++ b/tests/wapptest.cc @@ -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,