0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd::json: Add prepend/append tools for arrays.

This commit is contained in:
Jason Volk 2022-07-13 15:01:53 -07:00
parent 7ca36805be
commit 0725db0431
2 changed files with 50 additions and 0 deletions

View file

@ -43,6 +43,9 @@ namespace ircd::json
// Convenience toolset for higher level operations.
namespace ircd::json
{
strung append(const array &, const string_view &val);
strung prepend(const array &, const string_view &val);
void merge(stack::object &out, const vector &);
strung remove(const object &, const string_view &key);
strung remove(const object &, const size_t &index);

View file

@ -838,6 +838,53 @@ ircd::json::merge(stack::object &out,
top.compose(*out.s);
}
ircd::json::strung
ircd::json::prepend(const array &s,
const string_view &val)
{
if(unlikely(!empty(s) && type(s) != type::ARRAY))
throw type_error
{
"Cannot prepend value into JSON of type %s",
reflect(type(s))
};
size_t ctr{0}, i{0};
auto &b(value_buffer);
b.at(ctr++) = val;
for(const string_view &v : s)
b.at(ctr++) = v;
return strung
{
b.data(), b.data() + ctr
};
}
ircd::json::strung
ircd::json::append(const array &s,
const string_view &val)
{
if(unlikely(!empty(s) && type(s) != type::ARRAY))
throw type_error
{
"Cannot append value into JSON of type %s",
reflect(type(s))
};
size_t ctr{0}, i{0};
auto &b(value_buffer);
for(const string_view &v : s)
b.at(ctr++) = v;
b.at(ctr++) = val;
return strung
{
b.data(), b.data() + ctr
};
}
///////////////////////////////////////////////////////////////////////////////
//
// json/stack.h