mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
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:
@@ -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)!;
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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 --
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user