From a1b177a5a1d041a27fcc690014566b3ed011c6e7 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sat, 25 Oct 2025 00:04:38 +0100 Subject: [PATCH] ds: string: add function to copy string data from a b_stringstream unlike b_stringstream_ptr(), this function supports copying strings that contain null bytes. --- ds/include/blue/ds/string.h | 3 +++ ds/string.c | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/ds/include/blue/ds/string.h b/ds/include/blue/ds/string.h index 17cbce0..2cd2c4c 100644 --- a/ds/include/blue/ds/string.h +++ b/ds/include/blue/ds/string.h @@ -5,6 +5,7 @@ #include #include #include +#include #include B_DECLS_BEGIN; @@ -70,6 +71,8 @@ BLUE_API b_status b_string_reserve(b_string *str, size_t capacity); BLUE_API b_status b_string_replace( b_string *str, size_t start, size_t length, const char *new_data); BLUE_API b_status b_string_replace_all(b_string *str, const char *new_data); +BLUE_API b_status b_string_replace_all_with_stringstream( + b_string *str, const b_stringstream *new_data); BLUE_API b_status b_string_remove(b_string *str, size_t start, size_t length); BLUE_API b_status b_string_transform(b_string *str, int (*transformer)(int)); BLUE_API b_status b_string_trim(b_string *str); diff --git a/ds/string.c b/ds/string.c index e8f5f85..955d318 100644 --- a/ds/string.c +++ b/ds/string.c @@ -419,6 +419,21 @@ static b_status string_replace_all(struct b_string_p *str, const char *new_data) return B_SUCCESS; } +static b_status string_replace_all_with_stringstream( + struct b_string_p *str, const b_stringstream *new_data) +{ + size_t new_len = b_stringstream_get_length(new_data); + string_reserve(str, new_len); + char *dest = string_ptr(str); + memcpy(dest, b_stringstream_ptr(new_data), new_len); + dest[new_len] = '\0'; + + str->s_len = new_len; + str->s_codepoints = b_wchar_utf8_codepoint_count(dest, new_len); + + return B_SUCCESS; +} + static enum b_status remove_ansi(struct b_string_p *str, size_t start, size_t length) { b_status status = B_SUCCESS; @@ -1124,6 +1139,13 @@ b_status b_string_replace_all(b_string *str, const char *new_data) B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_replace_all, str, new_data); } +b_status b_string_replace_all_with_stringstream( + b_string *str, const b_stringstream *new_data) +{ + B_CLASS_DISPATCH_STATIC( + B_TYPE_STRING, string_replace_all_with_stringstream, str, new_data); +} + enum b_status b_string_remove(b_string *str, size_t start, size_t length) { B_CLASS_DISPATCH_STATIC(B_TYPE_STRING, string_remove, str, start, length);