2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2020-05-11 10:56:47 +02:00
|
|
|
#pragma GCC visibility push(internal)
|
2017-09-19 11:09:19 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
|
|
|
ircd::json::member,
|
|
|
|
( decltype(ircd::json::member::first), first )
|
|
|
|
( decltype(ircd::json::member::second), second )
|
|
|
|
)
|
2020-05-11 10:56:47 +02:00
|
|
|
#pragma GCC visibility pop
|
2017-09-19 11:09:19 +02:00
|
|
|
|
2020-05-11 10:56:47 +02:00
|
|
|
#pragma GCC visibility push(internal)
|
2017-09-19 11:09:19 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
|
|
|
ircd::json::object::member,
|
|
|
|
( decltype(ircd::json::object::member::first), first )
|
|
|
|
( decltype(ircd::json::object::member::second), second )
|
|
|
|
)
|
2020-05-11 10:56:47 +02:00
|
|
|
#pragma GCC visibility pop
|
2017-09-19 11:09:19 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
using namespace ircd::spirit;
|
2020-05-20 09:00:06 +02:00
|
|
|
|
|
|
|
template<class T = unused_type,
|
|
|
|
class... A>
|
2022-06-18 06:33:15 +02:00
|
|
|
struct [[clang::internal_linkage]] rule
|
|
|
|
:qi::rule<const char *, T, A...>
|
|
|
|
{
|
|
|
|
using qi::rule<const char *, T, A...>::rule;
|
|
|
|
};
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr NUL { lit('\0') ,"nul" };
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
// insignificant whitespaces
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr SP { lit('\x20') ,"space" };
|
|
|
|
const expr HT { lit('\x09') ,"horizontal tab" };
|
|
|
|
const expr CR { lit('\x0D') ,"carriage return" };
|
|
|
|
const expr LF { lit('\x0A') ,"line feed" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
// whitespace skipping
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr WS { SP | HT | CR | LF ,"whitespace" };
|
|
|
|
const expr ws { *(WS) ,"whitespace monoid" };
|
|
|
|
const expr wsp { +(WS) ,"whitespace semigroup" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
// structural
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr object_begin { lit('{') ,"object begin" };
|
|
|
|
const expr object_end { lit('}') ,"object end" };
|
|
|
|
const expr array_begin { lit('[') ,"array begin" };
|
|
|
|
const expr array_end { lit(']') ,"array end" };
|
|
|
|
const expr name_sep { lit(':') ,"name sep" };
|
|
|
|
const expr value_sep { lit(',') ,"value sep" };
|
|
|
|
const expr escape { lit('\\') ,"escape" };
|
|
|
|
const expr quote { lit('"') ,"quote" };
|
2017-12-25 04:21:49 +01:00
|
|
|
|
2018-01-30 22:37:13 +01:00
|
|
|
// literal
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr lit_false { lit("false") ,"literal false" };
|
|
|
|
const expr lit_true { lit("true") ,"literal true" };
|
|
|
|
const expr lit_null { lit("null") ,"null" };
|
|
|
|
const expr boolean { lit_true | lit_false ,"boolean" };
|
|
|
|
const expr literal { lit_true | lit_false | lit_null ,"literal" };
|
2018-01-30 22:37:13 +01:00
|
|
|
|
2020-06-11 12:32:41 +02:00
|
|
|
// numerical
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr number_int
|
2020-06-11 12:32:41 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
(char_('1', '9') >> repeat(0,18)[char_('0', '9')]) | lit('0')
|
2020-06-11 12:32:41 +02:00
|
|
|
,"integer"
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr number_frac
|
2020-06-11 12:32:41 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
lit('.') >> repeat(1, 19)[char_('0', '9')] >> -char_('1', '9')
|
2020-06-11 12:32:41 +02:00
|
|
|
,"fraction"
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr number_exp
|
2020-06-11 12:32:41 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
(lit('e') | lit('E')) >> -(lit('+') | lit('-')) >> repeat(1, 4)[char_('0', '9')]
|
2020-06-11 12:32:41 +02:00
|
|
|
,"exponent"
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr number
|
2018-03-28 01:15:20 +02:00
|
|
|
{
|
2020-06-18 06:11:30 +02:00
|
|
|
-lit('-') >> number_int >> -number_frac >> -number_exp
|
2018-03-28 01:15:20 +02:00
|
|
|
,"number"
|
|
|
|
};
|
2018-01-30 22:37:13 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr number_begin
|
2020-09-28 00:26:15 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
char_('0', '9') | lit('-')
|
2020-09-28 00:26:15 +02:00
|
|
|
,"first character of number"
|
|
|
|
};
|
|
|
|
|
2020-06-11 12:32:41 +02:00
|
|
|
// string
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr utf16_surrogate
|
2019-08-01 21:26:05 +02:00
|
|
|
{
|
|
|
|
qi::uint_parser
|
|
|
|
<
|
|
|
|
uint16_t, // 16 bit width
|
|
|
|
16U, // base-16 (hex)
|
|
|
|
4U, // minimum digits
|
|
|
|
4U // maximum digits
|
|
|
|
>{}
|
|
|
|
,"UTF-16 surrogate"
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr unicode
|
2017-12-25 04:21:49 +01:00
|
|
|
{
|
2019-08-01 21:26:05 +02:00
|
|
|
lit('u') >> utf16_surrogate
|
2017-12-25 04:21:49 +01:00
|
|
|
,"escaped unicode"
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr control
|
2019-08-01 22:03:06 +02:00
|
|
|
{
|
|
|
|
char_('\x00', '\x1F')
|
|
|
|
,"control character"
|
|
|
|
};
|
|
|
|
|
|
|
|
// characters that must be escaped
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr escaped
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2019-08-01 22:03:06 +02:00
|
|
|
quote | escape | control
|
|
|
|
,"escaped character"
|
2017-11-30 19:49:16 +01:00
|
|
|
};
|
|
|
|
|
2019-08-01 22:03:06 +02:00
|
|
|
// characters that should appear after an escaping solidus
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr escaper
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2020-05-25 06:45:32 +02:00
|
|
|
char_("btnfr0\"\\") | unicode
|
2019-08-01 22:03:06 +02:00
|
|
|
,"escaper"
|
2017-11-30 19:49:16 +01:00
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
// escapers supersetting the rule above with addl non-canonical chars
|
|
|
|
const expr escaper_nc
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
|
|
|
escaper | lit('/')
|
2019-08-01 22:03:06 +02:00
|
|
|
,"escaper"
|
2017-11-30 19:49:16 +01:00
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr chars
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2020-07-21 09:36:54 +02:00
|
|
|
//raw[*((char_ - escaped) | (escape >> escaper_nc))]
|
|
|
|
raw[*((~char_('\x00', '\x1F') - char_("\x22\x5C")) | (escape >> escaper_nc))]
|
2017-11-30 19:49:16 +01:00
|
|
|
,"characters"
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
// optimized string
|
|
|
|
template<class block_t>
|
|
|
|
static u64x2 string_content_block(const block_t, const block_t) noexcept;
|
|
|
|
const custom_parser<0> string_content;
|
2019-06-24 11:30:14 +02:00
|
|
|
const rule<string_view> string
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2020-08-30 12:45:52 +02:00
|
|
|
//quote >> chars >> (!escape >> quote)
|
2020-09-17 11:31:50 +02:00
|
|
|
string_content
|
2017-11-30 19:49:16 +01:00
|
|
|
,"string"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-11-30 19:49:16 +01:00
|
|
|
// container
|
2019-06-24 11:30:14 +02:00
|
|
|
const rule<string_view> name
|
2017-11-30 19:49:16 +01:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
//string.alias()
|
|
|
|
string_content
|
2017-11-30 19:49:16 +01:00
|
|
|
,"name"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-01-18 12:39:19 +01:00
|
|
|
// recursion depth
|
|
|
|
[[noreturn]] static void throws_exceeded();
|
2022-06-18 06:33:15 +02:00
|
|
|
using recursive = unused_type (uint);
|
|
|
|
static _r1_type depth;
|
|
|
|
|
|
|
|
[[clang::internal_linkage]]
|
|
|
|
extern const rule<recursive> value;
|
2018-01-18 12:39:19 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<recursive> member
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2020-11-26 22:08:51 +01:00
|
|
|
name >> ws >> name_sep >> ws >> value(depth)
|
2017-09-14 20:30:06 +02:00
|
|
|
,"member"
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<recursive> object
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
(eps(depth < json::object::max_recursion_depth) | eps[throws_exceeded]) >>
|
|
|
|
|
2020-11-26 22:08:51 +01:00
|
|
|
object_begin >> -((ws >> member(depth)) % (ws >> value_sep)) >> ws >> object_end
|
2017-03-18 04:32:32 +01:00
|
|
|
,"object"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<recursive> array
|
2017-03-18 04:32:32 +01:00
|
|
|
{
|
2018-01-18 12:39:19 +01:00
|
|
|
(eps(depth < json::array::max_recursion_depth) | eps[throws_exceeded]) >>
|
|
|
|
|
2020-11-26 22:08:51 +01:00
|
|
|
array_begin >> -((ws >> value(depth)) % (ws >> value_sep)) >> ws >> array_end
|
2017-09-14 20:30:06 +02:00
|
|
|
,"array"
|
2017-03-18 04:32:32 +01:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2020-02-21 20:25:28 +01:00
|
|
|
// primary recursive rule
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<recursive> value
|
2020-02-21 20:25:28 +01:00
|
|
|
{
|
2020-05-13 09:20:57 +02:00
|
|
|
("e >> string)
|
|
|
|
| (&object_begin >> object(depth + 1))
|
|
|
|
| (&array_begin >> array(depth + 1))
|
2020-09-28 00:26:15 +02:00
|
|
|
| (&number_begin >> number)
|
2022-06-18 06:33:15 +02:00
|
|
|
| literal
|
2020-02-21 20:25:28 +01:00
|
|
|
,"value"
|
|
|
|
};
|
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2022-06-18 06:33:15 +02:00
|
|
|
static bool parse(const char *&start, const char *const &stop, gen&&, attr&&...);
|
2020-06-10 14:29:07 +02:00
|
|
|
|
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2022-06-18 06:33:15 +02:00
|
|
|
static bool parse(const char *const &start, const char *const &stop, gen&&, attr&&...);
|
2020-05-20 01:11:09 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::printer
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
using namespace ircd::spirit;
|
2020-05-20 09:00:06 +02:00
|
|
|
|
|
|
|
template<class T = unused_type,
|
|
|
|
class... A>
|
2022-06-18 06:33:15 +02:00
|
|
|
struct [[clang::internal_linkage]] rule
|
|
|
|
:karma::rule<char *, T, A...>
|
|
|
|
{
|
|
|
|
using karma::rule<char *, T, A...>::rule;
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr NUL { lit('\0') ,"nul" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
// insignificant whitespaces
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr SP { lit('\x20') ,"space" };
|
|
|
|
const expr HT { lit('\x09') ,"horizontal tab" };
|
|
|
|
const expr CR { lit('\x0D') ,"carriage return" };
|
|
|
|
const expr LF { lit('\x0A') ,"line feed" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
// whitespace skipping
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr WS { SP | HT | CR | LF ,"whitespace" };
|
|
|
|
const expr ws { *(WS) ,"whitespace monoid" };
|
|
|
|
const expr wsp { +(WS) ,"whitespace semigroup" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
// structural
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr object_begin { lit('{') ,"object begin" };
|
|
|
|
const expr object_end { lit('}') ,"object end" };
|
|
|
|
const expr array_begin { lit('[') ,"array begin" };
|
|
|
|
const expr array_end { lit(']') ,"array end" };
|
|
|
|
const expr name_sep { lit(':') ,"name separator" };
|
|
|
|
const expr value_sep { lit(',') ,"value separator" };
|
|
|
|
const expr quote { lit('"') ,"quote" };
|
|
|
|
const expr escape { lit('\\') ,"escape" };
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2019-01-22 19:52:56 +01:00
|
|
|
// literal
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<string_view> lit_true { karma::string("true") ,"literal true" };
|
|
|
|
const rule<string_view> lit_false { karma::string("false") ,"literal false" };
|
|
|
|
const rule<string_view> lit_null { karma::string("null") ,"literal null" };
|
|
|
|
const rule<string_view> boolean { lit_true | lit_false ,"boolean" };
|
|
|
|
const rule<string_view> literal { lit_true | lit_false | lit_null ,"literal" };
|
2018-03-21 00:07:22 +01:00
|
|
|
|
2019-01-22 19:52:56 +01:00
|
|
|
// number
|
2019-06-24 11:30:14 +02:00
|
|
|
const rule<string_view> number
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
|
|
|
double_
|
|
|
|
,"number"
|
|
|
|
};
|
2018-02-14 06:14:52 +01:00
|
|
|
|
2019-01-22 19:52:56 +01:00
|
|
|
// string
|
2022-06-18 06:33:15 +02:00
|
|
|
using string_context = boost::spirit::context
|
|
|
|
<
|
|
|
|
fusion::cons<const string_view &>, fusion::vector<>
|
|
|
|
>;
|
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
static void string_generate(unused_type, string_context &, bool &) noexcept;
|
|
|
|
const rule<string_view()> string
|
2018-03-28 01:15:20 +02:00
|
|
|
{
|
2020-07-03 22:20:26 +02:00
|
|
|
quote << eps[std::bind(&printer::string_generate, ph::_1, ph::_2, ph::_3)] << quote
|
2018-02-14 06:14:52 +01:00
|
|
|
,"string"
|
|
|
|
};
|
|
|
|
|
2020-05-21 06:09:15 +02:00
|
|
|
const rule<string_view()> name
|
2018-02-14 06:14:52 +01:00
|
|
|
{
|
2020-04-18 09:08:08 +02:00
|
|
|
string.alias()
|
2018-02-14 06:14:52 +01:00
|
|
|
,"name"
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2019-06-24 13:27:15 +02:00
|
|
|
// primary recursive rule
|
2022-06-18 06:33:15 +02:00
|
|
|
[[clang::internal_linkage]]
|
|
|
|
extern const rule<string_view> value;
|
2018-03-21 00:07:22 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<json::object::member> member
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
name << name_sep << value
|
2019-06-24 13:27:15 +02:00
|
|
|
,"member"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<json::object> object
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
object_begin << -(member % value_sep) << object_end
|
2019-06-24 13:27:15 +02:00
|
|
|
,"object"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<json::array> array
|
2018-03-21 00:07:22 +01:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
array_begin << -(value % value_sep) << array_end
|
2019-06-24 13:27:15 +02:00
|
|
|
,"array"
|
2018-03-21 00:07:22 +01:00
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<string_view> value
|
|
|
|
{
|
|
|
|
(&object << object)
|
|
|
|
| (&array << array)
|
|
|
|
| (&literal << literal)
|
|
|
|
| (&number << number)
|
|
|
|
| string
|
|
|
|
};
|
|
|
|
|
2020-05-20 01:11:09 +02:00
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure>
|
|
|
|
static void list_protocol(mutable_buffer &, it_a begin, const it_b &end, closure&&);
|
|
|
|
|
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2022-06-18 06:33:15 +02:00
|
|
|
static void print(mutable_buffer &out, gen&&, attr&&...);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 03:59:51 +02:00
|
|
|
decltype(ircd::json::stats)
|
|
|
|
ircd::json::stats;
|
|
|
|
|
2020-07-06 16:02:51 +02:00
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline void
|
2022-06-18 06:33:15 +02:00
|
|
|
ircd::json::printer::print(mutable_buffer &out,
|
|
|
|
gen&& g,
|
|
|
|
attr&&... a)
|
2020-07-06 16:02:51 +02:00
|
|
|
{
|
2020-07-12 02:41:00 +02:00
|
|
|
#ifdef IRCD_JSON_PRINTER_STATS
|
2020-07-06 16:02:51 +02:00
|
|
|
++stats.print_calls;
|
|
|
|
const prof::scope_cycles timer{stats.print_cycles};
|
2020-07-12 02:41:00 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-06 16:02:51 +02:00
|
|
|
if(unlikely(!ircd::generate(out, std::forward<gen>(g), std::forward<attr>(a)...)))
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"Failed to generate JSON"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it_a,
|
|
|
|
class it_b,
|
|
|
|
class closure>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline void
|
|
|
|
ircd::json::printer::list_protocol(mutable_buffer &out,
|
|
|
|
it_a it,
|
|
|
|
const it_b &end,
|
|
|
|
closure&& lambda)
|
|
|
|
{
|
|
|
|
if(likely(it != end))
|
|
|
|
{
|
|
|
|
lambda(out, *it);
|
|
|
|
for(++it; it != end; ++it)
|
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
print(out, value_sep);
|
2020-07-06 16:02:51 +02:00
|
|
|
lambda(out, *it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 06:09:15 +02:00
|
|
|
inline void
|
2020-07-03 22:20:26 +02:00
|
|
|
ircd::json::printer::string_generate(unused_type,
|
|
|
|
string_context &g,
|
|
|
|
bool &ret)
|
2020-05-21 06:09:15 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2021-04-11 01:44:51 +02:00
|
|
|
assume(ret == true);
|
2020-07-03 22:20:26 +02:00
|
|
|
assert(generator_state);
|
|
|
|
auto &state
|
|
|
|
{
|
|
|
|
*generator_state
|
|
|
|
};
|
2020-05-26 08:55:38 +02:00
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
const string_view &input
|
|
|
|
{
|
|
|
|
attr_at<0>(g)
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t output_length
|
|
|
|
{
|
|
|
|
json::string::stringify(state.out, input)
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t consumed
|
|
|
|
{
|
|
|
|
std::min(output_length, size(state.out))
|
|
|
|
};
|
|
|
|
|
|
|
|
state.consumed += consume(state.out, consumed);
|
|
|
|
state.generated += output_length;
|
2020-08-27 14:37:57 +02:00
|
|
|
ret = state.generated == state.consumed;
|
2020-05-21 06:09:15 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2020-06-24 18:24:49 +02:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline bool
|
2022-06-18 06:33:15 +02:00
|
|
|
ircd::json::parser::parse(const char *const &start_,
|
|
|
|
const char *const &stop,
|
|
|
|
gen&& g,
|
|
|
|
attr&&...a)
|
2020-06-10 14:29:07 +02:00
|
|
|
{
|
|
|
|
const char *start(start_);
|
2022-06-18 06:33:15 +02:00
|
|
|
return parser::parse(start, stop, std::forward<gen>(g), std::forward<attr>(a)...);
|
2020-06-10 14:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2020-06-24 18:24:49 +02:00
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline bool
|
2022-06-18 06:33:15 +02:00
|
|
|
ircd::json::parser::parse(const char *&start,
|
|
|
|
const char *const &stop,
|
|
|
|
gen&& g,
|
|
|
|
attr&&...a)
|
2020-06-10 14:29:07 +02:00
|
|
|
{
|
2020-07-12 02:41:00 +02:00
|
|
|
#ifdef IRCD_JSON_PARSER_STATS
|
2020-06-24 17:59:26 +02:00
|
|
|
++stats.parse_calls;
|
|
|
|
const prof::scope_cycles timer{stats.parse_cycles};
|
2020-07-12 02:41:00 +02:00
|
|
|
#endif
|
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
return ircd::parse<parse_error>(start, stop, std::forward<gen>(g), std::forward<attr>(a)...);
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:31:50 +02:00
|
|
|
/// The input covers everything from the alleged start of our alleged string
|
|
|
|
/// to the end of whatever the user provided. Returns true if successful and
|
|
|
|
/// the result string_view is set in the context attribute; the iterator is
|
|
|
|
/// advanced.
|
2020-09-27 05:02:14 +02:00
|
|
|
template<>
|
2020-08-30 12:45:52 +02:00
|
|
|
template<class iterator,
|
|
|
|
class context,
|
|
|
|
class skipper,
|
|
|
|
class attr>
|
|
|
|
inline bool
|
2022-06-18 06:33:15 +02:00
|
|
|
ircd::json::parser::custom_parser<0>::parse(iterator &__restrict__ start,
|
|
|
|
const iterator &__restrict__ stop,
|
|
|
|
context &g,
|
|
|
|
const skipper &,
|
|
|
|
attr &)
|
2020-08-30 12:45:52 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
// Clang scales between 128bit and 256bit systems when we use the 256 bit
|
|
|
|
// type (note that performance even improves on some 128 bit systems). GCC
|
|
|
|
// falls back to scalar instead, so we have to case 128bit systems on GCC.
|
|
|
|
#if defined(__AVX__) || defined(__clang__)
|
|
|
|
using block_t = u8x32;
|
|
|
|
#else
|
|
|
|
using block_t = u8x16;
|
|
|
|
#endif
|
|
|
|
|
2020-09-17 11:31:50 +02:00
|
|
|
assert(start <= stop);
|
|
|
|
const size_t input_max
|
2020-08-30 12:45:52 +02:00
|
|
|
{
|
2020-09-17 11:31:50 +02:00
|
|
|
size_t(std::distance(start, stop))
|
2020-08-30 12:45:52 +02:00
|
|
|
};
|
|
|
|
|
2020-09-17 11:31:50 +02:00
|
|
|
// The input is a priori invalid if the length is not greater than "" or
|
|
|
|
// the first character is not quote.
|
|
|
|
const bool input_valid
|
|
|
|
{
|
|
|
|
input_max >= 2 && start[0] == '"'
|
|
|
|
};
|
|
|
|
|
|
|
|
// When the input is valid subtract one for the new max length. Otherwise
|
|
|
|
// we mask this length to zero to void the remainder of this frame.
|
2020-09-09 09:27:29 +02:00
|
|
|
const u64x2 max
|
2020-08-30 12:45:52 +02:00
|
|
|
{
|
2020-09-17 11:31:50 +02:00
|
|
|
0, (input_max - 1) & boolmask<u64>(input_valid)
|
|
|
|
};
|
|
|
|
|
|
|
|
static const auto each_block
|
|
|
|
{
|
|
|
|
json::parser::string_content_block<block_t>
|
2020-09-09 09:27:29 +02:00
|
|
|
};
|
2020-08-30 12:45:52 +02:00
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
const auto count
|
2020-08-30 12:45:52 +02:00
|
|
|
{
|
2020-10-10 09:07:06 +02:00
|
|
|
simd::for_each<block_t>(start + 1, max, each_block)
|
2020-09-09 09:27:29 +02:00
|
|
|
};
|
2020-08-30 12:45:52 +02:00
|
|
|
|
2020-09-17 11:31:50 +02:00
|
|
|
const bool ok
|
|
|
|
{
|
|
|
|
count[0] == 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set the result in the context attribute. This covers the string content
|
|
|
|
// without surrounding quotes.
|
2020-08-30 12:45:52 +02:00
|
|
|
attr_at<0>(g) = string_view
|
|
|
|
{
|
2020-09-17 11:31:50 +02:00
|
|
|
start + ok, count[1] & boolmask<u64>(ok)
|
2020-08-30 12:45:52 +02:00
|
|
|
};
|
|
|
|
|
2020-09-17 11:31:50 +02:00
|
|
|
// Advance the iterator the length of the full string including quotes
|
|
|
|
// iff this parser was successful.
|
|
|
|
start += (1 + count[1] + 1) & boolmask<u64>(ok);
|
|
|
|
|
|
|
|
return ok;
|
2020-08-30 12:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class block_t>
|
|
|
|
inline ircd::u64x2
|
|
|
|
ircd::json::parser::string_content_block(const block_t block,
|
|
|
|
const block_t block_mask)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(block_mask[0] == 0xff);
|
|
|
|
const block_t is_esc
|
|
|
|
(
|
|
|
|
block == '\\'
|
|
|
|
);
|
|
|
|
|
|
|
|
const block_t is_quote
|
|
|
|
(
|
|
|
|
block == '"'
|
|
|
|
);
|
|
|
|
|
|
|
|
const block_t is_ctrl
|
|
|
|
(
|
|
|
|
block < 0x20
|
|
|
|
);
|
|
|
|
|
|
|
|
const block_t is_special
|
|
|
|
{
|
|
|
|
is_esc | is_quote | is_ctrl
|
|
|
|
};
|
|
|
|
|
2020-09-28 08:36:15 +02:00
|
|
|
if(likely(simd::all(~is_special)))
|
2020-08-30 12:45:52 +02:00
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
0, sizeof(block)
|
2020-08-30 12:45:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const u64 regular_prefix_count
|
|
|
|
{
|
|
|
|
simd::lzcnt(is_special | ~block_mask) / 8
|
|
|
|
};
|
|
|
|
|
|
|
|
if(likely(regular_prefix_count))
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
0, regular_prefix_count
|
2020-08-30 12:45:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const u64 err
|
|
|
|
{
|
|
|
|
popmask<u64>(is_quote[0])
|
|
|
|
| boolmask<u64>(is_ctrl[0])
|
|
|
|
| boolmask<u64>(is_esc[0] & ~block_mask[1])
|
|
|
|
};
|
|
|
|
|
|
|
|
const u64 add
|
|
|
|
{
|
|
|
|
1UL + popmask<u64>(is_esc[0] & (is_quote[1] | is_esc[1]) & block_mask[1])
|
|
|
|
};
|
|
|
|
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
err, add & boolmask<u64>(err == 0)
|
2020-08-30 12:45:52 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
[[gnu::noinline, gnu::cold, noreturn]]
|
2018-01-18 12:39:19 +01:00
|
|
|
void
|
2020-05-20 01:11:09 +02:00
|
|
|
ircd::json::parser::throws_exceeded()
|
2018-01-18 12:39:19 +01:00
|
|
|
{
|
|
|
|
throw recursion_limit
|
|
|
|
{
|
|
|
|
"Maximum recursion depth exceeded"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-27 21:01:15 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/tool.h
|
|
|
|
//
|
|
|
|
|
2022-06-11 23:46:20 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
static thread_local std::array<member, iov::max_size> member_buffer;
|
|
|
|
static thread_local std::array<object::member, iov::max_size> object_member_buffer;
|
|
|
|
static thread_local std::array<string_view, iov::max_size> value_buffer;
|
|
|
|
}
|
|
|
|
|
2020-03-19 03:14:02 +01:00
|
|
|
ircd::json::strung
|
2020-10-27 07:57:16 +01:00
|
|
|
ircd::json::replace(const object &s,
|
2020-03-19 03:14:02 +01:00
|
|
|
const json::members &r)
|
|
|
|
{
|
|
|
|
static const auto in
|
|
|
|
{
|
|
|
|
[](const json::members &r, const object::member &m)
|
|
|
|
{
|
|
|
|
return std::any_of(begin(r), end(r), [&m]
|
|
|
|
(const json::member &r)
|
|
|
|
{
|
|
|
|
return string_view{r.first} == m.first;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
if(unlikely(!empty(s) && type(s) != type::OBJECT))
|
2020-03-19 03:14:02 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot replace member into JSON of type %s",
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr {0};
|
2022-06-11 23:46:20 +02:00
|
|
|
auto &mb(member_buffer);
|
2020-03-19 03:14:02 +01:00
|
|
|
for(const object::member &m : object{s})
|
|
|
|
if(!in(r, m))
|
|
|
|
mb.at(mctr++) = member{m};
|
|
|
|
|
|
|
|
for(const json::member &m : r)
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-19 00:27:27 +01:00
|
|
|
ircd::json::strung
|
2020-10-27 07:57:16 +01:00
|
|
|
ircd::json::replace(const object &s,
|
2020-03-19 00:27:27 +01:00
|
|
|
const json::member &m_)
|
|
|
|
{
|
2022-05-26 21:00:07 +02:00
|
|
|
if(unlikely(!empty(s) && type(s) != type::OBJECT))
|
2020-03-19 00:27:27 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot replace member into JSON of type %s",
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr {0};
|
2022-06-11 23:46:20 +02:00
|
|
|
auto &mb(member_buffer);
|
2020-03-19 00:27:27 +01:00
|
|
|
for(const object::member &m : object{s})
|
|
|
|
if(m.first != string_view{m_.first})
|
|
|
|
mb.at(mctr++) = member{m};
|
|
|
|
|
|
|
|
mb.at(mctr++) = m_;
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-27 21:01:15 +01:00
|
|
|
ircd::json::strung
|
2020-10-27 07:57:16 +01:00
|
|
|
ircd::json::insert(const object &s,
|
2019-02-27 21:01:15 +01:00
|
|
|
const json::member &m)
|
|
|
|
{
|
2022-05-26 21:00:07 +02:00
|
|
|
if(unlikely(!empty(s) && type(s) != type::OBJECT))
|
2019-02-27 21:01:15 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot insert member into JSON of type %s",
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr {0};
|
2022-06-11 23:46:20 +02:00
|
|
|
auto &mb(member_buffer);
|
2019-02-27 21:01:15 +01:00
|
|
|
for(const object::member &m : object{s})
|
|
|
|
mb.at(mctr++) = member{m};
|
|
|
|
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::strung
|
2020-10-27 07:57:16 +01:00
|
|
|
ircd::json::remove(const object &s,
|
2019-02-27 21:01:15 +01:00
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
if(empty(s))
|
|
|
|
return s;
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
if(unlikely(type(s) != type::OBJECT))
|
2019-02-27 21:01:15 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot remove object member '%s' from JSON of type %s",
|
|
|
|
key,
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr {0};
|
2022-06-11 23:46:20 +02:00
|
|
|
auto &mb(object_member_buffer);
|
2019-02-27 21:01:15 +01:00
|
|
|
for(const object::member &m : object{s})
|
|
|
|
if(m.first != key)
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::strung
|
2020-10-27 07:57:16 +01:00
|
|
|
ircd::json::remove(const object &s,
|
2019-02-27 21:01:15 +01:00
|
|
|
const size_t &idx)
|
|
|
|
{
|
|
|
|
if(empty(s))
|
|
|
|
return s;
|
|
|
|
|
2022-05-26 21:00:07 +02:00
|
|
|
if(unlikely(type(s) != type::ARRAY))
|
2019-02-27 21:01:15 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot remove array element [%zu] from JSON of type %s",
|
|
|
|
idx,
|
|
|
|
reflect(type(s))
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t mctr{0}, i{0};
|
2022-06-11 23:46:20 +02:00
|
|
|
auto &mb(value_buffer);
|
2019-02-27 21:01:15 +01:00
|
|
|
for(const string_view &m : array{s})
|
|
|
|
if(i++ != idx)
|
|
|
|
mb.at(mctr++) = m;
|
|
|
|
|
|
|
|
return strung
|
|
|
|
{
|
|
|
|
mb.data(), mb.data() + mctr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-28 00:23:07 +01:00
|
|
|
void
|
|
|
|
ircd::json::merge(stack::object &out,
|
|
|
|
const vector &v)
|
|
|
|
{
|
|
|
|
struct val
|
|
|
|
{
|
2019-02-28 19:17:07 +01:00
|
|
|
//TODO: optimize with std::pmr::monotonic_buffer_resource et al
|
2019-02-28 00:23:07 +01:00
|
|
|
std::map<string_view, val, std::less<>> o;
|
|
|
|
std::vector<string_view> a;
|
|
|
|
string_view v;
|
|
|
|
|
|
|
|
void _merge_object(const json::object &o)
|
|
|
|
{
|
|
|
|
for(const auto &m : o)
|
|
|
|
{
|
|
|
|
val &v(this->o[m.first]);
|
|
|
|
v.merge(m.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _merge_array(const json::array &a)
|
|
|
|
{
|
|
|
|
for(const auto &v : a)
|
|
|
|
this->a.emplace_back(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(const string_view &v)
|
|
|
|
{
|
2020-06-12 01:49:12 +02:00
|
|
|
switch(json::type(v))
|
|
|
|
{
|
|
|
|
case json::OBJECT: _merge_object(v); break;
|
|
|
|
case json::ARRAY: _merge_array(v); break;
|
|
|
|
default: this->v = v; break;
|
|
|
|
}
|
2019-02-28 00:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void _compose_object(json::stack &out, json::stack::object &object) const
|
|
|
|
{
|
|
|
|
for(const auto &m : o)
|
|
|
|
{
|
|
|
|
json::stack::member member{object, m.first};
|
|
|
|
m.second.compose(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _compose_object(json::stack &out, json::stack::member &member) const
|
|
|
|
{
|
|
|
|
json::stack::object object{member};
|
|
|
|
_compose_object(out, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _compose_object(json::stack &out) const
|
|
|
|
{
|
|
|
|
json::stack::chase c{out, true};
|
|
|
|
if(c.m)
|
|
|
|
_compose_object(out, *c.m);
|
|
|
|
else if(c.o)
|
|
|
|
_compose_object(out, *c.o);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _compose_array(json::stack &out) const
|
|
|
|
{
|
|
|
|
json::stack::array array{out};
|
|
|
|
for(const auto &v : a)
|
|
|
|
array.append(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _compose_value(json::stack &out) const
|
|
|
|
{
|
|
|
|
json::stack::chase c{out, true};
|
|
|
|
if(c.a)
|
|
|
|
c.a->append(v);
|
|
|
|
else if(c.m)
|
|
|
|
c.m->append(v);
|
|
|
|
else
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compose(json::stack &out) const
|
|
|
|
{
|
|
|
|
if(!o.empty())
|
|
|
|
_compose_object(out);
|
|
|
|
else if(!a.empty())
|
|
|
|
_compose_array(out);
|
|
|
|
else if(!v.empty())
|
|
|
|
_compose_value(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
val() = default;
|
|
|
|
val(const string_view &v)
|
|
|
|
{
|
|
|
|
merge(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
val top;
|
|
|
|
for(const auto &o : v)
|
|
|
|
top.merge(o);
|
|
|
|
|
|
|
|
assert(out.s);
|
|
|
|
top.compose(*out.s);
|
|
|
|
}
|
|
|
|
|
2022-07-14 00:01:53 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-01-22 19:52:56 +01:00
|
|
|
// json/stack.h
|
2018-03-18 00:55:32 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
ircd::json::stack::stack(const mutable_buffer &buf,
|
2018-05-08 08:48:31 +02:00
|
|
|
flush_callback flusher,
|
|
|
|
const size_t &hiwat,
|
|
|
|
const size_t &lowat)
|
2018-03-18 00:55:32 +01:00
|
|
|
:buf{buf}
|
|
|
|
,flusher{std::move(flusher)}
|
2018-05-08 08:48:31 +02:00
|
|
|
,hiwat{hiwat}
|
|
|
|
,lowat{lowat}
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::stack(stack &&other)
|
|
|
|
noexcept
|
|
|
|
:buf{std::move(other.buf)}
|
|
|
|
,flusher{std::move(other.flusher)}
|
2018-04-14 00:15:45 +02:00
|
|
|
,eptr{std::move(other.eptr)}
|
2019-02-24 20:20:26 +01:00
|
|
|
,cp{std::move(other.cp)}
|
2019-12-02 06:16:49 +01:00
|
|
|
,appended{std::move(other.appended)}
|
|
|
|
,flushed{std::move(other.flushed)}
|
2019-12-02 06:37:22 +01:00
|
|
|
,level{std::move(other.level)}
|
2018-05-08 08:48:31 +02:00
|
|
|
,hiwat{std::move(other.hiwat)}
|
|
|
|
,lowat{std::move(other.lowat)}
|
2018-04-10 22:33:26 +02:00
|
|
|
,co{std::move(other.co)}
|
|
|
|
,ca{std::move(other.ca)}
|
|
|
|
{
|
2019-02-24 20:20:26 +01:00
|
|
|
other.cp = nullptr;
|
2018-04-10 22:33:26 +02:00
|
|
|
other.co = nullptr;
|
|
|
|
other.ca = nullptr;
|
2019-02-27 00:27:03 +01:00
|
|
|
|
|
|
|
if(cp)
|
|
|
|
{
|
|
|
|
assert(cp->s == &other);
|
|
|
|
cp->s = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(co)
|
|
|
|
{
|
|
|
|
assert(co->s == &other);
|
|
|
|
co->s = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ca)
|
|
|
|
{
|
|
|
|
assert(ca->s == &other);
|
|
|
|
ca->s = this;
|
|
|
|
}
|
2018-04-10 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::~stack()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-02-24 22:48:44 +01:00
|
|
|
assert(closed());
|
|
|
|
if(buf.consumed())
|
|
|
|
flush(true);
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(clean() || done());
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:16:37 +01:00
|
|
|
void
|
|
|
|
ircd::json::stack::append(const char &c)
|
|
|
|
noexcept
|
|
|
|
{
|
2020-07-12 00:28:24 +02:00
|
|
|
append(1, [&c]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
noexcept
|
2019-02-07 17:16:37 +01:00
|
|
|
{
|
|
|
|
buf[0] = c;
|
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
void
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::append(const string_view &s)
|
2018-12-24 21:06:57 +01:00
|
|
|
noexcept
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
append(s.size(), [&s]
|
|
|
|
(const mutable_buffer &buf)
|
2020-07-12 00:28:24 +02:00
|
|
|
noexcept
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
assert(ircd::size(buf) >= s.size());
|
|
|
|
return ircd::copy(buf, s);
|
2018-03-18 00:55:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
void
|
2023-02-19 06:21:14 +01:00
|
|
|
ircd::json::stack::append(const size_t expect,
|
2018-04-11 01:59:08 +02:00
|
|
|
const window_buffer::closure &closure)
|
2018-12-24 21:06:57 +01:00
|
|
|
noexcept try
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
if(!expect || failed())
|
2018-04-14 00:15:45 +02:00
|
|
|
return;
|
|
|
|
|
2019-12-02 06:37:54 +01:00
|
|
|
// Minimum bytes we keep available all times to allow the JSON to close
|
|
|
|
// correctly without complication on the user's stack unwind; hinted by
|
|
|
|
// the recursion level.
|
|
|
|
const size_t buf_min
|
|
|
|
{
|
|
|
|
level + 8
|
|
|
|
};
|
|
|
|
|
|
|
|
// Calculated buffer bytes required.
|
|
|
|
const size_t buf_req
|
|
|
|
{
|
|
|
|
expect + buf_min
|
|
|
|
};
|
|
|
|
|
2019-09-23 21:58:02 +02:00
|
|
|
// Since all appends are atomic, we need to have buffer available to print
|
|
|
|
// the JSON without having to flush while doing so. If we're low on buffer,
|
|
|
|
// this branch triggers a flush. Afterward, if there is still not enough
|
|
|
|
// buffer that's an error so the user needs to flush enough when called.
|
2019-12-02 06:37:54 +01:00
|
|
|
if(buf_req > buf.remaining())
|
2018-04-11 02:13:40 +02:00
|
|
|
{
|
2019-09-10 04:01:50 +02:00
|
|
|
if(unlikely(!flusher))
|
|
|
|
throw print_panic
|
|
|
|
{
|
|
|
|
"Insufficient buffer. I need %zu more bytes; you only have %zu left (of %zu).",
|
2019-12-02 06:37:54 +01:00
|
|
|
buf_req,
|
2019-09-10 04:01:50 +02:00
|
|
|
buf.remaining(),
|
|
|
|
size(buf.base)
|
|
|
|
};
|
2018-04-11 02:13:40 +02:00
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(!flush(true))
|
2018-04-14 00:15:45 +02:00
|
|
|
return;
|
|
|
|
|
2019-12-02 06:37:54 +01:00
|
|
|
if(unlikely(buf_req > buf.remaining()))
|
2019-09-10 04:01:50 +02:00
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"Insufficient flush. I still need %zu more bytes to buffer.",
|
2019-12-02 06:37:54 +01:00
|
|
|
buf_req - buf.remaining(),
|
2019-09-10 04:01:50 +02:00
|
|
|
};
|
2018-04-11 02:13:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 21:58:02 +02:00
|
|
|
// Print the JSON to the buffer and advance the window pointer
|
2019-09-24 03:07:18 +02:00
|
|
|
const const_buffer appended
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
buf([&expect, &closure](const mutable_buffer &buf)
|
2019-09-23 21:58:02 +02:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
const size_t appended
|
|
|
|
{
|
|
|
|
closure(buf)
|
|
|
|
};
|
2019-09-23 21:58:02 +02:00
|
|
|
|
2019-09-24 03:07:18 +02:00
|
|
|
assert(appended <= size(buf));
|
|
|
|
assert(appended == expect);
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(buf), appended
|
|
|
|
};
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
this->appended += expect;
|
|
|
|
assert(size(appended) >= expect);
|
|
|
|
assert(this->appended >= size(appended));
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2019-09-23 21:58:02 +02:00
|
|
|
// Conditions to courtesy flush after a sufficiently large dump; when
|
|
|
|
// there's no buffer remaining we'll inevitably have to flush; the call
|
|
|
|
// is force=true so the flusher must accomplish something.
|
2018-04-11 02:13:40 +02:00
|
|
|
if(!buf.remaining())
|
2018-05-08 08:48:31 +02:00
|
|
|
flush(true);
|
2019-09-23 21:58:02 +02:00
|
|
|
|
|
|
|
// The high-watermark feature triggers a flush when the buffer has exceeded
|
|
|
|
// the threshold from accumulated writes; force is not set to true.
|
2018-05-08 08:48:31 +02:00
|
|
|
else if(buf.consumed() >= hiwat)
|
2018-04-11 02:13:40 +02:00
|
|
|
flush();
|
|
|
|
}
|
2018-04-14 00:15:45 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(!this->eptr);
|
|
|
|
this->eptr = std::current_exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::rethrow_exception()
|
|
|
|
{
|
|
|
|
if(unlikely(eptr))
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
2018-04-11 02:13:40 +02:00
|
|
|
|
|
|
|
bool
|
2023-02-19 06:21:14 +01:00
|
|
|
ircd::json::stack::flush(const bool force)
|
2018-12-24 21:06:57 +01:00
|
|
|
noexcept try
|
2018-04-11 02:13:40 +02:00
|
|
|
{
|
|
|
|
if(!flusher)
|
|
|
|
return false;
|
|
|
|
|
2019-03-28 23:36:33 +01:00
|
|
|
if(unlikely(failed()))
|
2018-04-14 00:15:45 +02:00
|
|
|
return false;
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(!force && buf.consumed() < lowat)
|
|
|
|
return false;
|
|
|
|
|
2019-02-24 20:20:26 +01:00
|
|
|
if(!force && cp)
|
|
|
|
return false;
|
|
|
|
|
2019-09-24 03:07:18 +02:00
|
|
|
if(unlikely(cp))
|
2019-03-28 23:40:25 +01:00
|
|
|
{
|
2019-03-30 22:42:10 +01:00
|
|
|
const size_t invalidated
|
|
|
|
{
|
|
|
|
invalidate_checkpoints()
|
|
|
|
};
|
|
|
|
|
2020-09-19 07:08:39 +02:00
|
|
|
if(invalidated)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"Flushing json::stack(%p) bytes:%zu level:%zu checkpoints:%zu",
|
|
|
|
this,
|
|
|
|
size(buf.completed()),
|
|
|
|
level,
|
|
|
|
invalidated,
|
|
|
|
};
|
2019-03-28 23:40:25 +01:00
|
|
|
}
|
2019-02-24 20:20:26 +01:00
|
|
|
|
2018-04-11 02:13:40 +02:00
|
|
|
// The user returns the portion of the buffer they were able to flush
|
|
|
|
// rather than forcing them to wait on their sink to flush the whole
|
|
|
|
// thing, they can continue with us for a little while more.
|
|
|
|
const const_buffer flushed
|
|
|
|
{
|
|
|
|
flusher(buf.completed())
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(data(flushed) == data(buf.completed())); // Can only flush front sry
|
2019-09-24 03:07:18 +02:00
|
|
|
this->flushed += size(flushed);
|
2018-04-11 02:13:40 +02:00
|
|
|
buf.shift(size(flushed));
|
|
|
|
return true;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
2018-04-14 00:15:45 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(!this->eptr);
|
|
|
|
this->eptr = std::current_exception();
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2019-03-30 22:42:10 +01:00
|
|
|
size_t
|
|
|
|
ircd::json::stack::invalidate_checkpoints()
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-03-30 22:42:10 +01:00
|
|
|
{
|
|
|
|
size_t ret(0);
|
2020-09-19 07:08:39 +02:00
|
|
|
for(auto cp(this->cp); cp; cp = cp->pc)
|
|
|
|
{
|
|
|
|
ret += cp->s != nullptr;
|
2019-03-30 22:42:10 +01:00
|
|
|
cp->s = nullptr;
|
2020-09-19 07:08:39 +02:00
|
|
|
}
|
2019-03-30 22:42:10 +01:00
|
|
|
|
|
|
|
this->cp = nullptr;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
void
|
|
|
|
ircd::json::stack::clear()
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
const size_t rewound
|
|
|
|
{
|
|
|
|
rewind(buf.consumed())
|
|
|
|
};
|
|
|
|
|
2018-04-14 00:15:45 +02:00
|
|
|
this->eptr = std::exception_ptr{};
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-27 02:01:07 +01:00
|
|
|
size_t
|
2023-02-19 06:21:14 +01:00
|
|
|
ircd::json::stack::rewind(const size_t bytes)
|
|
|
|
noexcept
|
2019-01-27 02:01:07 +01:00
|
|
|
{
|
2019-09-24 03:07:18 +02:00
|
|
|
const size_t before
|
|
|
|
{
|
|
|
|
buf.consumed()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(appended >= before);
|
|
|
|
const size_t &amount
|
|
|
|
{
|
|
|
|
std::min(bytes, before)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(appended >= amount);
|
|
|
|
const size_t after
|
|
|
|
{
|
|
|
|
size(buf.rewind(amount))
|
|
|
|
};
|
|
|
|
|
2019-01-27 02:01:07 +01:00
|
|
|
assert(before >= after);
|
|
|
|
assert(before - after == amount);
|
2019-09-24 03:07:18 +02:00
|
|
|
appended -= amount;
|
|
|
|
assert(appended >= after);
|
2019-01-27 02:01:07 +01:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
//
|
|
|
|
// object
|
|
|
|
//
|
|
|
|
|
2019-01-08 19:27:23 +01:00
|
|
|
ircd::json::stack::object &
|
|
|
|
ircd::json::stack::object::top(stack &s)
|
|
|
|
{
|
|
|
|
const chase t{s, true};
|
|
|
|
if(unlikely(!t.o))
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Top of stack is not of type object. (o:%b a:%b m:%b)",
|
|
|
|
bool(t.o),
|
|
|
|
bool(t.a),
|
|
|
|
bool(t.m),
|
|
|
|
};
|
|
|
|
|
|
|
|
return *t.o;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::stack::object &
|
|
|
|
ircd::json::stack::object::top(const stack &s)
|
|
|
|
{
|
|
|
|
const const_chase t{s, true};
|
|
|
|
if(unlikely(!t.o))
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Top of stack is not of type object. (o:%b a:%b m:%b)",
|
|
|
|
bool(t.o),
|
|
|
|
bool(t.a),
|
|
|
|
bool(t.m),
|
|
|
|
};
|
|
|
|
|
|
|
|
return *t.o;
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::object::object(object &&other)
|
|
|
|
noexcept
|
2019-01-08 23:49:04 +01:00
|
|
|
:m{std::move(other.m)}
|
|
|
|
,s{std::move(other.s)}
|
2018-04-10 22:33:26 +02:00
|
|
|
,pm{std::move(other.pm)}
|
|
|
|
,pa{std::move(other.pa)}
|
|
|
|
,cm{std::move(other.cm)}
|
|
|
|
,mc{std::move(other.mc)}
|
|
|
|
{
|
|
|
|
other.s = nullptr;
|
2019-02-27 00:27:03 +01:00
|
|
|
|
|
|
|
if(s)
|
|
|
|
{
|
|
|
|
assert(s->co == &other);
|
|
|
|
s->co = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pm)
|
|
|
|
{
|
|
|
|
assert(pm->co == &other);
|
|
|
|
pm->co = this;
|
|
|
|
}
|
|
|
|
else if(pa)
|
|
|
|
{
|
|
|
|
assert(pa->co == &other);
|
|
|
|
pa->co = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cm)
|
|
|
|
{
|
|
|
|
assert(cm->po == &other);
|
|
|
|
cm->po = this;
|
|
|
|
}
|
2018-04-10 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::object::object(stack &s)
|
|
|
|
:s{&s}
|
|
|
|
{
|
2019-01-08 19:27:23 +01:00
|
|
|
const chase t{s, true};
|
|
|
|
if(t.a)
|
|
|
|
{
|
|
|
|
new (this) object{*t.a};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(t.m)
|
|
|
|
{
|
|
|
|
new (this) object{*t.m};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(t.o)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(s.clean());
|
|
|
|
s.co = this;
|
2019-07-08 08:36:45 +02:00
|
|
|
s.append('{');
|
2019-12-02 06:37:22 +01:00
|
|
|
s.level++;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 23:49:04 +01:00
|
|
|
ircd::json::stack::object::object(stack &s,
|
2019-02-24 22:48:44 +01:00
|
|
|
const string_view &name)
|
2019-01-08 23:49:04 +01:00
|
|
|
:object{object::top(s), name}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::object::object(object &po,
|
|
|
|
const string_view &name)
|
|
|
|
:m{po, name}
|
|
|
|
,s{po.s}
|
|
|
|
,pm{&m}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
|
|
|
s->rethrow_exception();
|
|
|
|
|
|
|
|
assert(pm->co == nullptr);
|
|
|
|
assert(pm->ca == nullptr);
|
|
|
|
pm->co = this;
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('{');
|
2019-01-08 23:49:04 +01:00
|
|
|
pm->vc |= true;
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level++;
|
2019-01-08 23:49:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::object::object(member &pm)
|
|
|
|
:s{pm.s}
|
|
|
|
,pm{&pm}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pm.co == nullptr);
|
|
|
|
assert(pm.ca == nullptr);
|
|
|
|
pm.co = this;
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('{');
|
2019-01-07 22:56:47 +01:00
|
|
|
pm.vc |= true;
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level++;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::object::object(array &pa)
|
|
|
|
:s{pa.s}
|
|
|
|
,pa{&pa}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pa.co == nullptr);
|
|
|
|
assert(pa.ca == nullptr);
|
|
|
|
pa.co = this;
|
|
|
|
|
|
|
|
if(pa.vc)
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append(',');
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('{');
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level++;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 00:22:37 +01:00
|
|
|
void
|
|
|
|
ircd::json::stack::object::append(const json::object &object)
|
|
|
|
{
|
|
|
|
for(const auto &kv : object)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
*this, kv.first, kv.second
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:53:02 +01:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
2019-12-05 03:03:13 +01:00
|
|
|
__attribute__((optimize("no-lifetime-dse")))
|
2020-02-07 21:53:02 +01:00
|
|
|
#endif
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::object::~object()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-04-10 22:33:26 +02:00
|
|
|
if(!s)
|
2019-12-05 01:04:29 +01:00
|
|
|
{
|
|
|
|
assert(!m.s);
|
2018-04-10 22:33:26 +02:00
|
|
|
return; // std::move()'ed away
|
2019-12-05 01:04:29 +01:00
|
|
|
}
|
2018-04-10 22:33:26 +02:00
|
|
|
|
2018-04-10 23:17:56 +02:00
|
|
|
const unwind _{[this]
|
|
|
|
{
|
|
|
|
// Allows ~dtor to be called to close the JSON manually
|
|
|
|
s = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(cm == nullptr);
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('}');
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level--;
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pm) // branch taken if member of object
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pa == nullptr);
|
|
|
|
assert(pm->ca == nullptr);
|
|
|
|
assert(pm->co == this);
|
2018-04-11 00:11:44 +02:00
|
|
|
pm->co = nullptr;
|
2018-03-18 00:55:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pa) // branch taken if value in array
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pm == nullptr);
|
2018-04-04 00:58:47 +02:00
|
|
|
assert(pa->ca == nullptr);
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pa->co == this);
|
|
|
|
pa->vc++;
|
2018-04-04 00:58:47 +02:00
|
|
|
pa->co = nullptr;
|
2018-03-18 00:55:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:48:44 +01:00
|
|
|
// branch taken if top of stack::
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(s->co == this);
|
|
|
|
assert(s->ca == nullptr);
|
|
|
|
assert(pm == nullptr && pa == nullptr);
|
|
|
|
s->co = nullptr;
|
2019-02-24 22:48:44 +01:00
|
|
|
assert(s->closed());
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// array
|
|
|
|
//
|
|
|
|
|
2019-01-08 19:27:23 +01:00
|
|
|
ircd::json::stack::array &
|
|
|
|
ircd::json::stack::array::top(stack &s)
|
|
|
|
{
|
|
|
|
const chase t{s, true};
|
|
|
|
|
|
|
|
if(unlikely(!t.a))
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Top of stack is not of type array. (o:%b a:%b m:%b)",
|
|
|
|
bool(t.o),
|
|
|
|
bool(t.a),
|
|
|
|
bool(t.m),
|
|
|
|
};
|
|
|
|
|
|
|
|
return *t.a;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::stack::array &
|
|
|
|
ircd::json::stack::array::top(const stack &s)
|
|
|
|
{
|
|
|
|
const const_chase t{s, true};
|
|
|
|
|
|
|
|
if(unlikely(!t.a))
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Top of stack is not of type array. (o:%b a:%b m:%b)",
|
|
|
|
bool(t.o),
|
|
|
|
bool(t.a),
|
|
|
|
bool(t.m),
|
|
|
|
};
|
|
|
|
|
|
|
|
return *t.a;
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::array::array(array &&other)
|
|
|
|
noexcept
|
2019-01-08 23:49:04 +01:00
|
|
|
:m{std::move(other.m)}
|
|
|
|
,s{std::move(other.s)}
|
2018-04-10 22:33:26 +02:00
|
|
|
,pm{std::move(other.pm)}
|
|
|
|
,pa{std::move(other.pa)}
|
|
|
|
,co{std::move(other.co)}
|
|
|
|
,ca{std::move(other.ca)}
|
|
|
|
,vc{std::move(other.vc)}
|
|
|
|
{
|
|
|
|
other.s = nullptr;
|
2019-02-27 00:27:03 +01:00
|
|
|
|
|
|
|
if(s)
|
|
|
|
{
|
|
|
|
assert(s->ca == &other);
|
|
|
|
s->ca = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pm)
|
|
|
|
{
|
|
|
|
assert(pm->ca == &other);
|
|
|
|
pm->ca = this;
|
|
|
|
}
|
|
|
|
else if(pa)
|
|
|
|
{
|
|
|
|
assert(pa->ca == &other);
|
|
|
|
pa->ca = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(co)
|
|
|
|
{
|
|
|
|
assert(co->pa == &other);
|
|
|
|
co->pa = this;
|
|
|
|
}
|
|
|
|
else if(ca)
|
|
|
|
{
|
|
|
|
assert(ca->pa == &other);
|
|
|
|
ca->pa = this;
|
|
|
|
}
|
2018-04-10 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::array::array(stack &s)
|
|
|
|
:s{&s}
|
|
|
|
{
|
2019-01-08 19:27:23 +01:00
|
|
|
const chase t{s, true};
|
|
|
|
|
|
|
|
if(t.a)
|
|
|
|
{
|
|
|
|
new (this) array{*t.a};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(t.m)
|
|
|
|
{
|
|
|
|
new (this) array{*t.m};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(t.o)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(s.clean());
|
|
|
|
s.ca = this;
|
2019-07-08 08:36:45 +02:00
|
|
|
s.append('[');
|
2019-12-02 06:37:22 +01:00
|
|
|
s.level++;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 23:49:04 +01:00
|
|
|
ircd::json::stack::array::array(stack &s,
|
|
|
|
const string_view &name)
|
|
|
|
:array{object::top(s), name}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::array::array(object &po,
|
|
|
|
const string_view &name)
|
|
|
|
:m{po, name}
|
|
|
|
,s{po.s}
|
|
|
|
,pm{&m}
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2019-01-08 23:49:04 +01:00
|
|
|
assert(pm->co == nullptr);
|
|
|
|
assert(pm->ca == nullptr);
|
|
|
|
pm->ca = this;
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('[');
|
2019-01-08 23:49:04 +01:00
|
|
|
pm->vc |= true;
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level++;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::array::array(array &pa)
|
|
|
|
:s{pa.s}
|
|
|
|
,pa{&pa}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
2018-04-14 00:15:45 +02:00
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(pa.co == nullptr);
|
|
|
|
assert(pa.ca == nullptr);
|
|
|
|
pa.ca = this;
|
|
|
|
|
|
|
|
if(pa.vc)
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append(',');
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('[');
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level++;
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 23:49:04 +01:00
|
|
|
ircd::json::stack::array::array(member &pm)
|
|
|
|
:s{pm.s}
|
|
|
|
,pm{&pm}
|
|
|
|
{
|
|
|
|
assert(s->opened());
|
|
|
|
s->rethrow_exception();
|
|
|
|
|
|
|
|
assert(pm.co == nullptr);
|
|
|
|
assert(pm.ca == nullptr);
|
|
|
|
pm.ca = this;
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append('[');
|
2019-01-08 23:49:04 +01:00
|
|
|
pm.vc |= true;
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level++;
|
2019-01-08 23:49:04 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:53:02 +01:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
2019-12-05 03:03:13 +01:00
|
|
|
__attribute__((optimize("no-lifetime-dse")))
|
2020-02-07 21:53:02 +01:00
|
|
|
#endif
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::array::~array()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-04-10 22:33:26 +02:00
|
|
|
if(!s)
|
2019-12-05 01:04:29 +01:00
|
|
|
{
|
|
|
|
assert(!m.s);
|
2018-04-10 22:33:26 +02:00
|
|
|
return; // std::move()'ed away
|
2019-12-05 01:04:29 +01:00
|
|
|
}
|
2018-04-10 22:33:26 +02:00
|
|
|
|
2018-04-10 23:17:56 +02:00
|
|
|
const unwind _{[this]
|
|
|
|
{
|
|
|
|
// Allows ~dtor to be called to close the JSON manually
|
|
|
|
s = nullptr;
|
|
|
|
}};
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(co == nullptr);
|
|
|
|
assert(ca == nullptr);
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append(']');
|
2019-12-02 06:37:22 +01:00
|
|
|
s->level--;
|
2018-03-18 00:55:32 +01:00
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pm) // branch taken if member of object
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pa == nullptr);
|
|
|
|
assert(pm->ca == this);
|
|
|
|
assert(pm->co == nullptr);
|
|
|
|
pm->ca = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:48:31 +02:00
|
|
|
if(pa) // branch taken if value in array
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
assert(pm == nullptr);
|
|
|
|
assert(pa->ca == this);
|
|
|
|
assert(pa->co == nullptr);
|
|
|
|
pa->vc++;
|
|
|
|
pa->ca = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:48:44 +01:00
|
|
|
// branch taken if top of stack::
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(s->ca == this);
|
|
|
|
assert(s->co == nullptr);
|
|
|
|
assert(pm == nullptr && pa == nullptr);
|
|
|
|
s->ca = nullptr;
|
2019-02-24 22:48:44 +01:00
|
|
|
assert(s->closed());
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::array::append(const json::value &value)
|
|
|
|
{
|
|
|
|
assert(s);
|
|
|
|
_pre_append();
|
2020-09-07 21:37:54 +02:00
|
|
|
const unwind_nominal post{[this]
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
_post_append();
|
|
|
|
}};
|
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(serialized(value), [&value]
|
|
|
|
(mutable_buffer buf)
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
return size(stringify(buf, value));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::array::_pre_append()
|
|
|
|
{
|
|
|
|
if(vc)
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append(',');
|
2019-01-04 00:54:53 +01:00
|
|
|
|
|
|
|
s->rethrow_exception();
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// member
|
|
|
|
//
|
|
|
|
|
2019-01-08 19:27:23 +01:00
|
|
|
ircd::json::stack::member &
|
|
|
|
ircd::json::stack::member::top(stack &s)
|
|
|
|
{
|
|
|
|
const chase t{s, true};
|
|
|
|
|
|
|
|
if(unlikely(!t.m))
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Top of stack is not of type member. (o:%b a:%b m:%b)",
|
|
|
|
bool(t.o),
|
|
|
|
bool(t.a),
|
|
|
|
bool(t.m),
|
|
|
|
};
|
|
|
|
|
|
|
|
return *t.m;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::stack::member &
|
|
|
|
ircd::json::stack::member::top(const stack &s)
|
|
|
|
{
|
|
|
|
const const_chase t{s, true};
|
|
|
|
|
|
|
|
if(unlikely(!t.m))
|
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Top of stack is not of type member. (o:%b a:%b m:%b)",
|
|
|
|
bool(t.o),
|
|
|
|
bool(t.a),
|
|
|
|
bool(t.m),
|
|
|
|
};
|
|
|
|
|
|
|
|
return *t.m;
|
|
|
|
}
|
|
|
|
|
2018-04-10 22:33:26 +02:00
|
|
|
ircd::json::stack::member::member(member &&other)
|
|
|
|
noexcept
|
|
|
|
:s{std::move(other.s)}
|
|
|
|
,po{std::move(other.po)}
|
|
|
|
,name{std::move(other.name)}
|
|
|
|
,co{std::move(other.co)}
|
|
|
|
,ca{std::move(other.ca)}
|
2019-02-27 00:27:03 +01:00
|
|
|
,vc{std::move(other.vc)}
|
2018-04-10 22:33:26 +02:00
|
|
|
{
|
|
|
|
other.s = nullptr;
|
2019-02-27 00:27:03 +01:00
|
|
|
|
|
|
|
if(po)
|
|
|
|
{
|
|
|
|
assert(po->cm == &other);
|
|
|
|
po->cm = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(co)
|
|
|
|
{
|
|
|
|
assert(co->pm == &other);
|
|
|
|
co->pm = this;
|
|
|
|
}
|
|
|
|
else if(ca)
|
|
|
|
{
|
|
|
|
assert(ca->pm == &other);
|
|
|
|
ca->pm = this;
|
|
|
|
}
|
2018-04-10 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:27:23 +01:00
|
|
|
ircd::json::stack::member::member(stack &s,
|
|
|
|
const string_view &name)
|
|
|
|
:member
|
|
|
|
{
|
|
|
|
object::top(s), name
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::printer
|
|
|
|
{
|
|
|
|
const rule<string_view> member_name
|
|
|
|
{
|
|
|
|
name << name_sep
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::member::member(object &po,
|
|
|
|
const string_view &name)
|
|
|
|
:s{po.s}
|
|
|
|
,po{&po}
|
|
|
|
,name{name}
|
|
|
|
{
|
2018-04-14 00:15:45 +02:00
|
|
|
assert(s->opened());
|
|
|
|
s->rethrow_exception();
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(po.cm == nullptr);
|
|
|
|
po.cm = this;
|
|
|
|
|
|
|
|
if(po.mc)
|
2019-07-08 08:36:45 +02:00
|
|
|
s->append(',');
|
2018-04-11 01:59:08 +02:00
|
|
|
|
2020-04-16 01:06:47 +02:00
|
|
|
char tmp[512];
|
2018-04-11 01:59:08 +02:00
|
|
|
mutable_buffer buf{tmp};
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::member_name, name);
|
2018-04-11 01:59:08 +02:00
|
|
|
assert(data(buf) >= tmp);
|
|
|
|
s->append(string_view{tmp, size_t(data(buf) - tmp)});
|
2018-03-18 00:55:32 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:27:23 +01:00
|
|
|
ircd::json::stack::member::member(stack &s,
|
|
|
|
const string_view &name,
|
|
|
|
const json::value &value)
|
|
|
|
:member
|
|
|
|
{
|
|
|
|
object::top(s), name, value
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::member::member(object &po,
|
|
|
|
const string_view &name,
|
|
|
|
const json::value &value)
|
|
|
|
:member{po, name}
|
|
|
|
{
|
|
|
|
append(value);
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:53:02 +01:00
|
|
|
#if defined(__GNUC__) && !defined(__clang__)
|
2019-12-05 03:03:13 +01:00
|
|
|
__attribute__((optimize("no-lifetime-dse")))
|
2020-02-07 21:53:02 +01:00
|
|
|
#endif
|
2018-03-18 00:55:32 +01:00
|
|
|
ircd::json::stack::member::~member()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-04-10 22:33:26 +02:00
|
|
|
if(!s)
|
|
|
|
return; // std::move()'ed away
|
|
|
|
|
2018-04-10 23:17:56 +02:00
|
|
|
const unwind _{[this]
|
|
|
|
{
|
|
|
|
// Allows ~dtor to be called to close the JSON manually
|
|
|
|
s = nullptr;
|
|
|
|
}};
|
|
|
|
|
2019-01-07 22:56:47 +01:00
|
|
|
if(!vc)
|
|
|
|
s->append("null");
|
|
|
|
|
2018-03-18 00:55:32 +01:00
|
|
|
assert(co == nullptr);
|
|
|
|
assert(ca == nullptr);
|
|
|
|
assert(po);
|
|
|
|
assert(po->cm == this);
|
|
|
|
po->mc++;
|
|
|
|
po->cm = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::stack::member::append(const json::value &value)
|
|
|
|
{
|
|
|
|
assert(s);
|
|
|
|
_pre_append();
|
2020-09-07 21:37:54 +02:00
|
|
|
const unwind_nominal post{[this]
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
_post_append();
|
|
|
|
}};
|
|
|
|
|
2018-04-11 01:59:08 +02:00
|
|
|
s->append(serialized(value), [&value]
|
|
|
|
(mutable_buffer buf)
|
2018-03-18 00:55:32 +01:00
|
|
|
{
|
|
|
|
return size(stringify(buf, value));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:20:26 +01:00
|
|
|
//
|
|
|
|
// stack::checkpoint
|
|
|
|
//
|
|
|
|
|
2019-03-30 22:06:18 +01:00
|
|
|
ircd::json::stack::checkpoint::checkpoint(stack &s,
|
2023-02-19 06:21:14 +01:00
|
|
|
const bool committed,
|
|
|
|
const bool exception_rollback)
|
2019-02-24 20:20:26 +01:00
|
|
|
:s{&s}
|
|
|
|
,pc{s.cp}
|
|
|
|
,point
|
|
|
|
{
|
|
|
|
s.buf.consumed()
|
|
|
|
}
|
|
|
|
,vc{[&s]
|
|
|
|
{
|
|
|
|
const chase top
|
|
|
|
{
|
|
|
|
s, true
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
top.o?
|
|
|
|
top.o->mc:
|
|
|
|
top.a?
|
|
|
|
top.a->vc:
|
|
|
|
top.m?
|
|
|
|
top.m->vc:
|
|
|
|
0;
|
|
|
|
}()}
|
2019-03-30 22:06:18 +01:00
|
|
|
,committed
|
|
|
|
{
|
|
|
|
committed
|
|
|
|
}
|
2019-03-30 22:35:07 +01:00
|
|
|
,exception_rollback
|
|
|
|
{
|
|
|
|
exception_rollback
|
|
|
|
}
|
2019-02-24 20:20:26 +01:00
|
|
|
{
|
|
|
|
s.cp = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::checkpoint::~checkpoint()
|
|
|
|
noexcept
|
|
|
|
{
|
2019-03-30 22:35:07 +01:00
|
|
|
if(std::uncaught_exceptions() && exception_rollback)
|
2020-04-15 00:22:40 +02:00
|
|
|
committing(false);
|
2019-02-24 20:20:26 +01:00
|
|
|
|
|
|
|
if(!committing())
|
2019-03-30 22:02:51 +01:00
|
|
|
rollback();
|
2019-02-24 20:20:26 +01:00
|
|
|
|
2019-03-30 22:42:10 +01:00
|
|
|
if(!s)
|
|
|
|
return;
|
|
|
|
|
2019-02-24 20:20:26 +01:00
|
|
|
assert(s->cp == this);
|
|
|
|
s->cp = pc;
|
2020-04-16 00:59:03 +02:00
|
|
|
|
|
|
|
// Certain uses of json::stack in loops might create and destroy
|
|
|
|
// checkpoints without any appends between their lifetimes. This results
|
|
|
|
// in the buffer filling up and inevitably force-flushing under an unlucky
|
|
|
|
// checkpoint (which is bad). This non-forced flush here prevents that once
|
|
|
|
// the buffer reaches the low-watermark and there is no parent checkpoint.
|
|
|
|
if(committing())
|
|
|
|
s->flush();
|
2019-02-24 20:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::checkpoint::rollback()
|
|
|
|
{
|
2019-03-30 22:42:10 +01:00
|
|
|
if(!s)
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
"Attempting rollback of invalidated checkpoint(%p).",
|
|
|
|
this,
|
|
|
|
};
|
|
|
|
|
2019-03-30 22:02:51 +01:00
|
|
|
return false;
|
2019-03-30 22:42:10 +01:00
|
|
|
}
|
2019-03-30 22:02:51 +01:00
|
|
|
|
|
|
|
assert(point <= s->buf.consumed());
|
2019-03-30 22:42:10 +01:00
|
|
|
s->rewind(s->buf.consumed() - point);
|
2019-03-30 22:02:51 +01:00
|
|
|
|
|
|
|
const chase top
|
|
|
|
{
|
|
|
|
*s, true
|
|
|
|
};
|
|
|
|
|
|
|
|
if(top.o)
|
|
|
|
top.o->mc = vc;
|
|
|
|
else if(top.a)
|
|
|
|
top.a->vc = vc;
|
|
|
|
else if(top.m)
|
|
|
|
top.m->vc = vc;
|
|
|
|
|
2020-04-15 00:22:40 +02:00
|
|
|
committing(true);
|
2019-03-30 22:02:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:27:23 +01:00
|
|
|
//
|
|
|
|
// chase
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::json
|
|
|
|
{
|
2023-02-19 06:21:14 +01:00
|
|
|
template<class chase> static bool _next(chase &) noexcept;
|
|
|
|
template<class chase> static bool _prev(chase &) noexcept;
|
2019-01-08 19:27:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::stack::chase::chase(stack &s,
|
2023-02-19 06:21:14 +01:00
|
|
|
const bool prechase)
|
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
:a{s.ca}
|
|
|
|
,o{s.co}
|
|
|
|
,m{nullptr}
|
|
|
|
{
|
|
|
|
if(prechase)
|
|
|
|
while(next());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::chase::next()
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
{
|
|
|
|
return _next(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::chase::prev()
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
{
|
|
|
|
return _prev(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// const_chase
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::json::stack::const_chase::const_chase(const stack &s,
|
2023-02-19 06:21:14 +01:00
|
|
|
const bool prechase)
|
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
:a{s.ca}
|
|
|
|
,o{s.co}
|
|
|
|
,m{nullptr}
|
|
|
|
{
|
|
|
|
if(prechase)
|
|
|
|
while(next());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::const_chase::next()
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
{
|
|
|
|
return _next(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::stack::const_chase::prev()
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
{
|
|
|
|
return _prev(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// chase internal
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class chase>
|
|
|
|
bool
|
|
|
|
ircd::json::_next(chase &c)
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
{
|
|
|
|
if(c.o)
|
|
|
|
{
|
|
|
|
if(!c.o->cm)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c.m = c.o->cm;
|
|
|
|
c.a = nullptr;
|
|
|
|
c.o = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(c.a)
|
|
|
|
{
|
|
|
|
if(!c.a->co && !c.a->ca)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c.m = nullptr;
|
|
|
|
c.o = c.a->co;
|
|
|
|
c.a = c.a->ca;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(c.m)
|
|
|
|
{
|
|
|
|
if(!c.m->co && !c.m->ca)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c.o = c.m->co;
|
|
|
|
c.a = c.m->ca;
|
|
|
|
c.m = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class chase>
|
|
|
|
bool
|
|
|
|
ircd::json::_prev(chase &c)
|
2023-02-19 06:21:14 +01:00
|
|
|
noexcept
|
2019-01-08 19:27:23 +01:00
|
|
|
{
|
|
|
|
if(c.o)
|
|
|
|
{
|
|
|
|
if(!c.o->pa && !c.o->pm)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c.a = c.o->pa;
|
|
|
|
c.m = c.o->pm;
|
|
|
|
c.o = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(c.a)
|
|
|
|
{
|
|
|
|
if(!c.a->pa && !c.a->pm)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
c.m = c.a->pm;
|
|
|
|
c.a = c.a->pa;
|
|
|
|
c.o = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(c.m)
|
|
|
|
{
|
|
|
|
assert(c.m->po);
|
|
|
|
c.o = c.m->po;
|
|
|
|
c.a = nullptr;
|
|
|
|
c.m = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-01-22 19:52:56 +01:00
|
|
|
// json/iov.h
|
2017-09-14 20:30:06 +02:00
|
|
|
//
|
|
|
|
|
2022-06-11 23:46:20 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
static thread_local const member *member_pointer_buffer[iov::max_size];
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const iov &iov)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(iov);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
ircd::string_view
|
2018-02-18 23:47:19 +01:00
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2017-09-14 20:30:06 +02:00
|
|
|
const iov &iov)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2019-08-26 20:00:14 +02:00
|
|
|
static const auto addressof //TODO: XXX
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2019-08-26 20:00:14 +02:00
|
|
|
[](const member &m) noexcept
|
|
|
|
{
|
|
|
|
return std::addressof(m);
|
|
|
|
}
|
|
|
|
};
|
2017-09-08 10:41:02 +02:00
|
|
|
|
2019-08-26 20:00:14 +02:00
|
|
|
static const auto less_member
|
2017-10-16 06:22:18 +02:00
|
|
|
{
|
2019-08-26 20:00:14 +02:00
|
|
|
[](const member *const &a, const member *const &b) noexcept
|
|
|
|
{
|
|
|
|
return *a < *b;
|
|
|
|
}
|
|
|
|
};
|
2017-10-16 06:22:18 +02:00
|
|
|
|
2018-02-19 00:20:43 +01:00
|
|
|
static const auto print_member
|
2018-02-18 23:47:19 +01:00
|
|
|
{
|
|
|
|
[](mutable_buffer &buf, const member *const &m)
|
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::name << printer::name_sep, m->first);
|
2018-02-18 23:47:19 +01:00
|
|
|
stringify(buf, m->second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-26 20:00:14 +02:00
|
|
|
const ctx::critical_assertion ca;
|
2022-06-11 23:46:20 +02:00
|
|
|
auto &m(member_pointer_buffer);
|
2019-08-26 20:00:14 +02:00
|
|
|
if(unlikely(size_t(iov.size()) > iov.max_size))
|
|
|
|
throw iov::oversize
|
|
|
|
{
|
|
|
|
"IOV has %zd members but maximum is %zu",
|
|
|
|
iov.size(),
|
|
|
|
iov.max_size
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto start(begin(buf));
|
|
|
|
std::transform(std::begin(iov), std::end(iov), m, addressof);
|
|
|
|
std::sort(m, m + iov.size(), less_member);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::object_begin);
|
2018-02-18 23:47:19 +01:00
|
|
|
printer::list_protocol(buf, m, m + iov.size(), print_member);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::object_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(iov) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const iov &iov)
|
|
|
|
{
|
|
|
|
const size_t ret
|
|
|
|
{
|
|
|
|
1U + iov.empty()
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::accumulate(std::begin(iov), std::end(iov), ret, []
|
|
|
|
(auto ret, const auto &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-26 02:45:49 +02:00
|
|
|
ircd::json::value &
|
|
|
|
ircd::json::iov::at(const string_view &key)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-05-26 02:45:49 +02:00
|
|
|
const auto it
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-05-26 02:45:49 +02:00
|
|
|
std::find_if(std::begin(*this), std::end(*this), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return string_view{member.first} == key;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == std::end(*this))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"key '%s' not found", key
|
|
|
|
};
|
|
|
|
|
|
|
|
return it->second;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::json::value &
|
|
|
|
ircd::json::iov::at(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
2018-01-22 23:55:00 +01:00
|
|
|
const auto it
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-01-22 23:55:00 +01:00
|
|
|
std::find_if(std::begin(*this), std::end(*this), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return string_view{member.first} == key;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(it == std::end(*this))
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"key '%s' not found", key
|
|
|
|
};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-05-26 02:45:49 +02:00
|
|
|
bool
|
|
|
|
ircd::json::iov::has(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::any_of(std::begin(*this), std::end(*this), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return string_view{member.first} == key;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::push::push(iov &iov,
|
|
|
|
member member)
|
|
|
|
:node
|
|
|
|
{
|
|
|
|
iov, std::move(member)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::iov::push::push(iov &iov,
|
|
|
|
const bool &b,
|
|
|
|
const conditional_member &cp)
|
|
|
|
:node
|
|
|
|
{
|
|
|
|
b?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
|
|
|
|
|
|
|
b?
|
|
|
|
member{cp.first, cp.second()}:
|
|
|
|
member{}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::iov::add::add(iov &iov,
|
|
|
|
member member)
|
|
|
|
:node
|
|
|
|
{
|
|
|
|
iov, [&iov, &member]
|
|
|
|
{
|
|
|
|
if(iov.has(member.first))
|
2018-06-05 20:16:52 +02:00
|
|
|
throw exists
|
|
|
|
{
|
2020-10-31 09:33:46 +01:00
|
|
|
"member '%s' already exists",
|
2018-06-05 20:16:52 +02:00
|
|
|
string_view{member.first}
|
|
|
|
};
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
return std::move(member);
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::add::add(iov &iov,
|
|
|
|
const bool &b,
|
|
|
|
const conditional_member &cp)
|
2017-09-14 20:30:06 +02:00
|
|
|
:node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
b?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
2017-09-08 10:41:02 +02:00
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
[&iov, &b, &cp]
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return member{};
|
|
|
|
|
|
|
|
if(iov.has(cp.first))
|
|
|
|
throw exists
|
|
|
|
{
|
2020-10-31 09:33:46 +01:00
|
|
|
"member '%s' already exists",
|
2018-06-05 20:16:52 +02:00
|
|
|
string_view{cp.first}
|
|
|
|
};
|
|
|
|
|
|
|
|
return member
|
|
|
|
{
|
|
|
|
cp.first, cp.second()
|
|
|
|
};
|
|
|
|
}()
|
2018-06-05 08:17:29 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::set::set(iov &iov,
|
|
|
|
member member)
|
2017-09-14 20:30:06 +02:00
|
|
|
:node
|
|
|
|
{
|
|
|
|
iov, [&iov, &member]
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
iov.remove_if([&member](const auto &existing)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return string_view{existing.first} == string_view{member.first};
|
|
|
|
});
|
2017-09-08 10:41:02 +02:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
return std::move(member);
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
{
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::set::set(iov &iov,
|
|
|
|
const bool &b,
|
|
|
|
const conditional_member &cp)
|
2017-09-14 20:30:06 +02:00
|
|
|
:node
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
b?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
|
|
|
|
|
|
|
[&iov, &b, &cp]
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return member{};
|
|
|
|
|
|
|
|
iov.remove_if([&cp](const auto &existing)
|
|
|
|
{
|
|
|
|
return string_view{existing.first} == cp.first;
|
|
|
|
});
|
|
|
|
|
|
|
|
return member
|
|
|
|
{
|
|
|
|
cp.first, cp.second()
|
|
|
|
};
|
|
|
|
}()
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
{
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 18:29:34 +02:00
|
|
|
ircd::json::iov::defaults::defaults(iov &iov,
|
|
|
|
member member)
|
|
|
|
:node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
!iov.has(member.first)?
|
|
|
|
&iov:
|
|
|
|
nullptr,
|
|
|
|
|
|
|
|
std::move(member)
|
2017-10-25 18:29:34 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-05 20:16:52 +02:00
|
|
|
ircd::json::iov::defaults::defaults(iov &iov,
|
|
|
|
bool b,
|
|
|
|
const conditional_member &cp)
|
2017-10-25 18:29:34 +02:00
|
|
|
:node
|
|
|
|
{
|
2018-06-05 20:16:52 +02:00
|
|
|
[&iov, &b, &cp]() -> json::iov *
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(!iov.has(cp.first))
|
|
|
|
return &iov;
|
|
|
|
|
|
|
|
b = false;
|
|
|
|
return nullptr;
|
|
|
|
}(),
|
|
|
|
|
|
|
|
[&iov, &b, &cp]
|
|
|
|
{
|
|
|
|
if(!b)
|
|
|
|
return member{};
|
|
|
|
|
|
|
|
return member
|
|
|
|
{
|
|
|
|
cp.first, cp.second()
|
|
|
|
};
|
|
|
|
}()
|
2017-10-25 18:29:34 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-06 21:08:32 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/vector.h
|
|
|
|
//
|
|
|
|
|
2019-02-27 20:23:52 +01:00
|
|
|
size_t
|
|
|
|
ircd::json::vector::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::distance(begin(), end());
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:47:00 +01:00
|
|
|
ircd::json::vector::value_type
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::vector::operator[](const size_t i)
|
2019-02-16 01:47:00 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(i));
|
2023-02-12 05:39:48 +01:00
|
|
|
return it != end()?
|
|
|
|
*it:
|
|
|
|
object{};
|
2019-02-16 01:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::vector::value_type
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::vector::at(const size_t i)
|
2019-02-16 01:47:00 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(i));
|
2023-02-12 05:39:48 +01:00
|
|
|
if(unlikely(it == end()))
|
2019-02-16 01:47:00 +01:00
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"indice %zu", i
|
|
|
|
};
|
|
|
|
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::vector::const_iterator
|
|
|
|
ircd::json::vector::find(size_t i)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
auto it(begin());
|
|
|
|
for(; it != end() && i; ++it, i--);
|
|
|
|
return it;
|
|
|
|
}
|
2020-05-15 05:10:52 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const rule<string_view> vector_object
|
|
|
|
{
|
|
|
|
raw[object(0)]
|
|
|
|
,"vector object"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<string_view> vector_begin_parse
|
|
|
|
{
|
|
|
|
expect[ws >> (eoi | (vector_object >> ws))]
|
|
|
|
,"object vector element"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-15 05:10:52 +02:00
|
|
|
ircd::json::vector::const_iterator
|
|
|
|
ircd::json::vector::begin()
|
2020-06-10 14:29:07 +02:00
|
|
|
const
|
2020-05-15 05:10:52 +02:00
|
|
|
{
|
|
|
|
const_iterator ret
|
|
|
|
{
|
|
|
|
string_view::begin(), string_view::end()
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view &state(ret.state);
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(ret.start, ret.stop, parser::vector_begin_parse, state);
|
2020-05-15 05:10:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// vector::const_iterator::const_iterator
|
|
|
|
//
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const rule<string_view> vector_next_parse
|
|
|
|
{
|
|
|
|
expect[eoi | (vector_object >> ws)]
|
|
|
|
,"next object vector element or end"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-06 21:08:32 +01:00
|
|
|
ircd::json::vector::const_iterator &
|
|
|
|
ircd::json::vector::const_iterator::operator++()
|
|
|
|
{
|
2020-05-15 05:10:52 +02:00
|
|
|
this->state = {};
|
|
|
|
string_view &state(this->state);
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(start, stop, parser::vector_next_parse, state);
|
2017-11-06 21:08:32 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/object.h
|
|
|
|
//
|
|
|
|
|
2020-06-20 08:04:47 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
using object_member_array_type = std::array<object::member, object::max_sorted_members>;
|
|
|
|
using object_member_arrays_type = std::array<object_member_array_type, object::max_recursion_depth>;
|
|
|
|
static_assert(sizeof(object_member_arrays_type) == 3_MiB); // yay reentrance .. joy :/
|
2022-06-18 06:33:15 +02:00
|
|
|
|
2020-06-20 08:04:47 +02:00
|
|
|
static thread_local object_member_arrays_type object_member_arrays;
|
|
|
|
static thread_local size_t object_member_arrays_ctr;
|
|
|
|
|
|
|
|
static string_view _stringify(mutable_buffer &buf, const object::member *const &b, const object::member *const &e);
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const object &object)
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(object);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
|
|
|
}
|
2017-03-11 04:31:20 +01:00
|
|
|
|
2017-09-19 11:06:52 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const object &object)
|
2018-03-21 08:10:29 +01:00
|
|
|
try
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2020-06-20 08:04:47 +02:00
|
|
|
const size_t mc(object_member_arrays_ctr);
|
|
|
|
assert(mc < object_member_arrays.size());
|
|
|
|
const scope_count _mc(object_member_arrays_ctr);
|
|
|
|
auto &m(object_member_arrays.at(object_member_arrays_ctr));
|
2017-09-19 11:06:52 +02:00
|
|
|
|
2018-03-21 08:10:29 +01:00
|
|
|
size_t i(0);
|
|
|
|
for(auto it(begin(object)); it != end(object); ++it, ++i)
|
|
|
|
m.at(i) = *it;
|
|
|
|
|
|
|
|
std::sort(begin(m), begin(m) + i, []
|
2019-08-26 20:16:45 +02:00
|
|
|
(const object::member &a, const object::member &b) noexcept
|
2018-03-21 08:10:29 +01:00
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
});
|
|
|
|
|
2020-06-20 08:04:47 +02:00
|
|
|
return _stringify(buf, m.data(), m.data() + i);
|
2018-03-21 08:10:29 +01:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"Too many members (%zu) for stringifying JSON object",
|
|
|
|
size(object)
|
|
|
|
};
|
2017-09-19 11:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const object &object)
|
|
|
|
{
|
2018-03-28 01:34:33 +02:00
|
|
|
const auto b(begin(object));
|
|
|
|
const auto e(end(object));
|
|
|
|
assert(!empty(object) || b == e);
|
|
|
|
const size_t ret(1 + (b == e));
|
|
|
|
return std::accumulate(b, e, ret, []
|
2017-09-19 11:06:52 +02:00
|
|
|
(auto ret, const object::member &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2018-03-21 20:29:16 +01:00
|
|
|
bool
|
|
|
|
ircd::json::sorted(const object &object)
|
|
|
|
{
|
|
|
|
auto it(begin(object));
|
|
|
|
if(it == end(object))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
string_view last{it->first};
|
|
|
|
for(++it; it != end(object); last = it->first, ++it)
|
|
|
|
if(it->first < last)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:02:12 +01:00
|
|
|
//
|
|
|
|
// object
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::object::operator[](const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
2020-11-09 09:00:20 +01:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
find(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
return it != end()?
|
|
|
|
it->second:
|
|
|
|
string_view{};
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2019-01-22 21:02:12 +01:00
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::object::get(const string_view &key,
|
|
|
|
const string_view &def)
|
|
|
|
const
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2023-02-12 05:39:48 +01:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
find(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
return it != end()?
|
|
|
|
it->second:
|
|
|
|
def;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::object::operator std::string()
|
|
|
|
const
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
return json::strung(*this);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2020-11-09 09:00:20 +01:00
|
|
|
bool
|
|
|
|
ircd::json::object::has(const string_view &key,
|
|
|
|
const enum json::type &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &it
|
|
|
|
{
|
|
|
|
find(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
return it != end()?
|
|
|
|
json::type(it->second, type, strict):
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2019-01-22 21:02:12 +01:00
|
|
|
bool
|
|
|
|
ircd::json::object::has(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return find(key) != end();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::object::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::distance(begin(), end());
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::find(const name_hash_t &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::find_if(begin(), end(), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return name_hash(member.first) == key;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::find(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::find_if(begin(), end(), [&key]
|
|
|
|
(const auto &member)
|
|
|
|
{
|
|
|
|
return member.first == key;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const expr object_member
|
|
|
|
{
|
|
|
|
name >> ws >> name_sep >> ws >> raw[value(0)]
|
|
|
|
,"object member"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr begin_object
|
|
|
|
{
|
|
|
|
object_begin >> ws >> (object_end | object_member)
|
|
|
|
,"object"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr begin_object_parse
|
|
|
|
{
|
|
|
|
ws >> (eoi | (begin_object >> ws))
|
|
|
|
,"object begin"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<object::member> parse_begin_object
|
|
|
|
{
|
|
|
|
expect[begin_object_parse]
|
|
|
|
,"object begin"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:18:20 +02:00
|
|
|
[[gnu::hot]]
|
2020-04-18 00:46:30 +02:00
|
|
|
ircd::json::object::const_iterator
|
|
|
|
ircd::json::object::begin()
|
|
|
|
const try
|
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
const_iterator ret
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
string_view::begin(), string_view::end()
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(ret.start, ret.stop, parser::parse_begin_object, ret.state);
|
2017-09-14 20:30:06 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2022-06-18 06:33:15 +02:00
|
|
|
catch(const parser::expectation_failure<parse_error> &e)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2019-03-20 01:27:37 +01:00
|
|
|
const auto type
|
|
|
|
{
|
|
|
|
json::type(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type != type::OBJECT)
|
|
|
|
throw type_error
|
|
|
|
{
|
2020-06-10 14:29:07 +02:00
|
|
|
"Expected JSON type OBJECT, not %s.",
|
|
|
|
reflect(type)
|
2019-03-20 01:27:37 +01:00
|
|
|
};
|
|
|
|
|
2020-06-10 14:29:07 +02:00
|
|
|
throw;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 21:02:12 +01:00
|
|
|
//
|
|
|
|
// object::const_iterator
|
|
|
|
//
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const expr next_member
|
|
|
|
{
|
|
|
|
value_sep >> ws >> object_member
|
|
|
|
,"next member"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr end_object
|
|
|
|
{
|
|
|
|
object_end >> ws >> eoi
|
|
|
|
,"end of object"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr next_member_parse
|
|
|
|
{
|
|
|
|
(next_member | end_object) >> ws
|
|
|
|
,"object increment"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<object::member> parse_next_member
|
|
|
|
{
|
|
|
|
expect[next_member_parse]
|
|
|
|
,"object increment"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:18:20 +02:00
|
|
|
[[gnu::hot]]
|
2019-01-22 21:02:12 +01:00
|
|
|
ircd::json::object::const_iterator &
|
|
|
|
ircd::json::object::const_iterator::operator++()
|
|
|
|
{
|
2020-05-19 02:56:28 +02:00
|
|
|
assert(start != stop);
|
2020-05-14 04:44:56 +02:00
|
|
|
state = {};
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(start, stop, parser::parse_next_member, state);
|
2019-01-22 21:02:12 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// object::member
|
|
|
|
//
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const object::member &member)
|
|
|
|
{
|
|
|
|
s << json::strung(member);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const object::member &member)
|
|
|
|
{
|
|
|
|
char *const start(begin(buf));
|
2019-04-25 05:32:10 +02:00
|
|
|
assert(!surrounds(member.first, '"'));
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::name << printer::name_sep, member.first);
|
2019-01-22 21:02:12 +01:00
|
|
|
stringify(buf, member.second);
|
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(member) == size(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const object::member &member)
|
|
|
|
{
|
2020-10-21 08:11:19 +02:00
|
|
|
const json::value key
|
|
|
|
{
|
|
|
|
member.first, json::STRING
|
|
|
|
};
|
|
|
|
|
|
|
|
return serialized(key) + 1 + serialized(member.second);
|
2019-01-22 21:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2023-02-12 05:39:48 +01:00
|
|
|
const object::member *const b,
|
|
|
|
const object::member *const e)
|
2019-01-22 21:02:12 +01:00
|
|
|
{
|
2020-06-20 08:04:47 +02:00
|
|
|
const size_t mc(object_member_arrays_ctr);
|
|
|
|
assert(mc < object_member_arrays.size());
|
|
|
|
const scope_count _mc(object_member_arrays_ctr);
|
|
|
|
auto &m(object_member_arrays.at(object_member_arrays_ctr));
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
for(auto it(b); it != e; ++it, ++i)
|
|
|
|
m.at(i) = *it;
|
|
|
|
|
|
|
|
std::sort(begin(m), begin(m) + i, []
|
|
|
|
(const object::member &a, const object::member &b) noexcept
|
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
});
|
|
|
|
|
|
|
|
return _stringify(buf, begin(m), begin(m) + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::_stringify(mutable_buffer &buf,
|
|
|
|
const object::member *const &b,
|
|
|
|
const object::member *const &e)
|
|
|
|
{
|
2019-01-22 21:02:12 +01:00
|
|
|
static const auto stringify_member
|
|
|
|
{
|
|
|
|
[](mutable_buffer &buf, const object::member &member)
|
|
|
|
{
|
|
|
|
stringify(buf, member);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-20 08:04:47 +02:00
|
|
|
char *const start(begin(buf));
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::object_begin);
|
2019-01-22 21:02:12 +01:00
|
|
|
printer::list_protocol(buf, b, e, stringify_member);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::object_end);
|
2019-01-22 21:02:12 +01:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::serialized(const object::member *const begin,
|
|
|
|
const object::member *const end)
|
2019-01-22 21:02:12 +01:00
|
|
|
{
|
|
|
|
const size_t ret(1 + (begin == end));
|
|
|
|
return std::accumulate(begin, end, ret, []
|
|
|
|
(auto ret, const object::member &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::sorted(const object::member *const begin,
|
|
|
|
const object::member *const end)
|
2019-01-22 21:02:12 +01:00
|
|
|
{
|
|
|
|
return std::is_sorted(begin, end, []
|
|
|
|
(const object::member &a, const object::member &b)
|
|
|
|
{
|
|
|
|
return a.first < b.first;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/array.h
|
|
|
|
//
|
|
|
|
|
2019-02-21 23:00:00 +01:00
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const array &a)
|
|
|
|
{
|
|
|
|
s << json::strung(a);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const array &v)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2020-05-26 07:47:24 +02:00
|
|
|
if(likely(!string_view{v}.empty()))
|
|
|
|
return array::stringify(buf, begin(v), end(v));
|
2017-11-01 23:55:28 +01:00
|
|
|
|
2020-05-26 07:47:24 +02:00
|
|
|
const char *const start{begin(buf)};
|
|
|
|
consume(buf, copy(buf, empty_array));
|
|
|
|
const string_view ret{start, begin(buf)};
|
|
|
|
assert(serialized(v) == size(ret));
|
|
|
|
return ret;
|
2018-03-20 22:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const array &v)
|
|
|
|
{
|
2018-03-28 01:34:33 +02:00
|
|
|
assert(!empty(v) || (begin(v) == end(v)));
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::serialized(begin(v), end(v));
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2023-02-12 05:39:48 +01:00
|
|
|
const std::string *const b,
|
|
|
|
const std::string *const e)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return array::stringify(buf, b, e);
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:47:56 +02:00
|
|
|
size_t
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::serialized(const std::string *const b,
|
|
|
|
const std::string *const e)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::serialized(b, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2023-02-12 05:39:48 +01:00
|
|
|
const string_view *const b,
|
|
|
|
const string_view *const e)
|
2018-03-20 22:51:58 +01:00
|
|
|
{
|
|
|
|
return array::stringify(buf, b, e);
|
2017-09-28 05:47:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::serialized(const string_view *const b,
|
|
|
|
const string_view *const e)
|
2017-09-28 05:47:56 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
return array::serialized(b, e);
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2019-02-21 23:00:00 +01:00
|
|
|
//
|
|
|
|
// array::array
|
|
|
|
//
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
template<class it>
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::array::stringify(mutable_buffer &buf,
|
|
|
|
const it &b,
|
|
|
|
const it &e)
|
2017-03-21 09:54:10 +01:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
static const auto print_element
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
[](mutable_buffer &buf, const string_view &element)
|
2017-03-31 00:55:17 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
json::stringify(buf, element);
|
2017-03-31 00:55:17 +02:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
};
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2018-03-28 01:45:56 +02:00
|
|
|
using ircd::buffer::begin;
|
|
|
|
char *const start(begin(buf));
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::array_begin);
|
2017-09-16 20:58:27 +02:00
|
|
|
printer::list_protocol(buf, b, e, print_element);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::array_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
using ircd::buffer::size;
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
template<class it>
|
|
|
|
size_t
|
|
|
|
ircd::json::array::serialized(const it &b,
|
|
|
|
const it &e)
|
|
|
|
{
|
2018-03-24 07:18:11 +01:00
|
|
|
const size_t ret(1 + (b == e));
|
2018-03-20 22:51:58 +01:00
|
|
|
return std::accumulate(b, e, ret, []
|
|
|
|
(auto ret, const string_view &value)
|
|
|
|
{
|
|
|
|
return ret += json::serialized(value) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:00:00 +01:00
|
|
|
ircd::json::array::operator std::string()
|
|
|
|
const
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2019-02-21 23:00:00 +01:00
|
|
|
return json::strung(*this);
|
2017-03-21 09:54:10 +01:00
|
|
|
}
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const rule<string_view> array_value
|
|
|
|
{
|
|
|
|
raw[value(0)]
|
|
|
|
,"array element"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr begin_array
|
|
|
|
{
|
|
|
|
array_begin >> ws >> (array_end | array_value)
|
|
|
|
,"array begin element"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr begin_array_parse
|
|
|
|
{
|
|
|
|
ws >> (eoi | (begin_array >> ws))
|
|
|
|
,"array begin"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<string_view> parse_begin_array
|
|
|
|
{
|
|
|
|
expect[begin_array_parse]
|
|
|
|
,"array begin"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:18:20 +02:00
|
|
|
[[gnu::hot]]
|
2019-02-21 23:00:00 +01:00
|
|
|
ircd::json::array::const_iterator
|
|
|
|
ircd::json::array::begin()
|
2020-06-10 14:29:07 +02:00
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2019-02-21 23:00:00 +01:00
|
|
|
const_iterator ret
|
|
|
|
{
|
|
|
|
string_view::begin(), string_view::end()
|
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(ret.start, ret.stop, parser::parse_begin_array, ret.state);
|
2019-02-21 23:00:00 +01:00
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-31 00:55:17 +02:00
|
|
|
|
2019-02-21 23:00:00 +01:00
|
|
|
ircd::string_view
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::array::operator[](const size_t i)
|
2019-02-21 23:00:00 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(i));
|
|
|
|
return it != end()? *it : string_view{};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::array::at(const size_t i)
|
2019-02-21 23:00:00 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto it(find(i));
|
2020-12-10 14:40:48 +01:00
|
|
|
if(unlikely(it == end()))
|
2019-02-21 23:00:00 +01:00
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"indice %zu", i
|
|
|
|
};
|
|
|
|
|
|
|
|
return *it;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::array::const_iterator
|
2019-02-21 23:00:00 +01:00
|
|
|
ircd::json::array::find(size_t i)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
auto it(begin());
|
|
|
|
for(; it != end() && i; ++it, i--);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::array::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::json::array::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::distance(begin(), end());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// array::const_iterator
|
|
|
|
//
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const expr array_next_value
|
|
|
|
{
|
|
|
|
value_sep >> ws >> array_value
|
|
|
|
,"array next value"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr array_next_parse
|
|
|
|
{
|
|
|
|
(array_end | array_next_value) >> ws
|
|
|
|
,"array end or next value"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<string_view> parse_next_value
|
|
|
|
{
|
|
|
|
expect[array_next_parse]
|
|
|
|
,"array increment"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:18:20 +02:00
|
|
|
[[gnu::hot]]
|
2019-02-21 23:00:00 +01:00
|
|
|
ircd::json::array::const_iterator &
|
|
|
|
ircd::json::array::const_iterator::operator++()
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2020-05-19 02:56:28 +02:00
|
|
|
assert(start != stop);
|
2019-02-21 23:00:00 +01:00
|
|
|
state = string_view{};
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(start, stop, parser::parse_next_value, state);
|
2019-02-21 23:00:00 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2017-04-03 05:58:30 +02:00
|
|
|
|
2018-03-21 21:49:00 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/member.h
|
|
|
|
//
|
|
|
|
|
2022-06-11 23:46:20 +02:00
|
|
|
namespace ircd::json
|
2018-03-21 21:49:00 +01:00
|
|
|
{
|
2019-01-21 22:13:29 +01:00
|
|
|
using member_array = std::array<const member *, object::max_sorted_members>;
|
2018-03-21 21:49:59 +01:00
|
|
|
using member_arrays = std::array<member_array, object::max_recursion_depth>;
|
2020-03-20 02:09:15 +01:00
|
|
|
static_assert(sizeof(member_arrays) == 768_KiB);
|
2018-03-21 21:49:59 +01:00
|
|
|
|
2022-06-11 23:46:20 +02:00
|
|
|
static thread_local member_arrays member_array_buffer;
|
|
|
|
static thread_local size_t member_array_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2023-02-12 05:39:48 +01:00
|
|
|
const member *const b,
|
|
|
|
const member *const e)
|
2022-06-11 23:46:20 +02:00
|
|
|
{
|
2019-08-26 20:16:45 +02:00
|
|
|
static const auto less_member
|
2018-03-21 21:49:59 +01:00
|
|
|
{
|
2020-02-08 06:51:24 +01:00
|
|
|
[](const member *const &a, const member *const &b)
|
|
|
|
noexcept
|
2019-08-26 20:16:45 +02:00
|
|
|
{
|
|
|
|
return *a < *b;
|
|
|
|
}
|
|
|
|
};
|
2018-03-21 21:49:59 +01:00
|
|
|
|
2018-03-21 21:49:00 +01:00
|
|
|
static const auto print_member
|
|
|
|
{
|
2018-03-21 21:49:59 +01:00
|
|
|
[](mutable_buffer &buf, const member *const &m)
|
2018-03-21 21:49:00 +01:00
|
|
|
{
|
2019-04-25 05:32:10 +02:00
|
|
|
assert(type(m->first) == STRING);
|
|
|
|
stringify(buf, m->first);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::name_sep);
|
2018-03-21 21:49:59 +01:00
|
|
|
stringify(buf, m->second);
|
2018-03-21 21:49:00 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-08 06:51:24 +01:00
|
|
|
const size_t count(std::distance(b, e));
|
|
|
|
if(unlikely(count > object::max_sorted_members))
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"json::member vector of %zu exceeds maximum %zu.",
|
|
|
|
count,
|
|
|
|
object::max_sorted_members,
|
|
|
|
};
|
|
|
|
|
2022-06-11 23:46:20 +02:00
|
|
|
const ctx::critical_assertion ca;
|
|
|
|
auto &mctr(member_array_counter);
|
|
|
|
auto &ma(member_array_buffer);
|
|
|
|
|
2019-08-26 20:16:45 +02:00
|
|
|
const size_t mc{mctr};
|
|
|
|
const scope_count _mc{mctr};
|
|
|
|
assert(mc < ma.size());
|
2020-02-08 06:51:24 +01:00
|
|
|
auto &m(ma.at(mc));
|
|
|
|
for(size_t i(0); i < count; ++i)
|
|
|
|
m[i] = b + i;
|
2019-08-26 20:16:45 +02:00
|
|
|
|
2020-02-08 06:51:24 +01:00
|
|
|
std::sort(begin(m), begin(m) + count, less_member);
|
|
|
|
const char *const start(begin(buf));
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::object_begin);
|
2020-02-08 06:51:24 +01:00
|
|
|
printer::list_protocol(buf, begin(m), begin(m) + count, print_member);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::object_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2018-03-21 21:49:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::serialized(const member *const begin,
|
|
|
|
const member *const end)
|
2018-03-21 21:49:00 +01:00
|
|
|
{
|
2018-03-24 07:18:11 +01:00
|
|
|
const size_t ret(1 + (begin == end));
|
2018-03-21 21:49:00 +01:00
|
|
|
return std::accumulate(begin, end, ret, []
|
|
|
|
(auto ret, const auto &member)
|
|
|
|
{
|
|
|
|
return ret += serialized(member) + 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::sorted(const member *const begin,
|
|
|
|
const member *const end)
|
2018-03-21 21:49:00 +01:00
|
|
|
{
|
|
|
|
return std::is_sorted(begin, end, []
|
|
|
|
(const member &a, const member &b)
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:18:57 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/string.h
|
|
|
|
//
|
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
extern const char ctrl_tab[0x20][16];
|
|
|
|
extern const int32_t ctrl_tab_len[0x20];
|
|
|
|
|
|
|
|
static u8x16 lookup_ctrl_tab_len(const u8x16 block);
|
|
|
|
static u64x2 string_serialized_ctrl(const u8x16 block, const u8x16 mask, const u8x16 ctrl_mask);
|
|
|
|
static u64x2 string_serialized_utf16(const u8x16 block, const u8x16 mask);
|
|
|
|
static u64x2 string_serialized(const u8x16 block, const u8x16 mask);
|
|
|
|
|
2020-07-22 12:16:14 +02:00
|
|
|
static u64x2 string_stringify_utf16(u8x16 &block, const u8x16 mask);
|
|
|
|
static u64x2 string_stringify(u8x16 &block, const u8x16 mask);
|
2020-11-13 06:34:42 +01:00
|
|
|
|
2020-11-14 05:51:11 +01:00
|
|
|
static u64x2 string_unescape_utf16(u8x16 &block, const u8x16 mask);
|
2020-11-13 06:34:42 +01:00
|
|
|
static u64x2 string_unescape(u8x16 &block, const u8x16 mask);
|
2020-07-03 22:20:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Escaped control character LUT.
|
|
|
|
decltype(ircd::json::ctrl_tab)
|
|
|
|
ircd::json::ctrl_tab
|
|
|
|
alignas(32)
|
|
|
|
{
|
2020-07-05 08:52:15 +02:00
|
|
|
"\\u0000",
|
2020-07-03 22:20:26 +02:00
|
|
|
"\\u0001", "\\u0002", "\\u0003",
|
|
|
|
"\\u0004", "\\u0005", "\\u0006",
|
|
|
|
"\\u0007",
|
|
|
|
"\\b",
|
|
|
|
"\\t",
|
|
|
|
"\\n",
|
2022-07-02 23:26:22 +02:00
|
|
|
"\\u000b",
|
2020-07-03 22:20:26 +02:00
|
|
|
"\\f",
|
|
|
|
"\\r",
|
2022-07-02 23:26:22 +02:00
|
|
|
"\\u000e", "\\u000f", "\\u0010",
|
2020-07-03 22:20:26 +02:00
|
|
|
"\\u0011", "\\u0012", "\\u0013",
|
|
|
|
"\\u0014", "\\u0015", "\\u0016",
|
|
|
|
"\\u0017", "\\u0018", "\\u0019",
|
2022-07-02 23:26:22 +02:00
|
|
|
"\\u001a", "\\u001b", "\\u001c",
|
|
|
|
"\\u001d", "\\u001e", "\\u001f",
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Escaped control character LUT length hints
|
|
|
|
decltype(ircd::json::ctrl_tab_len)
|
|
|
|
ircd::json::ctrl_tab_len
|
|
|
|
alignas(32)
|
|
|
|
{
|
2020-07-05 08:52:15 +02:00
|
|
|
6,
|
2020-07-03 22:20:26 +02:00
|
|
|
6, 6, 6,
|
|
|
|
6, 6, 6,
|
|
|
|
6,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
6,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
6, 6, 6,
|
|
|
|
6, 6, 6,
|
|
|
|
6, 6, 6,
|
|
|
|
6, 6, 6,
|
|
|
|
6, 6, 6,
|
|
|
|
6, 6, 6,
|
|
|
|
};
|
|
|
|
|
2020-11-13 06:34:42 +01:00
|
|
|
/// Streaming transform of serialized (valid, escaped) JSON string content to
|
|
|
|
/// preimage content. The result may contain integrals and control codes
|
|
|
|
/// (including null characters) if the input contains their escaped rep.
|
|
|
|
///
|
|
|
|
/// Use this function with extreme care. Note that it is almost entirely
|
|
|
|
/// unnecessary to use this operation during the normal course of network
|
|
|
|
/// server operation with JSON in -> JSON out as the rest of this ircd::json
|
|
|
|
/// API (i.e stringify()) can rewrite/correct all inputs without performing
|
|
|
|
/// any unescape conversion at any point.
|
|
|
|
///
|
2019-08-02 07:18:57 +02:00
|
|
|
ircd::const_buffer
|
2020-11-13 06:34:42 +01:00
|
|
|
ircd::json::unescape(const mutable_buffer &dst,
|
|
|
|
const string &src)
|
|
|
|
{
|
|
|
|
using block_t = u8x16;
|
|
|
|
|
|
|
|
const u64x2 max
|
|
|
|
{
|
|
|
|
size(dst), size(src),
|
|
|
|
};
|
|
|
|
|
|
|
|
const u64x2 res // consumed [dst, src]
|
|
|
|
{
|
|
|
|
simd::transform<block_t>(data(dst), data(src), max, string_unescape)
|
|
|
|
};
|
|
|
|
|
|
|
|
return const_buffer
|
|
|
|
{
|
|
|
|
data(dst), res[0] // output pos (bytes written)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::u64x2
|
|
|
|
ircd::json::string_unescape(u8x16 &block,
|
|
|
|
const u8x16 block_mask)
|
2019-08-02 07:18:57 +02:00
|
|
|
{
|
2020-11-13 06:34:42 +01:00
|
|
|
const u8x16 is_esc
|
|
|
|
(
|
|
|
|
block == '\\'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Fastest-path; backward branch to count and consume all of the input.
|
|
|
|
if(likely(!simd::any(is_esc | ~block_mask)))
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
sizeof(block), sizeof(block)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u64 regular_prefix_count
|
|
|
|
{
|
|
|
|
simd::lzcnt(is_esc | ~block_mask) / 8
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fast-path; backward branch to count and consume uninteresting characters
|
|
|
|
// from the front of the input.
|
|
|
|
if(likely(regular_prefix_count))
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
regular_prefix_count, regular_prefix_count,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Escape sequence case
|
|
|
|
assert(block[0] == '\\');
|
|
|
|
const u8x16 subject
|
|
|
|
{
|
|
|
|
simd::broad_cast(block, block[1]) &
|
|
|
|
simd::broad_cast(block_mask, block_mask[1])
|
|
|
|
};
|
|
|
|
|
|
|
|
// Legitimately escaped sequence bank
|
|
|
|
const u8x16 cases
|
|
|
|
{
|
|
|
|
'b', 't', 'n', 'f', 'r', '"', '\\',
|
|
|
|
'u',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Unescaped replacements
|
|
|
|
const u8x16 integral
|
|
|
|
{
|
|
|
|
'\b', '\t', '\n', '\f', '\r', '"', '\\', // replacement integrals
|
|
|
|
'u', // not selected b/c utf16 branch taken
|
|
|
|
block[1], block[1], block[1], block[1], // filler for unnecessary escapes
|
|
|
|
block[1], block[1], block[1], block[1], // filler for unnecessary escapes
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 match
|
|
|
|
(
|
|
|
|
subject == cases
|
|
|
|
);
|
|
|
|
|
|
|
|
const u64 match_depth
|
|
|
|
{
|
|
|
|
simd::lzcnt(match) / 8
|
|
|
|
};
|
|
|
|
|
|
|
|
// Possible utf-16 surrogate(s)
|
|
|
|
if(match_depth == 7)
|
|
|
|
{
|
|
|
|
assert(block[1] == 'u');
|
2020-11-14 05:51:11 +01:00
|
|
|
return string_unescape_utf16(block, block_mask);
|
2020-11-13 06:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform replacement of the escaped character.
|
|
|
|
assert(match_depth < sizeof(integral));
|
|
|
|
block[0] = integral[match_depth];
|
|
|
|
|
|
|
|
// Increment output by 1 and input by 2 because we lost the escaping
|
|
|
|
// solidus and left a replacement character
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
1UL, 2UL,
|
|
|
|
};
|
2019-08-02 07:18:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 05:51:11 +01:00
|
|
|
/// Unrestricted UTF-16 surrogate to UTF-8 integral conversion functor; this
|
|
|
|
/// will output any character including control codes, such as \u0000.
|
|
|
|
ircd::u64x2
|
|
|
|
ircd::json::string_unescape_utf16(u8x16 &block,
|
|
|
|
const u8x16 block_mask)
|
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
const u32x4 unicode
|
2020-11-14 05:51:11 +01:00
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
utf16::decode_surrogate_aligned_next(block & block_mask)
|
2020-11-14 05:51:11 +01:00
|
|
|
};
|
|
|
|
|
2023-01-31 09:51:18 +01:00
|
|
|
const u32x4 length
|
2021-02-26 03:06:03 +01:00
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
utf8::length(unicode)
|
2021-02-26 03:06:03 +01:00
|
|
|
};
|
|
|
|
|
2023-01-31 09:51:18 +01:00
|
|
|
const u8x16 pair_mask
|
|
|
|
(
|
|
|
|
length != 0 || shl<32>(length) == 4
|
|
|
|
);
|
|
|
|
|
|
|
|
const u8x16 is_surrogate
|
|
|
|
(
|
|
|
|
utf16::find_surrogate(block & block_mask) & pair_mask
|
|
|
|
);
|
2020-11-14 05:51:11 +01:00
|
|
|
|
|
|
|
const u32x4 encoded_sparse
|
|
|
|
{
|
2021-08-06 08:37:58 +02:00
|
|
|
utf8::encode_sparse(unicode)
|
2020-11-14 05:51:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 encoded
|
|
|
|
(
|
|
|
|
encoded_sparse
|
|
|
|
);
|
|
|
|
|
|
|
|
size_t di(0), i(0);
|
|
|
|
for(; i < 2 && length[i] > 0; ++i)
|
|
|
|
for(size_t j(0); j < length[i]; ++j)
|
|
|
|
block[di++] = encoded[i * 4 + j];
|
|
|
|
|
2023-01-31 09:51:18 +01:00
|
|
|
const auto surrogates
|
|
|
|
{
|
|
|
|
simd::popcnt(u64x2(popmask(is_surrogate)))
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(surrogates > 0 && surrogates <= 2);
|
2020-11-14 05:51:11 +01:00
|
|
|
assert(di == length[0] + length[1]);
|
|
|
|
assert(i >= 1 && i <= 2);
|
|
|
|
return u64x2
|
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
di, 6U * surrogates
|
2020-11-14 05:51:11 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:18:57 +02:00
|
|
|
ircd::json::string
|
|
|
|
ircd::json::escape(const mutable_buffer &buf,
|
|
|
|
const string_view &in)
|
|
|
|
{
|
2020-05-21 06:09:15 +02:00
|
|
|
mutable_buffer out{buf};
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(out, printer::string, in);
|
2020-05-21 06:09:15 +02:00
|
|
|
const string_view ret
|
2019-08-02 07:18:57 +02:00
|
|
|
{
|
|
|
|
data(buf), data(out)
|
|
|
|
};
|
2020-05-21 06:09:15 +02:00
|
|
|
|
|
|
|
return ret;
|
2019-08-02 07:18:57 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
/// Streaming transform for canonical JSON strings. This function takes
|
|
|
|
/// virtually any input and "always makes it right" i.e. always outputs
|
|
|
|
/// the application's so-called canonical JSON.
|
|
|
|
///
|
|
|
|
/// This involves a variable-length transformation where the output might
|
|
|
|
/// end up as significantly longer or shorter than the input; neither will
|
|
|
|
/// have any hope for aligned access, and most of the inputs are short and
|
|
|
|
/// already canonical. This is all tricky.
|
|
|
|
///
|
|
|
|
size_t
|
|
|
|
ircd::json::string::stringify(const mutable_buffer &buf,
|
|
|
|
const string_view &input)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
using block_t = u8x16;
|
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
const u64x2 max
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
ircd::size(buf), ircd::size(input),
|
|
|
|
};
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
const auto consumed
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2020-10-10 09:07:06 +02:00
|
|
|
simd::transform<block_t>(ircd::data(buf), ircd::data(input), max, string_stringify)
|
2020-09-09 09:27:29 +02:00
|
|
|
};
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
return consumed[0]; // output pos (bytes written)
|
2020-07-03 22:20:26 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
/// Returns two addends to the outer loop. The second advances the input string
|
2020-07-03 22:20:26 +02:00
|
|
|
/// pointer any number of bytes; the block for the next invocation will start
|
|
|
|
/// at the new offset. This function may want to advance the input less than
|
|
|
|
/// the full block width if there's a possibility something important is being
|
|
|
|
/// split between blocks (i.e. an escaped utf-16 surrogate pair of 12 chars);
|
|
|
|
/// next invocation will then encounter the contiguous sequence without issue.
|
2020-09-09 09:27:29 +02:00
|
|
|
/// The first value is added to the final return count to indicate the length
|
2020-07-03 22:20:26 +02:00
|
|
|
/// of the input string in serialized form after correction. Partial sequences
|
|
|
|
/// trailing off the block are not counted here so they can be pushed over to
|
|
|
|
/// the next invocation.
|
|
|
|
///
|
|
|
|
/// The input is a block of characters from the original string. When the block
|
|
|
|
/// cannot be filled at the end of a string (or a short string) the block_mask
|
|
|
|
/// will indicate 0 for any bytes past the end, otherwise -1 for valid chars;
|
|
|
|
/// note that null characters in the string are valid which we will escape.
|
|
|
|
///
|
|
|
|
ircd::u64x2
|
2020-07-22 12:16:14 +02:00
|
|
|
ircd::json::string_stringify(u8x16 &block,
|
2020-07-03 22:20:26 +02:00
|
|
|
const u8x16 block_mask)
|
|
|
|
{
|
|
|
|
const u8x16 is_esc
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
block == '\\'
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u8x16 is_quote
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
block == '"'
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u8x16 is_ctrl
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
block < 0x20
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u8x16 is_special
|
|
|
|
{
|
|
|
|
is_esc | is_quote | is_ctrl
|
|
|
|
};
|
|
|
|
|
2020-09-04 04:04:09 +02:00
|
|
|
// Fastest-path; backward branch to count and consume all of the input.
|
2020-09-28 08:36:15 +02:00
|
|
|
if(likely(!simd::any(is_special | ~block_mask)))
|
2020-09-04 04:04:09 +02:00
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
sizeof(u8x16), sizeof(u8x16)
|
|
|
|
};
|
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
// Count the number of uninteresting characters from the front of the
|
|
|
|
// block. With the special characters masked, we count leading zeroes.
|
|
|
|
// The inverted block_mask generates non-zero bits to terminate any
|
|
|
|
// counting past the end of the string.
|
|
|
|
const u64 regular_prefix_count
|
|
|
|
{
|
2020-08-23 10:32:16 +02:00
|
|
|
simd::lzcnt(is_special | ~block_mask) / 8
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Fast-path; backward branch to count and consume uninteresting characters
|
|
|
|
// from the front of the input.
|
|
|
|
if(likely(regular_prefix_count))
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
regular_prefix_count, regular_prefix_count,
|
|
|
|
};
|
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Unescaped quote case
|
|
|
|
if(is_quote[0])
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2020-07-16 02:26:59 +02:00
|
|
|
block[0] = '\\';
|
|
|
|
block[1] = '"';
|
|
|
|
return u64x2
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
2, 1
|
2020-07-16 02:26:59 +02:00
|
|
|
};
|
|
|
|
}
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Control character case
|
|
|
|
if(is_ctrl[0])
|
|
|
|
{
|
2022-06-14 04:11:03 +02:00
|
|
|
static const auto tab
|
|
|
|
{
|
|
|
|
reinterpret_cast<const u128x1 *>(ctrl_tab)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8 idx
|
|
|
|
{
|
|
|
|
block[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
block = tab[idx];
|
2020-07-16 02:26:59 +02:00
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
u64(ctrl_tab_len[idx]), 1
|
2020-07-16 02:26:59 +02:00
|
|
|
};
|
|
|
|
}
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Escape sequence case
|
|
|
|
assert(block[0] == '\\');
|
|
|
|
|
|
|
|
// Legitimately escaped sequence bank
|
|
|
|
const u8x16 cases
|
|
|
|
{
|
|
|
|
'b', 't', 'n', 'f', 'r', '"', '\\', 'u'
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 subject
|
|
|
|
{
|
|
|
|
simd::broad_cast(block, block[1]) &
|
|
|
|
simd::broad_cast(block_mask, block_mask[1])
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 match
|
|
|
|
(
|
|
|
|
subject == cases
|
|
|
|
);
|
|
|
|
|
|
|
|
const u64 match_depth
|
|
|
|
{
|
2020-08-23 10:32:16 +02:00
|
|
|
simd::lzcnt(match) / 8
|
2020-07-16 02:26:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Legitimately escaped single char
|
|
|
|
if(match_depth < 7)
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
2, 2
|
|
|
|
};
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Unnecessary escape; unless it's the last char.
|
|
|
|
if(match_depth > 7)
|
|
|
|
{
|
|
|
|
block[0] = '\\';
|
|
|
|
block[1] = '\\';
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
block_mask[1]? 0UL: 2UL, 1
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
}
|
2020-07-16 02:26:59 +02:00
|
|
|
|
|
|
|
// Possible utf-16 surrogate(s)
|
|
|
|
assert(block[1] == 'u');
|
|
|
|
return string_stringify_utf16(block, block_mask);
|
2020-07-03 22:20:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::u64x2
|
2020-07-22 12:16:14 +02:00
|
|
|
ircd::json::string_stringify_utf16(u8x16 &block,
|
2020-07-03 22:20:26 +02:00
|
|
|
const u8x16 block_mask)
|
|
|
|
{
|
|
|
|
const u32x4 unicode
|
|
|
|
{
|
|
|
|
utf16::decode_surrogate_aligned_next(block & block_mask)
|
|
|
|
};
|
|
|
|
|
2023-01-31 09:51:18 +01:00
|
|
|
const u32x4 length_encoded
|
|
|
|
{
|
|
|
|
utf8::length(unicode)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 pair_mask
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2023-01-31 09:51:18 +01:00
|
|
|
length_encoded != 0 || shl<32>(length_encoded) == 4
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2023-01-31 09:51:18 +01:00
|
|
|
const u8x16 is_surrogate
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2023-01-31 09:51:18 +01:00
|
|
|
utf16::find_surrogate(block & block_mask) & pair_mask
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-06 16:19:17 +02:00
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
const u32x4 is_ctrl
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
unicode < 0x20
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u32x4 ctrl_idx
|
|
|
|
{
|
|
|
|
unicode & is_ctrl
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x4 length_surrogate
|
|
|
|
{
|
|
|
|
u32(ctrl_tab_len[ctrl_idx[0]]),
|
|
|
|
u32(ctrl_tab_len[ctrl_idx[1]]),
|
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:17 +02:00
|
|
|
// Supplement the escaped surrogate length for excluded codepoints.
|
2020-07-03 22:20:26 +02:00
|
|
|
const u32x4 length
|
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
(length_encoded & ~is_ctrl) | (length_surrogate & is_ctrl)
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const u32x4 encoded_sparse
|
|
|
|
{
|
2021-08-06 08:37:58 +02:00
|
|
|
utf8::encode_sparse(unicode)
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 encoded
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
encoded_sparse
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
size_t di(0);
|
|
|
|
for(size_t i(0); i < 2; ++i)
|
|
|
|
for(size_t j(0); j < length[i]; ++j)
|
|
|
|
block[di++] = is_ctrl[i]?
|
|
|
|
ctrl_tab[ctrl_idx[i]][j]:
|
|
|
|
encoded[i * 4 + j];
|
|
|
|
|
2020-07-05 04:56:17 +02:00
|
|
|
const auto surrogates
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
simd::popcnt(u64x2(popmask(is_surrogate)))
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(di == length[0] + length[1]);
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
di, std::max(6U * surrogates, 1U)
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-04 04:05:13 +02:00
|
|
|
/// Determine the length of the JSON string value after canonization by
|
|
|
|
/// string::stringify() on the input. See the docs for string::stringify()
|
|
|
|
/// as most details are the same here, except this has no output stream
|
|
|
|
/// or transformation logic.
|
2020-07-03 22:20:26 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::string::serialized(const string_view &input)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
using block_t = u8x16;
|
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
const u64x2 max
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
0, ircd::size(input)
|
|
|
|
};
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
const auto count
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2020-10-10 09:07:06 +02:00
|
|
|
simd::for_each<block_t>(ircd::data(input), max, string_serialized)
|
2020-09-09 09:27:29 +02:00
|
|
|
};
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-09-09 09:27:29 +02:00
|
|
|
return count[0];
|
2020-07-03 22:20:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::u64x2
|
|
|
|
ircd::json::string_serialized(const u8x16 block,
|
|
|
|
const u8x16 block_mask)
|
|
|
|
{
|
2020-07-16 02:26:59 +02:00
|
|
|
assert(block_mask[0] == 0xff);
|
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
const u8x16 is_esc
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
block == '\\'
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u8x16 is_quote
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
block == '"'
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u8x16 is_ctrl
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
block < 0x20
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u8x16 is_special
|
|
|
|
{
|
|
|
|
is_esc | is_quote | is_ctrl
|
|
|
|
};
|
|
|
|
|
2020-09-04 04:04:09 +02:00
|
|
|
// Fastest-path; backward branch to count and consume all of the input.
|
2020-09-28 08:36:15 +02:00
|
|
|
if(likely(!simd::any(is_special | ~block_mask)))
|
2020-09-04 04:04:09 +02:00
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
sizeof(u8x16), sizeof(u8x16)
|
|
|
|
};
|
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
const u64 regular_prefix_count
|
|
|
|
{
|
2020-08-23 10:32:16 +02:00
|
|
|
simd::lzcnt(is_special | ~block_mask) / 8
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Fast-path; backward branch to count and consume uninteresting characters
|
|
|
|
// from the front of the input.
|
|
|
|
if(likely(regular_prefix_count))
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
regular_prefix_count, regular_prefix_count,
|
|
|
|
};
|
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Unescaped quote: +1
|
|
|
|
if(is_quote[0])
|
|
|
|
return u64x2
|
2020-07-05 14:47:23 +02:00
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
2, 1
|
2020-07-16 02:26:59 +02:00
|
|
|
};
|
2020-07-05 14:47:23 +02:00
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Covers the ctrl 0x00-0x20 range only; no other character here.
|
|
|
|
if(is_ctrl[0])
|
|
|
|
return string_serialized_ctrl(block, block_mask, is_ctrl);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
// Escape sequence
|
|
|
|
assert(block[0] == '\\');
|
|
|
|
|
|
|
|
// Legitimate escape bank
|
|
|
|
const u8x16 cases
|
|
|
|
{
|
|
|
|
'b', 't', 'n', 'f', 'r', '"', '\\', 'u'
|
|
|
|
};
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2020-07-16 02:26:59 +02:00
|
|
|
const u8x16 subject
|
|
|
|
{
|
|
|
|
simd::broad_cast(block, block[1]) &
|
|
|
|
simd::broad_cast(block_mask, block_mask[1])
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 match
|
|
|
|
(
|
|
|
|
subject == cases
|
|
|
|
);
|
|
|
|
|
|
|
|
const u64 match_depth
|
|
|
|
{
|
2020-08-23 10:32:16 +02:00
|
|
|
simd::lzcnt(match) / 8
|
2020-07-16 02:26:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Legitimately escaped single char
|
|
|
|
if(match_depth < 7)
|
|
|
|
return u64x2
|
|
|
|
{
|
|
|
|
2, 2
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
2020-07-16 02:26:59 +02:00
|
|
|
|
|
|
|
// Unnecessary escape; unless it's the last char: -1
|
|
|
|
if(match_depth > 7)
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
block_mask[1]? 0UL: 2UL, 1
|
2020-07-16 02:26:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Possible utf-16 surrogate(s)
|
|
|
|
assert(block[1] == 'u');
|
|
|
|
return string_serialized_utf16(block, block_mask);
|
2020-07-03 22:20:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::u64x2
|
|
|
|
ircd::json::string_serialized_utf16(const u8x16 block,
|
|
|
|
const u8x16 block_mask)
|
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
const u32x4 unicode
|
|
|
|
{
|
|
|
|
utf16::decode_surrogate_aligned_next(block & block_mask)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u32x4 length_encoded
|
|
|
|
{
|
|
|
|
utf8::length(unicode)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u8x16 pair_mask
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2023-01-31 09:51:18 +01:00
|
|
|
length_encoded != 0 || shl<32>(length_encoded) == 4
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
2023-01-31 09:51:18 +01:00
|
|
|
const u8x16 is_surrogate
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2023-01-31 09:51:18 +01:00
|
|
|
utf16::find_surrogate(block & block_mask) & pair_mask
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-06 16:19:17 +02:00
|
|
|
|
2020-07-03 22:20:26 +02:00
|
|
|
const u32x4 is_ctrl
|
2020-07-10 03:53:02 +02:00
|
|
|
(
|
2020-07-03 22:20:26 +02:00
|
|
|
unicode < 0x20
|
2020-07-10 03:53:02 +02:00
|
|
|
);
|
2020-07-03 22:20:26 +02:00
|
|
|
|
|
|
|
const u32x4 ctrl_idx
|
|
|
|
{
|
|
|
|
unicode & is_ctrl
|
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:17 +02:00
|
|
|
const i32x4 length_surrogate
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
|
|
|
ctrl_tab_len[ctrl_idx[0]],
|
|
|
|
ctrl_tab_len[ctrl_idx[1]],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Supplement the escaped surrogate length for excluded codepoints.
|
|
|
|
const u32x4 length
|
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
(length_encoded & ~is_ctrl) | (length_surrogate & is_ctrl)
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto total_length
|
|
|
|
{
|
|
|
|
length[0] + length[1]
|
|
|
|
};
|
|
|
|
|
2020-07-05 04:56:17 +02:00
|
|
|
const auto surrogates
|
2020-07-03 22:20:26 +02:00
|
|
|
{
|
2023-01-31 09:51:18 +01:00
|
|
|
popcnt(u64x2(popmask(is_surrogate)))
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
total_length, std::max(6U * surrogates, 1U)
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::u64x2
|
|
|
|
ircd::json::string_serialized_ctrl(const u8x16 block,
|
|
|
|
const u8x16 block_mask,
|
|
|
|
const u8x16 is_ctrl)
|
|
|
|
{
|
|
|
|
assert(block[0] < 0x20);
|
|
|
|
const u8x16 ctrl_esc_len
|
|
|
|
{
|
|
|
|
lookup_ctrl_tab_len(block & is_ctrl)
|
|
|
|
};
|
|
|
|
|
|
|
|
const u64 ctrl_prefix_count
|
|
|
|
{
|
2020-08-23 10:32:16 +02:00
|
|
|
simd::lzcnt(~is_ctrl | ~block_mask) / 8
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
u64 ret(0);
|
|
|
|
for(size_t i(0); i < ctrl_prefix_count; ++i)
|
|
|
|
ret += ctrl_esc_len[i];
|
|
|
|
|
|
|
|
return u64x2
|
|
|
|
{
|
2020-09-09 09:27:29 +02:00
|
|
|
ret, ctrl_prefix_count
|
2020-07-03 22:20:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs a parallel transform of control characters in the input into
|
|
|
|
/// the length of their escape surrogate. The input character must be in
|
|
|
|
/// the control character range.
|
|
|
|
ircd::u8x16
|
|
|
|
ircd::json::lookup_ctrl_tab_len(const u8x16 in)
|
|
|
|
{
|
|
|
|
static const int32_t *const tab
|
|
|
|
{
|
|
|
|
ctrl_tab_len
|
|
|
|
};
|
|
|
|
|
2020-11-12 04:40:50 +01:00
|
|
|
size_t i, j, k(0);
|
2020-07-03 22:20:26 +02:00
|
|
|
i32x4 idx[4]
|
|
|
|
{
|
|
|
|
{ in[k++], in[k++], in[k++], in[k++] },
|
|
|
|
{ in[k++], in[k++], in[k++], in[k++] },
|
|
|
|
{ in[k++], in[k++], in[k++], in[k++] },
|
|
|
|
{ in[k++], in[k++], in[k++], in[k++] },
|
|
|
|
};
|
|
|
|
|
2022-06-14 04:11:03 +02:00
|
|
|
u8x16 ret{0};
|
2020-11-12 04:40:50 +01:00
|
|
|
i32x4 res[4];
|
|
|
|
for(k = 0, i = 0; i < 4; ++i)
|
|
|
|
for(j = 0; j < 4; ++j)
|
|
|
|
res[i][j] = tab[idx[i][j]],
|
2020-07-03 22:20:26 +02:00
|
|
|
ret[k++] = res[i][j];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// json/value.h
|
|
|
|
//
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::json::operator<<(std::ostream &s, const value &v)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
s << json::strung(v);
|
2017-09-14 20:30:06 +02:00
|
|
|
return s;
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
2023-02-12 05:39:48 +01:00
|
|
|
const value *const b,
|
|
|
|
const value *const e)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
static const auto print_value
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
[](mutable_buffer &buf, const value &value)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2017-09-16 20:58:27 +02:00
|
|
|
stringify(buf, value);
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-09-16 20:58:27 +02:00
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-16 20:58:27 +02:00
|
|
|
char *const start(begin(buf));
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::array_begin);
|
2017-09-16 20:58:27 +02:00
|
|
|
printer::list_protocol(buf, b, e, print_value);
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::array_end);
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(b, e) == size(ret));
|
|
|
|
return ret;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const value &v)
|
|
|
|
{
|
|
|
|
const auto start
|
|
|
|
{
|
|
|
|
begin(buf)
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(v.type)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case STRING:
|
|
|
|
{
|
2018-09-06 08:10:51 +02:00
|
|
|
if(!v.string)
|
|
|
|
{
|
|
|
|
consume(buf, copy(buf, empty_string));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-09-09 04:45:19 +02:00
|
|
|
if(unlikely(v.len > value::max_string_size))
|
|
|
|
throw print_error
|
|
|
|
{
|
|
|
|
"String value cannot have length:%zu which exceeds limit:%zu",
|
|
|
|
v.len,
|
|
|
|
value::max_string_size,
|
|
|
|
};
|
|
|
|
|
2018-09-06 08:10:51 +02:00
|
|
|
const string_view sv
|
|
|
|
{
|
|
|
|
v.string, v.len
|
|
|
|
};
|
|
|
|
|
2020-06-24 20:37:00 +02:00
|
|
|
if(v.serial)
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::string, json::string(sv));
|
2020-06-24 20:37:00 +02:00
|
|
|
else
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::string, sv);
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
{
|
2018-03-21 23:43:35 +01:00
|
|
|
if(v.serial)
|
2022-06-18 06:33:15 +02:00
|
|
|
printer::print(buf, printer::literal, string_view{v});
|
2018-03-21 23:43:35 +01:00
|
|
|
else if(v.integer)
|
|
|
|
consume(buf, copy(buf, "true"_sv));
|
|
|
|
else
|
|
|
|
consume(buf, copy(buf, "false"_sv));
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
{
|
|
|
|
if(v.serial)
|
2018-03-20 22:51:58 +01:00
|
|
|
stringify(buf, json::object{string_view{v}});
|
2020-06-23 09:55:16 +02:00
|
|
|
else if(v.object)
|
2017-09-14 20:30:06 +02:00
|
|
|
stringify(buf, v.object, v.object + v.len);
|
2020-06-23 09:55:16 +02:00
|
|
|
else
|
|
|
|
consume(buf, copy(buf, empty_object));
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
{
|
|
|
|
if(v.serial)
|
2018-03-20 22:51:58 +01:00
|
|
|
stringify(buf, json::array{string_view{v}});
|
2020-06-23 09:55:16 +02:00
|
|
|
else if(v.array)
|
2017-09-14 20:30:06 +02:00
|
|
|
stringify(buf, v.array, v.array + v.len);
|
2020-06-23 09:55:16 +02:00
|
|
|
else
|
|
|
|
consume(buf, copy(buf, empty_array));
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
|
|
|
{
|
|
|
|
if(v.serial)
|
2022-06-18 06:33:15 +02:00
|
|
|
//printer::print(buf, printer::number, string_view{v});
|
2018-03-28 01:15:20 +02:00
|
|
|
consume(buf, copy(buf, strip(string_view{v}, ' ')));
|
2018-03-20 22:51:58 +01:00
|
|
|
else if(v.floats)
|
|
|
|
consume(buf, copy(buf, lex_cast(v.floating)));
|
2017-09-14 20:30:06 +02:00
|
|
|
else
|
2018-03-20 22:51:58 +01:00
|
|
|
consume(buf, copy(buf, lex_cast(v.integer)));
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 01:45:56 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
|
|
|
start, begin(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(serialized(v) == size(ret));
|
|
|
|
return ret;
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:41:02 +02:00
|
|
|
size_t
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::serialized(const values &v)
|
2017-09-08 10:41:02 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
return serialized(std::begin(v), std::end(v));
|
2017-09-08 10:41:02 +02:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:36:18 +01:00
|
|
|
size_t
|
2023-02-12 05:39:48 +01:00
|
|
|
ircd::json::serialized(const value *const begin,
|
|
|
|
const value *const end)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
// One opening '[' and either one ']' or comma count.
|
2018-03-24 07:18:11 +01:00
|
|
|
const size_t ret(1 + (begin == end));
|
2017-09-14 20:30:06 +02:00
|
|
|
return std::accumulate(begin, end, size_t(ret), []
|
|
|
|
(auto ret, const value &v)
|
|
|
|
{
|
|
|
|
return ret += serialized(v) + 1; // 1 comma
|
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
size_t
|
|
|
|
ircd::json::serialized(const value &v)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(v.type)
|
|
|
|
{
|
2018-04-04 22:36:30 +02:00
|
|
|
case OBJECT: return
|
|
|
|
v.serial?
|
|
|
|
serialized(json::object{v}):
|
|
|
|
serialized(v.object, v.object + v.len);
|
|
|
|
|
|
|
|
case ARRAY: return
|
|
|
|
v.serial?
|
|
|
|
serialized(json::array{v}):
|
|
|
|
serialized(v.array, v.array + v.len);
|
|
|
|
|
|
|
|
case LITERAL: return
|
|
|
|
v.serial?
|
|
|
|
v.len:
|
|
|
|
v.integer?
|
|
|
|
size(literal_true):
|
|
|
|
size(literal_false);
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case NUMBER:
|
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
if(v.serial)
|
2018-03-28 01:15:20 +02:00
|
|
|
return size(strip(string_view{v}, ' '));
|
2018-03-20 22:51:58 +01:00
|
|
|
else if(v.floats)
|
|
|
|
return size(lex_cast(v.floating));
|
|
|
|
else
|
|
|
|
return size(lex_cast(v.integer));
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case STRING:
|
|
|
|
{
|
|
|
|
if(!v.string)
|
2018-03-29 07:05:54 +02:00
|
|
|
return size(empty_string);
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2020-07-04 03:03:54 +02:00
|
|
|
const string_view sv
|
|
|
|
{
|
|
|
|
v.string, v.len
|
|
|
|
};
|
2020-06-24 20:37:00 +02:00
|
|
|
|
2020-07-04 03:03:54 +02:00
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
v.serial?
|
|
|
|
json::string::serialized(json::string(sv)):
|
|
|
|
json::string::serialized(sv)
|
|
|
|
};
|
2020-06-24 20:37:00 +02:00
|
|
|
|
2020-07-04 03:03:54 +02:00
|
|
|
return 1 + ret + 1;
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-21 22:13:29 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding the size of a type[%u] is undefined", int(v.type)
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
//
|
2018-04-05 00:20:24 +02:00
|
|
|
// value::value
|
2017-09-12 18:37:44 +02:00
|
|
|
//
|
|
|
|
|
2018-04-05 00:20:24 +02:00
|
|
|
ircd::json::value::value(const std::string &s,
|
|
|
|
const enum type &type)
|
|
|
|
:string{nullptr}
|
|
|
|
,len{0}
|
|
|
|
,type{type}
|
|
|
|
,serial{type == STRING? surrounds(s, '"') : true}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
|
|
|
{
|
|
|
|
const string_view sv{s};
|
|
|
|
create_string(serialized(sv), [&sv]
|
2020-02-07 21:47:17 +01:00
|
|
|
(mutable_buffer &buffer)
|
2018-04-05 00:20:24 +02:00
|
|
|
{
|
2020-02-07 21:47:17 +01:00
|
|
|
json::stringify(buffer, sv);
|
2018-04-05 00:20:24 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::value(const json::members &members)
|
|
|
|
:string{nullptr}
|
|
|
|
,len{serialized(members)}
|
|
|
|
,type{OBJECT}
|
|
|
|
,serial{true}
|
|
|
|
,alloc{true}
|
|
|
|
,floats{false}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
create_string(len, [&members]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
json::stringify(buffer, members);
|
|
|
|
});
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::value(const value &other)
|
|
|
|
:integer{other.integer}
|
2023-03-24 02:26:16 +01:00
|
|
|
,len{other.serial? serialized(other): other.len}
|
2017-09-14 20:30:06 +02:00
|
|
|
,type{other.type}
|
|
|
|
,serial{other.serial}
|
|
|
|
,alloc{other.alloc}
|
|
|
|
,floats{other.floats}
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(serial)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
create_string(len, [&other]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2019-03-07 23:04:45 +01:00
|
|
|
json::stringify(buffer, other);
|
2017-09-14 20:30:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else switch(type)
|
2017-03-16 21:36:18 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case OBJECT:
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(!object)
|
2018-01-27 04:02:56 +01:00
|
|
|
break;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2018-01-27 04:02:56 +01:00
|
|
|
const size_t count(this->len);
|
|
|
|
create_string(serialized(object, object + count), [this, &count]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
|
|
|
json::stringify(buffer, object, object + count);
|
|
|
|
});
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
2018-01-27 04:02:56 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(!array)
|
2018-01-27 04:02:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
const size_t count(this->len);
|
|
|
|
create_string(serialized(array, array + count), [this, &count]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-01-27 04:02:56 +01:00
|
|
|
json::stringify(buffer, array, array + count);
|
|
|
|
});
|
2017-09-14 20:30:06 +02:00
|
|
|
break;
|
2018-01-27 04:02:56 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2017-03-18 04:32:32 +01:00
|
|
|
case STRING:
|
2018-01-27 04:02:56 +01:00
|
|
|
{
|
2018-03-12 18:55:18 +01:00
|
|
|
if(!string)
|
2018-01-27 04:02:56 +01:00
|
|
|
break;
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
create_string(serialized(other), [&other]
|
2018-02-18 23:04:22 +01:00
|
|
|
(mutable_buffer &buffer)
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
2018-03-20 22:51:58 +01:00
|
|
|
json::stringify(buffer, other);
|
2018-01-27 04:02:56 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
|
2017-03-20 23:59:14 +01:00
|
|
|
case LITERAL:
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 00:20:24 +02:00
|
|
|
ircd::json::value &
|
|
|
|
ircd::json::value::operator=(value &&other)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~value();
|
2019-04-25 04:37:23 +02:00
|
|
|
new (this) value(std::move(other));
|
|
|
|
assert(other.alloc == false);
|
2018-04-05 00:20:24 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value &
|
|
|
|
ircd::json::value::operator=(const value &other)
|
|
|
|
{
|
|
|
|
this->~value();
|
|
|
|
new (this) value(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-29 00:09:05 +01:00
|
|
|
[[gnu::hot]]
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::~value()
|
|
|
|
noexcept
|
|
|
|
{
|
2020-03-11 23:12:37 +01:00
|
|
|
if(alloc) switch(serial? STRING : static_cast<enum type>(type))
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
|
|
|
case STRING:
|
|
|
|
delete[] string;
|
|
|
|
break;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
case OBJECT:
|
2017-09-14 20:30:06 +02:00
|
|
|
delete[] object;
|
|
|
|
break;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
|
|
|
case ARRAY:
|
2017-09-14 20:30:06 +02:00
|
|
|
delete[] array;
|
|
|
|
break;
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
default:
|
|
|
|
break;
|
2017-03-16 21:36:18 +01:00
|
|
|
}
|
2017-09-14 20:30:06 +02:00
|
|
|
}
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::operator std::string()
|
|
|
|
const
|
|
|
|
{
|
2017-10-16 06:18:42 +02:00
|
|
|
return json::strung(*this);
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::operator string_view()
|
2017-03-16 21:36:18 +01:00
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-03-18 04:32:32 +01:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case STRING:
|
|
|
|
return unquote(string_view{string, len});
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
case NUMBER:
|
2017-09-14 20:30:06 +02:00
|
|
|
return serial? string_view{string, len}:
|
|
|
|
floats? byte_view<string_view>{floating}:
|
2017-12-25 04:21:49 +01:00
|
|
|
byte_view<string_view>{integer};
|
2017-03-18 04:32:32 +01:00
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
2017-03-20 23:59:14 +01:00
|
|
|
case LITERAL:
|
2017-09-14 20:30:06 +02:00
|
|
|
if(likely(serial))
|
2017-03-18 04:32:32 +01:00
|
|
|
return string_view{string, len};
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"value type[%d] is not a string", int(type)
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 21:29:21 +02:00
|
|
|
ircd::json::value::operator int64_t()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return likely(!floats)? integer : floating;
|
|
|
|
|
|
|
|
case STRING:
|
2017-09-14 20:30:06 +02:00
|
|
|
return lex_cast<int64_t>(string_view{*this});
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
|
|
|
case LITERAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"value type[%d] is not an int64_t", int(type)
|
|
|
|
};
|
2017-09-08 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::value::operator double()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return likely(floats)? floating : integer;
|
|
|
|
|
|
|
|
case STRING:
|
2017-09-14 20:30:06 +02:00
|
|
|
return lex_cast<double>(string_view{*this});
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
case OBJECT:
|
|
|
|
case LITERAL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"value type[%d] is not a float", int(type)
|
|
|
|
};
|
2017-09-08 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
bool
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::value::operator!()
|
2017-09-12 19:02:27 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
2017-09-14 20:30:06 +02:00
|
|
|
return floats? !(floating > 0.0 || floating < 0.0):
|
|
|
|
!bool(integer);
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case STRING:
|
2019-04-25 04:19:06 +02:00
|
|
|
return string? !len || (serial && string_view{string, len} == empty_string):
|
2017-09-14 20:30:06 +02:00
|
|
|
true;
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
2017-09-14 20:30:06 +02:00
|
|
|
return serial? !len || string_view{*this} == empty_object:
|
|
|
|
object? !len:
|
|
|
|
true;
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case ARRAY:
|
2017-09-14 20:30:06 +02:00
|
|
|
return serial? !len || (string_view{*this} == empty_array):
|
|
|
|
array? !len:
|
|
|
|
true;
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case LITERAL:
|
2017-09-14 20:30:06 +02:00
|
|
|
if(serial)
|
|
|
|
return string == nullptr ||
|
|
|
|
string_view{*this} == literal_false ||
|
|
|
|
string_view{*this} == literal_null;
|
2018-04-12 07:57:10 +02:00
|
|
|
else
|
|
|
|
return !bool(integer);
|
2017-09-12 19:02:27 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is falsy is undefined", int(type)
|
|
|
|
};
|
2017-09-12 19:02:27 +02:00
|
|
|
}
|
|
|
|
|
2017-04-03 05:54:35 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::value::empty()
|
2017-04-03 05:54:35 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
|
|
|
return serial? !len:
|
|
|
|
floats? !(floating > 0.0 || floating < 0.0):
|
|
|
|
!bool(integer);
|
|
|
|
|
|
|
|
case STRING:
|
2019-04-25 04:19:06 +02:00
|
|
|
return !string || !len || (serial && string_view{string, len} == empty_string);
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
return serial? !len || string_view{*this} == empty_object:
|
|
|
|
object? !len:
|
|
|
|
true;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
return serial? !len || string_view{*this} == empty_array:
|
2018-02-28 12:12:17 +01:00
|
|
|
array? !len:
|
|
|
|
true;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
return serial? !len:
|
2018-04-12 07:57:10 +02:00
|
|
|
false;
|
2017-04-03 05:54:35 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is empty is undefined", int(type)
|
|
|
|
};
|
2017-04-03 05:54:35 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 19:02:27 +02:00
|
|
|
bool
|
|
|
|
ircd::json::value::null()
|
|
|
|
const
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2017-09-12 19:02:27 +02:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
return floats? !(floating > 0.0 || floating < 0.0):
|
2017-09-14 20:30:06 +02:00
|
|
|
!bool(integer);
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case STRING:
|
2018-04-12 07:57:10 +02:00
|
|
|
return string == nullptr ||
|
|
|
|
string_view{string, len}.null();
|
2017-09-12 19:02:27 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
return serial? string == nullptr:
|
2017-09-14 20:30:06 +02:00
|
|
|
object? false:
|
2017-09-12 19:02:27 +02:00
|
|
|
true;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
return serial? string == nullptr:
|
2018-02-28 12:12:17 +01:00
|
|
|
array? false:
|
2017-09-12 19:02:27 +02:00
|
|
|
true;
|
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
return serial? string == nullptr:
|
2018-04-12 07:57:10 +02:00
|
|
|
string? literal_null == string:
|
|
|
|
false;
|
2017-09-12 19:02:27 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is null is undefined", int(type)
|
|
|
|
};
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
bool
|
|
|
|
ircd::json::value::undefined()
|
|
|
|
const
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(type)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER:
|
2018-05-20 07:10:58 +02:00
|
|
|
return integer == undefined_number;
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case STRING:
|
2018-04-12 07:57:10 +02:00
|
|
|
return string_view{string, len}.undefined();
|
2017-09-14 20:30:06 +02:00
|
|
|
|
|
|
|
case OBJECT:
|
|
|
|
return serial? string == nullptr:
|
|
|
|
object? false:
|
|
|
|
true;
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
return serial? string == nullptr:
|
|
|
|
array? false:
|
|
|
|
true;
|
|
|
|
|
|
|
|
case LITERAL:
|
|
|
|
return serial? string == nullptr:
|
2018-04-12 06:59:47 +02:00
|
|
|
false;
|
2017-03-16 21:36:18 +01:00
|
|
|
};
|
2017-03-18 04:32:32 +01:00
|
|
|
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"deciding if a type[%u] is undefined is undefined", int(type)
|
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
void
|
|
|
|
ircd::json::value::create_string(const size_t &len,
|
|
|
|
const create_string_closure &closure)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
const size_t max
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
len + 1
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-09-09 04:39:42 +02:00
|
|
|
if(unlikely(max > max_string_size))
|
2019-09-10 04:01:50 +02:00
|
|
|
throw print_panic
|
2019-09-09 04:39:42 +02:00
|
|
|
{
|
|
|
|
"Value cannot have string length:%zu which exceeds limit:%zu",
|
|
|
|
max,
|
|
|
|
max_string_size,
|
|
|
|
};
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
std::unique_ptr<char[]> string
|
|
|
|
{
|
|
|
|
new char[max]
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2018-02-18 23:04:22 +01:00
|
|
|
mutable_buffer buffer
|
2017-09-14 20:30:06 +02:00
|
|
|
{
|
|
|
|
string.get(), len
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
closure(buffer);
|
|
|
|
(string.get())[len] = '\0';
|
|
|
|
this->alloc = true;
|
|
|
|
this->serial = true;
|
|
|
|
this->len = len;
|
|
|
|
this->string = string.release();
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2017-08-23 23:32:28 +02:00
|
|
|
ircd::json::operator<(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2019-07-15 19:48:43 +02:00
|
|
|
if(type(a) == type(b)) switch(type(b))
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
assert(!a.serial && !b.serial);
|
|
|
|
assert(a.floats == b.floats);
|
|
|
|
return b.floats?
|
|
|
|
a.floating < b.floating:
|
|
|
|
a.integer < b.integer;
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
case STRING:
|
|
|
|
return static_cast<string_view>(a) < static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot compare type[%u] %s to type[%u] %s",
|
|
|
|
uint(type(a)),
|
|
|
|
reflect(type(a)),
|
|
|
|
uint(type(b)),
|
|
|
|
reflect(type(b)),
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:55:17 +02:00
|
|
|
bool
|
2019-07-15 19:48:43 +02:00
|
|
|
ircd::json::operator==(const value &a, const value &b)
|
2017-03-20 23:59:14 +01:00
|
|
|
{
|
2019-07-30 01:46:15 +02:00
|
|
|
if(a.serial && b.serial)
|
|
|
|
return string_view(a) == string_view(b);
|
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
if(type(a) == type(b)) switch(type(b))
|
|
|
|
{
|
|
|
|
case NUMBER:
|
|
|
|
assert(!a.serial && !b.serial);
|
|
|
|
assert(!a.floats && !b.floats);
|
|
|
|
if(unlikely(a.floats || b.floats))
|
|
|
|
break;
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
return a.integer == b.integer;
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
case STRING:
|
|
|
|
return static_cast<string_view>(a) == static_cast<string_view>(b);
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
2019-07-15 19:48:43 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Cannot compare type[%u] %s to type[%u] %s",
|
|
|
|
uint(type(a)),
|
|
|
|
reflect(type(a)),
|
|
|
|
uint(type(b)),
|
|
|
|
reflect(type(b)),
|
|
|
|
};
|
2017-03-20 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:30:06 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-01-22 19:52:56 +01:00
|
|
|
// json/util.h
|
2017-09-14 20:30:06 +02:00
|
|
|
//
|
2016-11-29 16:23:38 +01:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
2020-05-15 06:14:31 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr validation
|
|
|
|
{
|
|
|
|
value(0) >> ws >> eoi
|
|
|
|
,"validation"
|
|
|
|
};
|
2020-05-15 06:14:31 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr validation_expect
|
|
|
|
{
|
|
|
|
expect[validation]
|
|
|
|
,"valid expectation"
|
|
|
|
};
|
|
|
|
}
|
2020-05-15 06:14:31 +02:00
|
|
|
|
2020-05-19 05:35:02 +02:00
|
|
|
const ircd::string_view
|
|
|
|
ircd::json::literal_null { "null" },
|
|
|
|
ircd::json::literal_true { "true" },
|
|
|
|
ircd::json::literal_false { "false" },
|
|
|
|
ircd::json::empty_string { "\"\"" },
|
|
|
|
ircd::json::empty_object { "{}" },
|
|
|
|
ircd::json::empty_array { "[]" };
|
2019-01-22 19:52:56 +01:00
|
|
|
|
|
|
|
decltype(ircd::json::undefined_number)
|
|
|
|
ircd::json::undefined_number
|
|
|
|
{
|
|
|
|
std::numeric_limits<decltype(ircd::json::undefined_number)>::min()
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
ircd::json::undefined_number != 0
|
|
|
|
);
|
|
|
|
|
2018-03-29 05:47:26 +02:00
|
|
|
std::string
|
|
|
|
ircd::json::why(const string_view &s)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
valid(s);
|
|
|
|
return {};
|
|
|
|
}
|
2019-02-26 19:55:53 +01:00
|
|
|
catch(const std::exception &e)
|
2018-03-29 05:47:26 +02:00
|
|
|
{
|
|
|
|
return e.what();
|
|
|
|
}
|
|
|
|
|
2017-11-26 20:57:46 +01:00
|
|
|
bool
|
|
|
|
ircd::json::valid(const string_view &s,
|
|
|
|
std::nothrow_t)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
const char *start(begin(s)), *const stop(end(s));
|
2022-06-18 06:33:15 +02:00
|
|
|
return parser::parse(start, stop, parser::validation);
|
2017-11-26 20:57:46 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2020-05-15 06:14:31 +02:00
|
|
|
assert(false);
|
2017-11-26 20:57:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::json::valid(const string_view &s)
|
|
|
|
{
|
2020-05-15 06:14:31 +02:00
|
|
|
const char *start(begin(s)), *const stop(end(s));
|
|
|
|
const bool ret
|
2018-03-29 07:05:54 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(start, stop, parser::validation_expect)
|
2018-03-29 07:05:54 +02:00
|
|
|
};
|
|
|
|
|
2020-05-15 06:14:31 +02:00
|
|
|
assert(ret);
|
2017-11-26 20:57:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 06:11:26 +02:00
|
|
|
void
|
|
|
|
ircd::json::valid_output(const string_view &sv,
|
|
|
|
const size_t &expected)
|
|
|
|
{
|
|
|
|
if(unlikely(size(sv) != expected))
|
2019-09-10 04:01:50 +02:00
|
|
|
throw print_panic
|
2018-03-29 06:11:26 +02:00
|
|
|
{
|
2020-02-07 21:45:09 +01:00
|
|
|
"stringified:%zu != serialized:%zu :%s",
|
2018-03-29 06:11:26 +02:00
|
|
|
size(sv),
|
|
|
|
expected,
|
|
|
|
sv
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!valid(sv, std::nothrow))) //note: false alarm when T=json::member
|
2019-09-10 04:01:50 +02:00
|
|
|
throw print_panic
|
2018-03-29 06:11:26 +02:00
|
|
|
{
|
2020-02-07 21:45:09 +01:00
|
|
|
"strung %zu bytes :%s :%s",
|
2018-03-29 06:11:26 +02:00
|
|
|
size(sv),
|
|
|
|
why(sv),
|
|
|
|
sv
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-16 02:25:15 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::json::stringify(mutable_buffer &buf,
|
|
|
|
const string_view &v)
|
|
|
|
{
|
2020-09-13 18:41:05 +02:00
|
|
|
const json::value value(v);
|
|
|
|
if(v.empty() && defined(value))
|
2017-11-16 02:25:15 +01:00
|
|
|
{
|
2018-03-28 01:47:48 +02:00
|
|
|
const char *const start{begin(buf)};
|
2018-01-24 06:18:47 +01:00
|
|
|
consume(buf, copy(buf, empty_string));
|
2018-03-28 01:47:48 +02:00
|
|
|
const string_view ret{start, begin(buf)};
|
|
|
|
assert(serialized(v) == size(ret));
|
|
|
|
return ret;
|
2017-11-16 02:25:15 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 22:51:58 +01:00
|
|
|
return stringify(buf, value);
|
2017-11-16 02:25:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:47:07 +02:00
|
|
|
size_t
|
2018-03-20 22:51:58 +01:00
|
|
|
ircd::json::serialized(const string_view &v)
|
2017-09-08 16:47:07 +02:00
|
|
|
{
|
2020-09-16 01:51:41 +02:00
|
|
|
if(v.empty() && defined(v))
|
|
|
|
return size(empty_string);
|
|
|
|
|
2020-06-11 11:53:42 +02:00
|
|
|
// Query the json::type of the input string here in relaxed mode. The
|
|
|
|
// json::value ctor uses strict_t by default which is a full validation;
|
|
|
|
// we don't care about that for the serialized() suite.
|
|
|
|
const json::value value
|
|
|
|
{
|
2020-09-13 18:41:05 +02:00
|
|
|
v, json::type(v, std::nothrow)
|
2020-06-11 11:53:42 +02:00
|
|
|
};
|
|
|
|
|
2020-09-16 01:51:41 +02:00
|
|
|
return serialized(value);
|
2017-09-08 16:47:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-22 19:52:56 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-05-01 06:40:00 +02:00
|
|
|
// json/type.h
|
2019-01-22 19:52:56 +01:00
|
|
|
//
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
2020-05-15 06:13:02 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr type_parse_is_string
|
|
|
|
{
|
|
|
|
ws >> quote
|
|
|
|
,"is string"
|
|
|
|
};
|
2020-05-25 03:09:00 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr type_parse_is_object
|
|
|
|
{
|
|
|
|
ws >> object_begin
|
|
|
|
,"is object"
|
|
|
|
};
|
2020-05-25 03:35:42 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr type_parse_is_array
|
|
|
|
{
|
|
|
|
ws >> array_begin
|
|
|
|
,"is array"
|
|
|
|
};
|
2020-05-25 03:35:42 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr type_parse_is_number
|
|
|
|
{
|
|
|
|
ws >> number_begin
|
|
|
|
,"is number"
|
|
|
|
};
|
2020-05-25 03:09:00 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const expr type_parse_is_literal
|
|
|
|
{
|
|
|
|
ws >> literal >> ws >> eoi
|
|
|
|
,"is literal"
|
|
|
|
};
|
2020-05-25 03:09:00 +02:00
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<> type_parse_is[]
|
2020-05-25 03:35:42 +02:00
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
{ &type_parse_is_string ,"is string" },
|
|
|
|
{ &type_parse_is_object ,"is object" },
|
|
|
|
{ &type_parse_is_array ,"is array" },
|
|
|
|
{ &type_parse_is_number ,"is number" },
|
|
|
|
{ &type_parse_is_literal ,"is literal" },
|
2020-05-25 03:35:42 +02:00
|
|
|
};
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
const rule<enum json::type> type_parse
|
|
|
|
{
|
|
|
|
(omit[type_parse_is_string] >> attr(json::STRING)) |
|
|
|
|
(omit[type_parse_is_object] >> attr(json::OBJECT)) |
|
|
|
|
(omit[type_parse_is_array] >> attr(json::ARRAY)) |
|
|
|
|
(omit[type_parse_is_number] >> attr(json::NUMBER)) |
|
|
|
|
(omit[type_parse_is_literal] >> attr(json::LITERAL))
|
|
|
|
,"type check"
|
|
|
|
};
|
2020-05-25 03:35:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::type(const string_view &buf,
|
2022-06-18 06:33:15 +02:00
|
|
|
const enum type &type)
|
2020-05-25 03:35:42 +02:00
|
|
|
{
|
|
|
|
const bool ret
|
|
|
|
{
|
2022-06-18 06:33:15 +02:00
|
|
|
parser::parse(begin(buf), end(buf), parser::type_parse_is[type])
|
2020-05-25 03:35:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-29 05:01:56 +01:00
|
|
|
enum ircd::json::type
|
2020-05-25 04:10:59 +02:00
|
|
|
ircd::json::type(const string_view &buf)
|
2019-03-29 05:01:56 +01:00
|
|
|
{
|
|
|
|
enum type ret;
|
2022-06-18 06:33:15 +02:00
|
|
|
if(!parser::parse(begin(buf), end(buf), parser::type_parse, ret))
|
2019-03-29 05:01:56 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Failed to derive JSON value type from input buffer."
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const string_view &buf,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
|
|
|
enum type ret;
|
2022-06-18 06:33:15 +02:00
|
|
|
if(!parser::parse(begin(buf), end(buf), parser::type_parse, ret))
|
2019-03-29 05:01:56 +01:00
|
|
|
return STRING;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-18 06:33:15 +02:00
|
|
|
namespace ircd::json::parser
|
|
|
|
{
|
|
|
|
const expr type_parse_is_string_strict
|
|
|
|
{
|
|
|
|
ws >> "e >> string >> ws >> eoi
|
|
|
|
,"is string"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr type_parse_is_object_strict
|
|
|
|
{
|
|
|
|
ws >> &object_begin >> object(0) >> ws >> eoi
|
|
|
|
,"is object"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr type_parse_is_array_strict
|
|
|
|
{
|
|
|
|
ws >> &array_begin >> array(0) >> ws >> eoi
|
|
|
|
,"is array"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr type_parse_is_number_strict
|
|
|
|
{
|
|
|
|
ws >> &number_begin >> number >> ws >> eoi
|
|
|
|
,"is number"
|
|
|
|
};
|
|
|
|
|
|
|
|
const expr type_parse_is_literal_strict
|
|
|
|
{
|
|
|
|
ws >> literal >> ws >> eoi
|
|
|
|
,"is literal"
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<> type_parse_is_strict[]
|
|
|
|
{
|
|
|
|
{ &type_parse_is_string_strict ,"is string" },
|
|
|
|
{ &type_parse_is_object_strict ,"is object" },
|
|
|
|
{ &type_parse_is_array_strict ,"is array" },
|
|
|
|
{ &type_parse_is_number_strict ,"is number" },
|
|
|
|
{ &type_parse_is_literal_strict ,"is literal" },
|
|
|
|
};
|
|
|
|
|
|
|
|
const rule<enum json::type> type_parse_strict
|
|
|
|
{
|
|
|
|
(omit[type_parse_is_string_strict] >> attr(json::STRING)) |
|
|
|
|
(omit[type_parse_is_object_strict] >> attr(json::OBJECT)) |
|
|
|
|
(omit[type_parse_is_array_strict] >> attr(json::ARRAY)) |
|
|
|
|
(omit[type_parse_is_number_strict] >> attr(json::NUMBER)) |
|
|
|
|
(omit[type_parse_is_literal_strict] >> attr(json::LITERAL))
|
|
|
|
,"type check strict"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::json::type(const string_view &buf,
|
|
|
|
const enum type &type,
|
|
|
|
strict_t)
|
|
|
|
{
|
|
|
|
const bool ret
|
|
|
|
{
|
|
|
|
parser::parse(begin(buf), end(buf), parser::type_parse_is_strict[type])
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:23:38 +01:00
|
|
|
enum ircd::json::type
|
2020-05-25 04:10:59 +02:00
|
|
|
ircd::json::type(const string_view &buf,
|
|
|
|
strict_t)
|
2016-11-29 16:23:38 +01:00
|
|
|
{
|
|
|
|
enum type ret;
|
2022-06-18 06:33:15 +02:00
|
|
|
if(!parser::parse(begin(buf), end(buf), parser::type_parse_strict, ret))
|
2018-05-20 07:11:39 +02:00
|
|
|
throw type_error
|
|
|
|
{
|
2019-01-22 19:52:56 +01:00
|
|
|
"Failed to derive JSON value type from input buffer."
|
2018-05-20 07:11:39 +02:00
|
|
|
};
|
2016-11-29 16:23:38 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-20 23:59:14 +01:00
|
|
|
|
|
|
|
enum ircd::json::type
|
|
|
|
ircd::json::type(const string_view &buf,
|
2020-05-25 04:10:59 +02:00
|
|
|
strict_t,
|
2017-03-20 23:59:14 +01:00
|
|
|
std::nothrow_t)
|
|
|
|
{
|
|
|
|
enum type ret;
|
2022-06-18 06:33:15 +02:00
|
|
|
if(!parser::parse(begin(buf), end(buf), parser::type_parse_strict, ret))
|
2017-03-20 23:59:14 +01:00
|
|
|
return STRING;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-09-08 21:29:21 +02:00
|
|
|
|
|
|
|
ircd::string_view
|
2017-09-14 20:30:06 +02:00
|
|
|
ircd::json::reflect(const enum type &type)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
switch(type)
|
2017-09-08 21:29:21 +02:00
|
|
|
{
|
2017-09-14 20:30:06 +02:00
|
|
|
case NUMBER: return "NUMBER";
|
|
|
|
case OBJECT: return "OBJECT";
|
|
|
|
case ARRAY: return "ARRAY";
|
|
|
|
case LITERAL: return "LITERAL";
|
|
|
|
case STRING: return "STRING";
|
2017-09-12 19:03:06 +02:00
|
|
|
}
|
2017-09-08 21:29:21 +02:00
|
|
|
|
2019-01-22 19:52:56 +01:00
|
|
|
throw type_error
|
|
|
|
{
|
|
|
|
"Unknown type %x", uint(type)
|
|
|
|
};
|
2017-09-08 21:29:21 +02:00
|
|
|
}
|