ds: string: update to_string(); remove legacy b_stream callbacks

b_string will no longer implement the b_stream interface. writing to a string
via b_stream will now be handled by b_stringstream.
This commit is contained in:
2025-10-24 12:39:19 +01:00
parent 0fed0d2718
commit d5136239a8
2 changed files with 1 additions and 128 deletions

View File

@@ -81,7 +81,6 @@ static inline b_status b_string_tolower(b_string *str)
{ {
return b_string_transform(str, tolower); return b_string_transform(str, tolower);
} }
BLUE_API b_status b_string_open_stream(b_string *str, struct b_stream **out);
BLUE_API b_status b_string_append_c(b_string *dest, char c); BLUE_API b_status b_string_append_c(b_string *dest, char c);
BLUE_API b_status b_string_append_wc(b_string *dest, b_wchar c); BLUE_API b_status b_string_append_wc(b_string *dest, b_wchar c);

View File

@@ -1214,107 +1214,6 @@ static uint64_t string_hash(const struct b_string_p *str)
return hash; return hash;
} }
/*** STREAM FUNCTIONS *********************************************************/
static enum b_status stream_close(struct b_stream *stream)
{
b_string *str = stream->s_ptr0;
b_string_unref(str);
return B_SUCCESS;
}
static enum b_status stream_getc(struct b_stream *stream, int *out)
{
struct b_string_p *str = stream->s_ptr1;
if (stream->s_cursor >= str->s_len) {
return B_ERR_NO_DATA;
}
char *s = string_ptr(str);
*out = s[stream->s_cursor];
stream->s_cursor++;
return B_SUCCESS;
}
static enum b_status stream_read(
struct b_stream *stream, unsigned char *buf, size_t count, size_t *nr_read)
{
struct b_string_p *str = stream->s_ptr1;
if (stream->s_cursor >= str->s_len) {
*nr_read = 0;
return B_SUCCESS;
}
size_t available = str->s_len - stream->s_cursor;
size_t to_read = b_min(size_t, count, available);
char *s = string_ptr(str) + stream->s_cursor;
memcpy(buf, s, to_read);
*nr_read = to_read;
return B_SUCCESS;
}
static enum b_status stream_write(
struct b_stream *stream, const unsigned char *buf, size_t count,
size_t *nr_written)
{
struct b_string_p *str = stream->s_ptr1;
enum b_status status = B_SUCCESS;
if (stream->s_cursor + count > str->s_max) {
status = string_reserve(str, stream->s_cursor + count);
}
if (!B_OK(status)) {
return status;
}
string_insert_cstr(str, (const char *)buf, count, stream->s_cursor);
stream->s_cursor += count;
*nr_written = count;
return B_SUCCESS;
}
static enum b_status stream_seek(
struct b_stream *stream, long long offset, b_stream_seek_origin origin)
{
struct b_string_p *str = stream->s_ptr1;
size_t abs_offset;
switch (origin) {
case B_STREAM_SEEK_START:
abs_offset = offset;
break;
case B_STREAM_SEEK_CURRENT:
abs_offset = stream->s_cursor + offset;
break;
case B_STREAM_SEEK_END:
abs_offset = str->s_len + offset;
break;
default:
return B_ERR_INVALID_ARGUMENT;
}
stream->s_cursor = abs_offset;
return B_SUCCESS;
}
static enum b_status stream_reserve(struct b_stream *stream, size_t len)
{
struct b_string_p *str = stream->s_ptr1;
size_t new_capacity = str->s_len + len;
return string_reserve(str, new_capacity);
}
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
b_string *b_string_create_from_cstr(const char *s) b_string *b_string_create_from_cstr(const char *s)
@@ -1539,31 +1438,6 @@ uint64_t b_string_hash(const b_string *str)
B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_hash, str); B_CLASS_DISPATCH_STATIC_0(B_TYPE_STRING, string_hash, str);
} }
enum b_status b_string_open_stream(b_string *str, struct b_stream **out)
{
struct b_stream *stream = malloc(sizeof *stream);
if (!stream) {
return B_ERR_NO_MEMORY;
}
memset(stream, 0x0, sizeof *stream);
stream->s_mode |= B_STREAM_READ | B_STREAM_WRITE;
stream->s_ptr0 = b_string_ref(str);
stream->s_ptr1 = b_object_get_private(str, B_TYPE_STRING);
stream->s_close = stream_close;
stream->s_getc = stream_getc;
stream->s_read = stream_read;
stream->s_write = stream_write;
stream->s_seek = stream_seek;
stream->s_reserve = stream_reserve;
*out = stream;
return B_SUCCESS;
}
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/ /*** PUBLIC ALIAS FUNCTIONS ***************************************************/
enum b_status b_string_append_c(b_string *dest, char c) enum b_status b_string_append_c(b_string *dest, char c)
@@ -1635,7 +1509,7 @@ static void string_fini(b_object *obj, void *priv)
} }
} }
static void string_to_string(const b_object *obj, struct b_stream *out) static void string_to_string(const b_object *obj, b_stream *out)
{ {
struct b_string_p *str = b_object_get_private(obj, B_TYPE_STRING); struct b_string_p *str = b_object_get_private(obj, B_TYPE_STRING);
const char *s = string_ptr(str); const char *s = string_ptr(str);