2018-01-30 19:19:15 +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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JSON_UTIL_H
|
|
|
|
|
2019-02-27 21:01:15 +01:00
|
|
|
// This section contains utilities which are useful other parts of ircd::json
|
|
|
|
// or developers directly. This interface itself (though not its definition)
|
|
|
|
// has little dependence on other headers in ircd::json. In contrast, a suite
|
|
|
|
// of rich developer tools which depend on various assets in ircd::json are
|
|
|
|
// found in tool.h instead.
|
2018-01-30 19:19:15 +01:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
2019-01-22 19:52:56 +01:00
|
|
|
template<size_t SIZE> struct buffer;
|
2018-01-30 19:19:15 +01:00
|
|
|
|
2019-02-27 21:01:15 +01:00
|
|
|
using name_hash_t = size_t;
|
2019-08-15 08:16:38 +02:00
|
|
|
constexpr name_hash_t name_hash(const string_view name) noexcept;
|
|
|
|
constexpr name_hash_t operator ""_(const char *const name, const size_t len) noexcept;
|
2019-01-22 19:52:56 +01:00
|
|
|
|
2020-05-21 18:17:13 +02:00
|
|
|
void valid_output(const string_view &, const size_t &expected);
|
|
|
|
void debug_valid_output(const string_view &, const size_t &expected);
|
|
|
|
|
2018-01-30 19:19:15 +01:00
|
|
|
size_t serialized(const string_view &);
|
|
|
|
string_view stringify(mutable_buffer &, const string_view &);
|
2019-01-22 19:52:56 +01:00
|
|
|
template<class... T> size_t print(const mutable_buffer &buf, T&&... t);
|
2018-01-30 19:19:15 +01:00
|
|
|
|
2018-03-21 07:51:14 +01:00
|
|
|
// Validate JSON - checks if valid JSON (not canonical).
|
2018-01-30 19:19:15 +01:00
|
|
|
bool valid(const string_view &, std::nothrow_t) noexcept;
|
|
|
|
void valid(const string_view &);
|
2018-03-29 05:47:26 +02:00
|
|
|
std::string why(const string_view &);
|
2020-06-24 17:59:26 +02:00
|
|
|
|
|
|
|
struct stats extern stats;
|
2018-01-30 19:19:15 +01:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:59:26 +02:00
|
|
|
/// Statistics counter access; unfortunately these cannot participate as
|
|
|
|
/// ircd::stats items right now.
|
|
|
|
struct ircd::json::stats
|
|
|
|
{
|
|
|
|
uint64_t print_calls {0}, print_cycles {0};
|
|
|
|
uint64_t parse_calls {0}, parse_cycles {0};
|
|
|
|
};
|
|
|
|
|
2019-01-22 19:52:56 +01:00
|
|
|
/// Alternative to `json::strung` which uses a fixed array rather than an
|
|
|
|
/// allocated string as the target.
|
|
|
|
template<size_t SIZE>
|
|
|
|
struct ircd::json::buffer
|
|
|
|
:string_view
|
|
|
|
{
|
|
|
|
std::array<char, SIZE> b;
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
buffer(T&&... t)
|
|
|
|
:string_view{stringify(b, std::forward<T>(t)...)}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Convenience template using the syntax print(mutable_buffer, ...)
|
|
|
|
/// which stringifies with null termination into buffer.
|
|
|
|
///
|
|
|
|
template<class... T>
|
|
|
|
size_t
|
|
|
|
ircd::json::print(const mutable_buffer &buf,
|
|
|
|
T&&... t)
|
|
|
|
{
|
|
|
|
if(unlikely(!size(buf)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mutable_buffer out
|
|
|
|
{
|
|
|
|
data(buf), size(buf) - 1
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto sv
|
|
|
|
{
|
|
|
|
stringify(out, std::forward<T>(t)...)
|
|
|
|
};
|
|
|
|
|
|
|
|
buf[sv.size()] = '\0';
|
2020-05-21 18:17:13 +02:00
|
|
|
debug_valid_output(sv, size(sv)); // no size expectation check
|
2019-01-22 19:52:56 +01:00
|
|
|
return sv.size();
|
|
|
|
}
|
|
|
|
|
2020-05-21 18:17:13 +02:00
|
|
|
extern inline void
|
|
|
|
__attribute__((always_inline, gnu_inline, artificial))
|
|
|
|
ircd::json::debug_valid_output(const string_view &in,
|
|
|
|
const size_t &expected)
|
|
|
|
{
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
valid_output(in, expected);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-30 19:19:15 +01:00
|
|
|
constexpr ircd::json::name_hash_t
|
|
|
|
ircd::json::operator ""_(const char *const text, const size_t len)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-01-30 19:19:15 +01:00
|
|
|
{
|
2018-09-17 05:01:55 +02:00
|
|
|
return name_hash(string_view(text, len));
|
2018-01-30 19:19:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr ircd::json::name_hash_t
|
2018-09-17 05:01:55 +02:00
|
|
|
ircd::json::name_hash(const string_view name)
|
2019-08-15 08:16:38 +02:00
|
|
|
noexcept
|
2018-01-30 19:19:15 +01:00
|
|
|
{
|
|
|
|
return ircd::hash(name);
|
|
|
|
}
|