diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index ef91f73..a465fbb 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -1,7 +1,9 @@ #include "str8.h" #include "aliases.h" #include "mem_allocator.h" +#include #include +#include #include #include @@ -150,7 +152,7 @@ Str8 wapp_str8_slice(Str8RO *str, u64 start, u64 end) { }; } -Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) { +Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src) { if (!allocator || !dst || !src) { return NULL; } @@ -226,6 +228,26 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) { memcpy(dst, src->buf, to_copy); } +void wapp_str8_format(Str8 *dst, const char *format, ...) { + if (!dst || !format) { + return; + } + + va_list args1; + va_list args2; + + va_start(args1, format); + va_copy(args2, args1); + + u64 total_size = vsnprintf(NULL, 0, format, args1); + dst->size = total_size <= dst->capacity ? total_size : dst->capacity; + + vsnprintf((char *)(dst->buf), dst->capacity, format, args2); + + va_end(args1); + va_end(args2); +} + 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 e0e4e0b..f8632b1 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -67,6 +67,7 @@ Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity); Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str); Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str); Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end); +Str8 *wapp_str8_alloc_concat(const Allocator *allocator, Str8 *dst, Str8RO *src); // Only needed for allocators like malloc where each allocation has to be freed on its own. // No need to use it for allocators like Arena. void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str); @@ -74,16 +75,16 @@ void wapp_str8_dealloc_buf(const Allocator *allocator, Str8 **str); /** * Str8 utilities */ -c8 wapp_str8_get(Str8RO *str, u64 index); -void wapp_str8_set(Str8 *str, u64 index, c8 c); -bool wapp_str8_equal(Str8RO *s1, Str8RO *s2); -bool wapp_str8_equal_to_count(Str8RO* s1, Str8RO* s2, u64 count); -Str8 wapp_str8_slice(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); -void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity); +c8 wapp_str8_get(Str8RO *str, u64 index); +void wapp_str8_set(Str8 *str, u64 index, c8 c); +bool wapp_str8_equal(Str8RO *s1, Str8RO *s2); +bool 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); +void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src); +void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity); +void wapp_str8_format(Str8 *dst, const char *format, ...); /** * Str8 find functions diff --git a/src/os/shell/commander/commander.c b/src/os/shell/commander/commander.c index 6a76ac5..1d3b40f 100644 --- a/src/os/shell/commander/commander.c +++ b/src/os/shell/commander/commander.c @@ -32,7 +32,7 @@ CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_bu } // Redirect output - cmd_str = wapp_str8_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1")); + cmd_str = wapp_str8_alloc_concat(&arena, cmd_str, &wapp_str8_lit_ro(" 2>&1")); CMDResult output = execute_command(cmd_str, out_handling, out_buf); diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c index c7380a1..21ad40f 100644 --- a/tests/str8/test_str8.c +++ b/tests/str8/test_str8.c @@ -270,7 +270,7 @@ TestFuncResult test_str8_slice(void) { return wapp_tester_result(result); } -TestFuncResult test_str8_concat(void) { +TestFuncResult test_str8_alloc_concat(void) { bool result; Allocator arena = wapp_mem_arena_allocator_init(KB(100)); @@ -282,10 +282,10 @@ TestFuncResult test_str8_concat(void) { Str8 *output; - output = wapp_str8_concat(&arena, &str, &suffix1); + output = wapp_str8_alloc_concat(&arena, &str, &suffix1); result = output->size == concat1.size && wapp_str8_equal(output, &concat1); - output = wapp_str8_concat(&arena, output, &suffix2); + output = wapp_str8_alloc_concat(&arena, output, &suffix2); result = result && output->size == concat2.size && wapp_str8_equal(output, &concat2); wapp_mem_arena_allocator_destroy(&arena); diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h index 4a60836..6b2bba3 100644 --- a/tests/str8/test_str8.h +++ b/tests/str8/test_str8.h @@ -14,12 +14,12 @@ TestFuncResult test_str8_alloc_buf(void); TestFuncResult test_str8_alloc_cstr(void); TestFuncResult test_str8_alloc_str8(void); TestFuncResult test_str8_alloc_substr(void); +TestFuncResult test_str8_alloc_concat(void); TestFuncResult test_str8_get_index_within_bounds(void); TestFuncResult test_str8_get_index_out_of_bounds(void); TestFuncResult test_str8_set(void); TestFuncResult test_str8_equal(void); TestFuncResult test_str8_slice(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); diff --git a/tests/wapptest.c b/tests/wapptest.c index 840d7cf..6d59fff 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -20,12 +20,12 @@ int main(void) { test_str8_alloc_cstr, test_str8_alloc_str8, test_str8_alloc_substr, + test_str8_alloc_concat, test_str8_get_index_within_bounds, test_str8_get_index_out_of_bounds, test_str8_set, test_str8_equal, test_str8_slice, - test_str8_concat, test_str8_concat_capped, test_str8_copy_cstr_capped, test_str8_copy_str8_capped,