From 0569fca1938a43271636d46ba6a09dcbb0878f6b Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 16 Feb 2025 17:34:48 +0000 Subject: [PATCH] Add str8_copy functions --- src/core/strings/str8/str8.c | 25 +++++++++++++++++++++++++ src/core/strings/str8/str8.h | 2 ++ tests/str8/test_str8.c | 35 +++++++++++++++++++++++++++++++++++ tests/str8/test_str8.h | 2 ++ tests/wapptest.c | 2 ++ 5 files changed, 66 insertions(+) diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index 62da190..dc53ba3 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -151,6 +151,31 @@ void wapp_str8_concat_capped(Str8 *dst, Str8RO *src) { dst->size += to_copy; } +void wapp_str8_copy_cstr_capped(Str8 *dst, const char *src) { + if (!dst || !src) { + return; + } + + u64 length = strlen(src); + u64 to_copy = length <= dst->capacity ? length : dst->capacity; + + memset(dst->buf, 0, dst->size); + memcpy(dst->buf, src, to_copy); + dst->size = to_copy; +} + +void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) { + if (!dst || !src) { + return; + } + + u64 to_copy = src->size <= dst->capacity ? src->size : dst->capacity; + + memset(dst->buf, 0, dst->size); + memcpy(dst->buf, src->buf, to_copy); + dst->size = to_copy; +} + i64 wapp_str8_find(Str8RO *str, Str8RO substr) { if (substr.size > str->size) { return -1; diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h index 55d10bc..84c620a 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -70,6 +70,8 @@ bool wapp_str8_equal(Str8RO *s1, Str8RO *s2); Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end); Str8 *wapp_str8_concat(const Allocator *allocator, 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_str8_capped(Str8 *dst, Str8RO *src); /** * Str8 find functions diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index 2b23a7f..71bc032 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -278,6 +278,41 @@ TestFuncResult test_str8_concat_capped(void) { return wapp_tester_result(result); } +TestFuncResult test_str8_copy_cstr_capped(void) { + bool result; + + Str8 buf = wapp_str8_buf(32); + const char *src1 = "Hello world"; + const char *src2 = "Hello world from the Wizard Apprentice standard library"; + Str8RO src1_cp = wapp_str8_lit_ro("Hello world"); + Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr"); + + wapp_str8_copy_cstr_capped(&buf, src1); + result = buf.size == src1_cp.size && wapp_str8_equal(&buf, &src1_cp); + + wapp_str8_copy_cstr_capped(&buf, src2); + result = result && buf.size == src2_cp.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_copy_str8_capped(void) { + bool result; + + Str8 buf = wapp_str8_buf(32); + Str8RO src1 = wapp_str8_lit_ro("Hello world"); + Str8RO src2 = wapp_str8_lit_ro("Hello world from the Wizard Apprentice standard library"); + Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr"); + + wapp_str8_copy_str8_capped(&buf, &src1); + result = buf.size == src1.size && wapp_str8_equal(&buf, &src1); + + wapp_str8_copy_str8_capped(&buf, &src2); + result = result && buf.size < src2.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp); + + return wapp_tester_result(result); +} + TestFuncResult test_str8_find(void) { bool result; Str8RO s = wapp_str8_lit("Do as I say, not as I do"); diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h index 025ec52..fc5f6fa 100644 --- a/tests/str8/test_str8.h +++ b/tests/str8/test_str8.h @@ -20,6 +20,8 @@ TestFuncResult test_str8_equal(void); TestFuncResult test_str8_substr(void); TestFuncResult test_str8_concat(void); TestFuncResult test_str8_concat_capped(void); +TestFuncResult test_str8_copy_cstr_capped(void); +TestFuncResult test_str8_copy_str8_capped(void); TestFuncResult test_str8_find(void); TestFuncResult test_str8_rfind(void); TestFuncResult test_str8_split(void); diff --git a/tests/wapptest.c b/tests/wapptest.c index 805a483..ca1a29f 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -26,6 +26,8 @@ int main(void) { test_str8_substr, test_str8_concat, test_str8_concat_capped, + test_str8_copy_cstr_capped, + test_str8_copy_str8_capped, test_str8_find, test_str8_rfind, test_str8_split,