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,7 +1,7 @@
#include "dict.h"
#include <blue/core/status.h>
#include <blue/core/stringstream.h>
#include <blue/core/stream.h>
#include <blue/object/dict.h>
#include <blue/object/string.h>
#include <blue/object/type.h>
@@ -24,7 +24,7 @@ uint64_t b_cstr_hash(const char *s)
}
static void dict_release(struct b_object *obj);
static void dict_to_string(struct b_object *obj, struct b_stringstream *out);
static void dict_to_string(struct b_object *obj, struct b_stream *out);
static struct b_object_type dict_type = {
.t_name = "corelib::dict",
@@ -186,35 +186,46 @@ bool b_dict_is_empty(const b_dict *dict)
return false;
}
static void dict_to_string(struct b_object *obj, struct b_stringstream *out)
static void dict_to_string(struct b_object *obj, struct b_stream *out)
{
struct b_dict *dict = B_DICT(obj);
if (b_dict_is_empty(dict)) {
b_stringstream_add(out, "{}");
b_stream_write_string(out, "{}", NULL);
return;
}
b_stringstream_add(out, "{\n");
b_stream_write_string(out, "{\n", NULL);
b_stringstream_push_indent(out, 1);
b_stream_push_indent(out, 1);
size_t len = b_dict_get_size(dict);
b_dict_iterator it;
b_dict_foreach(&it, dict)
{
b_stringstream_addf(out, "%s: ", it.key);
b_to_string(it.value, out);
b_stream_write_fmt(out, NULL, "%s: ", it.key);
if (it.i < len - 1) {
b_stringstream_add(out, ",");
bool is_string = b_typeid(it.value) == B_OBJECT_TYPE_STRING;
if (is_string) {
b_stream_write_char(out, '"');
}
b_stringstream_add(out, "\n");
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_stringstream_pop_indent(out);
b_stringstream_add(out, "}");
b_stream_pop_indent(out);
b_stream_write_char(out, '}');
}
static bool dict_iterator_next(struct b_iterator *it)