Add function to copy Str8 to C string buffer

This commit is contained in:
Abdelrahman Said 2025-02-16 21:30:25 +00:00
parent 76b078fbc0
commit 2c9e4c91a0
2 changed files with 15 additions and 1 deletions

View File

@ -176,6 +176,17 @@ void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src) {
dst->size = to_copy; dst->size = to_copy;
} }
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity) {
if (!dst || !src) {
return;
}
u64 to_copy = src->size < dst_capacity ? src->size : dst_capacity - 1;
memset(dst, 0, dst_capacity);
memcpy(dst, src->buf, to_copy);
}
i64 wapp_str8_find(Str8RO *str, Str8RO substr) { i64 wapp_str8_find(Str8RO *str, Str8RO substr) {
if (substr.size > str->size) { if (substr.size > str->size) {
return -1; return -1;
@ -340,7 +351,7 @@ Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8RO *d
} }
u64 capacity = list->total_size + (delimiter->size * (list->node_count - 1)); u64 capacity = list->total_size + (delimiter->size * (list->node_count - 1));
Str8 *output = wapp_str8_buf_alloc(allocator, capacity); Str8 *output = wapp_str8_buf_alloc(allocator, capacity * 2);
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
// MSVC Spectre mitigation warnings // MSVC Spectre mitigation warnings

View File

@ -72,6 +72,7 @@ Str8 *wapp_str8_concat(const Allocator *allocator, Str8 *dst, Str8RO *src);
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);
void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src); void wapp_str8_copy_str8_capped(Str8 *dst, Str8RO *src);
void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
/** /**
* Str8 find functions * Str8 find functions
@ -91,6 +92,8 @@ Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8R
/** /**
* Str8 list utilities * Str8 list utilities
*/ */
#define wapp_str8_node_from_cstr(STRING) ((Str8Node){.string = &wapp_str8_lit(STRING)})
#define wapp_str8_node_from_str8(STRING) ((Str8Node){.string = &STRING})
Str8Node *wapp_str8_list_get(const Str8List *list, u64 index); Str8Node *wapp_str8_list_get(const Str8List *list, u64 index);
void wapp_str8_list_push_front(Str8List *list, Str8Node *node); void wapp_str8_list_push_front(Str8List *list, Str8Node *node);
void wapp_str8_list_push_back(Str8List *list, Str8Node *node); void wapp_str8_list_push_back(Str8List *list, Str8Node *node);