object: update to_string callback to use b_stream instead of b_stringstream

This commit is contained in:
2025-06-27 21:47:55 +01:00
parent 579a9e8505
commit c54d51d381
9 changed files with 237 additions and 51 deletions

View File

@@ -1,12 +1,14 @@
#include "array.h"
#include <blue/core/iterator.h>
#include <blue/core/stream.h>
#include <blue/object/array.h>
#include <blue/object/type.h>
#include <stdlib.h>
#include <string.h>
static void array_release(struct b_object *obj);
static void array_to_string(struct b_object *obj, struct b_stream *out);
static struct b_object_type array_type = {
.t_flags = B_OBJECT_FUNDAMENTAL,
@@ -14,6 +16,7 @@ static struct b_object_type array_type = {
.t_name = "corelib::array",
.t_instance_size = sizeof(struct b_array),
.t_release = array_release,
.t_to_string = array_to_string,
};
struct b_array *b_array_create(void)
@@ -218,7 +221,47 @@ size_t b_array_capacity(const struct b_array *array)
return array->ar_cap;
}
void array_release(struct b_object *obj)
static void array_to_string(struct b_object *obj, struct b_stream *out)
{
struct b_array *array = B_ARRAY(obj);
if (!array->ar_len) {
b_stream_write_string(out, "[]", NULL);
return;
}
b_stream_write_string(out, "[\n", NULL);
b_stream_push_indent(out, 1);
size_t len = b_array_size(array);
b_array_iterator it;
b_array_foreach(&it, array)
{
bool is_string = b_typeid(it.value) == B_OBJECT_TYPE_STRING;
if (is_string) {
b_stream_write_char(out, '"');
}
b_to_string(it.value, out);
if (is_string) {
b_stream_write_char(out, '"');
}
if (it.i < len - 1) {
b_stream_write_string(out, ",", NULL);
}
b_stream_write_char(out, '\n');
}
b_stream_pop_indent(out);
b_stream_write_char(out, ']');
}
static void array_release(struct b_object *obj)
{
struct b_array *array = B_ARRAY(obj);