Add string methods to json, and fix issue in dstring when the formatter uses temp. Remove unnecessary use of temp allocator in to_format for json.

This commit is contained in:
Christoffer Lerno
2024-08-10 19:13:58 +02:00
parent 05421223be
commit 811cb2b95c
4 changed files with 62 additions and 12 deletions

View File

@@ -48,9 +48,9 @@ fn usz! Object.to_format(&self, Formatter* formatter) @dynamic
return n;
case ObjectInternalMap:
usz n = formatter.printf("{")!;
@pool()
@stack_mem(1024; Allocator mem)
{
foreach (i, key : self.map.key_tlist())
foreach (i, key : self.map.key_new_list(mem))
{
if (i > 0) n += formatter.printf(",")!;
n += formatter.printf(`"%s":`, key)!;

View File

@@ -371,18 +371,24 @@ fn void DString.insert_at(&self, usz index, String s)
fn usz! DString.appendf(&self, String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_string_append_fn, self);
return formatter.vprintf(format, args);
@pool(self.data().allocator)
{
Formatter formatter;
formatter.init(&out_string_append_fn, self);
return formatter.vprintf(format, args);
};
}
fn usz! DString.appendfn(&self, String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_string_append_fn, self);
usz len = formatter.vprintf(format, args)!;
self.append('\n');
return len + 1;
@pool(self.data().allocator)
{
Formatter formatter;
formatter.init(&out_string_append_fn, self);
usz len = formatter.vprintf(format, args)!;
self.append('\n');
return len + 1;
};
}
fn DString new_join(String[] s, String joiner, Allocator allocator = allocator::heap())

View File

@@ -15,11 +15,29 @@ fault JsonParsingError
INVALID_NUMBER,
}
fn Object*! parse_string(String s, Allocator allocator = allocator::heap())
{
return parse(ByteReader{}.init(s), allocator);
}
fn Object*! temp_parse_string(String s)
{
return parse(ByteReader{}.init(s), allocator::temp());
}
fn Object*! parse(InStream s, Allocator allocator = allocator::heap())
{
JsonContext context = { .last_string = dstring::new_with_capacity(64, allocator), .stream = s, .allocator = allocator };
defer context.last_string.free();
return parse_any(&context);
@pool(allocator)
{
return parse_any(&context);
};
}
fn Object*! temp_parse(InStream s)
{
return parse(s, allocator::temp());
}
// -- Implementation follows --

View File

@@ -24,4 +24,30 @@ fn void! simple_test2()
reader.init(`{"jsonrpc":"2.0","id":null,"method":"initialize"}`);
Object* o = json::parse(&reader)!;
defer o.free();
}
}
fn void! test_string()
{
Object* o = json::parse_string(`{"jsonrpc":"2","id":null,"method":"initialize"}`)!;
defer o.free();
String s = string::tformat("%s", *o);
Object* o2 = json::parse_string(s)!;
defer o2.free();
String s2 = string::tformat("%s", *o2);
assert(s2 == s, "Unexpectedly got %s and not %s", s2, s);
}
fn void! test_temp_string()
{
@pool()
{
Object* o = json::temp_parse_string(`{"jsonrpc":"2","id":null,"method":"initialize"}`)!;
defer o.free();
String s = string::tformat("%s", *o);
Object* o2 = json::temp_parse_string(s)!;
defer o2.free();
String s2 = string::tformat("%s", *o2);
assert(s2 == s, "Unexpectedly got %s and not %s", s2, s);
};
}