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.
This commit is contained in:
2025-10-25 00:04:38 +01:00
parent 7afc398f68
commit a1b177a5a1
2 changed files with 25 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
#include <blue/core/iterator.h>
#include <blue/core/macros.h>
#include <blue/core/status.h>
#include <blue/core/stringstream.h>
#include <ctype.h>
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);

View File

@@ -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);