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-09-17 04:48:25 +02:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
namespace ircd::fmt
|
2018-05-30 18:03:16 +02:00
|
|
|
{
|
|
|
|
using namespace ircd::spirit;
|
|
|
|
|
|
|
|
struct spec;
|
|
|
|
struct specifier;
|
2018-11-28 22:47:33 +01:00
|
|
|
struct bool_specifier extern const bool_specifier;
|
|
|
|
struct char_specifier extern const char_specifier;
|
|
|
|
struct signed_specifier extern const signed_specifier;
|
|
|
|
struct unsigned_specifier extern const unsigned_specifier;
|
|
|
|
struct float_specifier extern const float_specifier;
|
2019-05-02 02:38:11 +02:00
|
|
|
struct hex_uppercase_specifier extern const hex_uppercase_specifier;
|
2018-11-28 22:47:33 +01:00
|
|
|
struct hex_lowercase_specifier extern const hex_lowercase_specifier;
|
|
|
|
struct pointer_specifier extern const pointer_specifier;
|
|
|
|
struct string_specifier extern const string_specifier;
|
2018-05-30 18:03:16 +02:00
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
constexpr char SPECIFIER {'%'};
|
|
|
|
constexpr char SPECIFIER_TERMINATOR {'$'};
|
|
|
|
|
|
|
|
template<class generator>
|
2022-06-14 23:15:53 +02:00
|
|
|
static bool generate_string(char *&out, const size_t &max, generator&&, const arg &val);
|
2022-05-20 20:59:55 +02:00
|
|
|
|
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
2022-06-14 23:15:53 +02:00
|
|
|
static bool generate(mutable_buffer &, gen&&, attr&&...);
|
2022-05-20 20:59:55 +02:00
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class lambda>
|
2022-06-14 23:15:53 +02:00
|
|
|
static bool visit_type(const arg &val, lambda&& closure);
|
2022-05-20 20:59:55 +02:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
static void handle_specifier(mutable_buffer &out, const uint &idx, const spec &, const arg &);
|
|
|
|
}
|
2018-05-30 18:03:16 +02:00
|
|
|
|
2018-11-28 22:34:49 +01:00
|
|
|
/// Structural representation of a format specifier. The parse of each
|
|
|
|
/// specifier in the format string creates one of these.
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("internal")]]
|
|
|
|
ircd::fmt::spec
|
2018-05-30 18:03:16 +02:00
|
|
|
{
|
|
|
|
char sign {'+'};
|
2019-04-03 20:28:02 +02:00
|
|
|
char pad {' '};
|
2018-05-30 18:03:16 +02:00
|
|
|
ushort width {0};
|
2018-09-21 01:59:41 +02:00
|
|
|
ushort precision {0};
|
2018-05-30 18:03:16 +02:00
|
|
|
string_view name;
|
|
|
|
};
|
|
|
|
|
2018-11-28 22:34:49 +01:00
|
|
|
/// Reflects the fmt::spec struct to allow the spirit::qi grammar to directly
|
|
|
|
/// fill in the spec struct.
|
2022-05-20 03:32:16 +02:00
|
|
|
#pragma GCC visibility push(internal)
|
2016-09-17 04:48:25 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
|
|
|
ircd::fmt::spec,
|
2018-11-28 22:34:49 +01:00
|
|
|
( decltype(ircd::fmt::spec::sign), sign )
|
2019-04-03 20:28:02 +02:00
|
|
|
( decltype(ircd::fmt::spec::pad), pad )
|
2018-11-28 22:34:49 +01:00
|
|
|
( decltype(ircd::fmt::spec::width), width )
|
2018-09-21 01:59:41 +02:00
|
|
|
( decltype(ircd::fmt::spec::precision), precision )
|
2018-11-28 22:34:49 +01:00
|
|
|
( decltype(ircd::fmt::spec::name), name )
|
2016-09-17 04:48:25 +02:00
|
|
|
)
|
2022-05-20 03:32:16 +02:00
|
|
|
#pragma GCC visibility pop
|
2016-09-17 04:48:25 +02:00
|
|
|
|
2018-11-28 22:34:49 +01:00
|
|
|
/// The format string parser grammar.
|
2022-06-11 23:14:50 +02:00
|
|
|
namespace ircd::fmt::parser
|
2017-03-18 01:00:41 +01:00
|
|
|
{
|
2022-05-20 03:32:16 +02:00
|
|
|
template<class R = unused_type>
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("internal")]] rule
|
|
|
|
:qi::rule<const char *, R>
|
|
|
|
{
|
|
|
|
using qi::rule<const char *, R>::rule;
|
|
|
|
};
|
2017-03-18 01:00:41 +01:00
|
|
|
|
2022-06-11 23:14:50 +02:00
|
|
|
const expr specsym
|
2018-05-30 18:03:16 +02:00
|
|
|
{
|
|
|
|
lit(SPECIFIER)
|
|
|
|
,"format specifier"
|
|
|
|
};
|
|
|
|
|
2022-06-11 23:14:50 +02:00
|
|
|
const expr specterm
|
2018-05-30 18:03:16 +02:00
|
|
|
{
|
|
|
|
lit(SPECIFIER_TERMINATOR)
|
|
|
|
,"specifier termination"
|
|
|
|
};
|
|
|
|
|
2022-06-11 23:14:50 +02:00
|
|
|
const expr name
|
2017-03-18 01:00:41 +01:00
|
|
|
{
|
2017-03-23 23:00:26 +01:00
|
|
|
raw[repeat(1,14)[char_("A-Za-z")]]
|
2017-03-18 01:00:41 +01:00
|
|
|
,"specifier name"
|
|
|
|
};
|
|
|
|
|
2022-05-20 03:32:16 +02:00
|
|
|
const rule<fmt::spec> spec
|
2017-03-18 01:00:41 +01:00
|
|
|
{
|
2022-05-20 03:32:16 +02:00
|
|
|
(specsym >> !specsym)
|
|
|
|
>> -(char_('+') | char_('-'))
|
|
|
|
>> (-char_('0') | attr(' '))
|
|
|
|
>> -ushort_
|
|
|
|
>> -(lit('.') >> ushort_)
|
|
|
|
>> name
|
|
|
|
>> -specterm,
|
|
|
|
"specifier"
|
|
|
|
};
|
2017-03-18 01:00:41 +01:00
|
|
|
}
|
2018-05-30 18:03:16 +02:00
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
/// A format specifier handler module. This allows a new "%foo" to be defined
|
|
|
|
/// with custom handling by overriding. This abstraction is inserted into a
|
|
|
|
/// mapping key'ed by the supplied names leading to an instance of this.
|
|
|
|
///
|
2022-06-14 23:15:53 +02:00
|
|
|
class [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::specifier
|
2022-05-20 20:59:55 +02:00
|
|
|
{
|
|
|
|
static std::map<string_view, const specifier *, std::less<>> registry;
|
|
|
|
|
|
|
|
std::set<std::string> names;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool operator()(char *&out, const size_t &max, const spec &, const arg &) const = 0;
|
|
|
|
|
|
|
|
specifier(const std::initializer_list<std::string> &names);
|
|
|
|
specifier(const std::string &name);
|
|
|
|
virtual ~specifier() noexcept;
|
|
|
|
|
|
|
|
static bool exists(const string_view &name);
|
|
|
|
static const specifier &at(const string_view &name);
|
|
|
|
};
|
|
|
|
|
2022-06-15 18:36:03 +02:00
|
|
|
[[clang::always_destroy]]
|
2022-05-20 20:59:55 +02:00
|
|
|
decltype(ircd::fmt::specifier::registry)
|
|
|
|
ircd::fmt::specifier::registry;
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::string_specifier
|
2016-09-18 04:35:55 +02:00
|
|
|
:specifier
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
static const std::tuple
|
|
|
|
<
|
|
|
|
const char *,
|
2017-10-16 06:20:25 +02:00
|
|
|
std::string,
|
2017-03-21 03:13:44 +01:00
|
|
|
std::string_view,
|
2017-10-16 06:20:25 +02:00
|
|
|
ircd::string_view,
|
|
|
|
ircd::json::string,
|
|
|
|
ircd::json::object,
|
|
|
|
ircd::json::array
|
2017-03-21 03:13:44 +01:00
|
|
|
>
|
|
|
|
types;
|
2016-09-18 04:35:55 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
2016-09-18 04:35:55 +02:00
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::string_specifier
|
2016-09-18 04:35:55 +02:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
"s"s
|
2016-09-18 04:35:55 +02:00
|
|
|
};
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
decltype(ircd::fmt::string_specifier::types)
|
|
|
|
ircd::fmt::string_specifier::types;
|
2016-09-17 08:39:44 +02:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::bool_specifier
|
2017-04-03 05:53:14 +02:00
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
static const std::tuple
|
|
|
|
<
|
|
|
|
bool,
|
2018-01-11 03:34:16 +01:00
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
long long, unsigned long long
|
2017-04-03 05:53:14 +02:00
|
|
|
>
|
|
|
|
types;
|
|
|
|
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::bool_specifier
|
2017-04-03 05:53:14 +02:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
{ "b"s }
|
2017-04-03 05:53:14 +02:00
|
|
|
};
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
decltype(ircd::fmt::bool_specifier::types)
|
|
|
|
ircd::fmt::bool_specifier::types;
|
2017-04-03 05:53:14 +02:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::signed_specifier
|
2016-09-17 08:39:44 +02:00
|
|
|
:specifier
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
static const std::tuple
|
|
|
|
<
|
2017-04-03 05:53:14 +02:00
|
|
|
bool,
|
2018-01-11 03:34:16 +01:00
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
long long, unsigned long long
|
2017-03-21 03:13:44 +01:00
|
|
|
>
|
|
|
|
types;
|
2016-09-17 08:39:44 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
2016-09-17 08:39:44 +02:00
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::signed_specifier
|
2016-09-17 04:48:25 +02:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
{ "d"s, "ld"s, "zd"s }
|
2016-09-17 04:48:25 +02:00
|
|
|
};
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
decltype(ircd::fmt::signed_specifier::types)
|
|
|
|
ircd::fmt::signed_specifier::types;
|
2016-09-18 01:55:21 +02:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::unsigned_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
:specifier
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
static const std::tuple
|
|
|
|
<
|
2017-04-03 05:53:14 +02:00
|
|
|
bool,
|
2018-01-11 03:34:16 +01:00
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
long long, unsigned long long
|
2017-03-21 03:13:44 +01:00
|
|
|
>
|
|
|
|
types;
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::unsigned_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
{ "u"s, "lu"s, "zu"s }
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::hex_lowercase_specifier
|
2017-04-07 03:34:44 +02:00
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
static const std::tuple
|
|
|
|
<
|
|
|
|
bool,
|
2018-01-11 03:34:16 +01:00
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
long long, unsigned long long
|
2017-04-07 03:34:44 +02:00
|
|
|
>
|
|
|
|
types;
|
|
|
|
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::hex_lowercase_specifier
|
2017-04-07 03:34:44 +02:00
|
|
|
{
|
|
|
|
{ "x"s, "lx"s }
|
|
|
|
};
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
decltype(ircd::fmt::hex_lowercase_specifier::types)
|
|
|
|
ircd::fmt::hex_lowercase_specifier::types;
|
2017-04-07 03:34:44 +02:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::hex_uppercase_specifier
|
2019-05-02 02:38:11 +02:00
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
static const std::tuple
|
|
|
|
<
|
|
|
|
bool,
|
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
long long, unsigned long long
|
|
|
|
>
|
|
|
|
types;
|
|
|
|
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
|
|
|
const ircd::fmt::hex_uppercase_specifier
|
|
|
|
{
|
|
|
|
{ "X"s, "lX"s }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fmt::hex_uppercase_specifier::types)
|
|
|
|
ircd::fmt::hex_uppercase_specifier::types;
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
decltype(ircd::fmt::unsigned_specifier::types)
|
|
|
|
ircd::fmt::unsigned_specifier::types;
|
2017-03-21 03:13:44 +01:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::float_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
:specifier
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
static const std::tuple
|
|
|
|
<
|
2018-09-21 01:59:41 +02:00
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
float, double,
|
|
|
|
long double
|
2017-03-21 03:13:44 +01:00
|
|
|
>
|
|
|
|
types;
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::float_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
{ "f"s, "lf"s }
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
decltype(ircd::fmt::float_specifier::types)
|
|
|
|
ircd::fmt::float_specifier::types;
|
2017-03-21 03:13:44 +01:00
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::char_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::char_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
"c"s
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fmt::pointer_specifier
|
2017-03-18 05:03:15 +01:00
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
2018-11-28 22:47:33 +01:00
|
|
|
const ircd::fmt::pointer_specifier
|
2017-03-18 05:03:15 +01:00
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
"p"s
|
2017-03-18 05:03:15 +01:00
|
|
|
};
|
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
//
|
|
|
|
// snprintf::snprintf
|
|
|
|
//
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
[[gnu::visibility("protected")]]
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::snprintf::snprintf(internal_t,
|
|
|
|
const mutable_buffer &out,
|
|
|
|
const string_view &fmt,
|
|
|
|
const va_rtti &v)
|
2016-09-18 00:43:10 +02:00
|
|
|
try
|
2018-11-28 22:34:49 +01:00
|
|
|
:out{out}
|
|
|
|
,fmt{[&fmt]
|
|
|
|
{
|
|
|
|
// start the member fmt variable at the first specifier (or end)
|
|
|
|
const auto pos(fmt.find(SPECIFIER));
|
|
|
|
return pos != fmt.npos?
|
|
|
|
fmt.substr(pos):
|
|
|
|
string_view{};
|
|
|
|
}()}
|
2016-09-18 10:15:40 +02:00
|
|
|
,idx{0}
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2018-11-28 22:34:49 +01:00
|
|
|
// If out has no size we have nothing to do, not even null terminate it.
|
|
|
|
if(unlikely(empty(out)))
|
2016-09-18 10:15:40 +02:00
|
|
|
return;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2018-11-28 22:34:49 +01:00
|
|
|
// If fmt has no specifiers then we can just copy the fmt as best as
|
|
|
|
// possible to the out buffer.
|
|
|
|
if(empty(this->fmt))
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2018-12-15 00:51:12 +01:00
|
|
|
append(fmt);
|
2016-09-18 10:15:40 +02:00
|
|
|
return;
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:34:49 +01:00
|
|
|
// Copy everything from fmt up to the first specifier.
|
|
|
|
assert(data(this->fmt) >= data(fmt));
|
2018-12-15 00:51:12 +01:00
|
|
|
append(string_view(data(fmt), data(this->fmt)));
|
2018-11-28 22:34:49 +01:00
|
|
|
|
|
|
|
// Iterate
|
2017-03-18 03:58:17 +01:00
|
|
|
auto it(begin(v));
|
2018-05-20 04:25:41 +02:00
|
|
|
for(size_t i(0); i < v.size() && !finished(); ++it, i++)
|
2017-03-18 03:58:17 +01:00
|
|
|
{
|
2018-11-28 22:34:49 +01:00
|
|
|
const void *const &ptr(get<0>(*it));
|
|
|
|
const std::type_index type(*get<1>(*it));
|
|
|
|
argument(std::make_tuple(ptr, type));
|
2017-03-18 03:58:17 +01:00
|
|
|
}
|
2018-12-16 02:46:35 +01:00
|
|
|
|
|
|
|
// Ensure null termination if out buffer is non-empty.
|
|
|
|
assert(size(this->out) > 0);
|
|
|
|
assert(this->out.remaining());
|
2020-04-19 12:16:43 +02:00
|
|
|
copy(mutable_buffer(this->out), '\0');
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2018-01-19 05:19:38 +01:00
|
|
|
throw invalid_format
|
|
|
|
{
|
|
|
|
"Format string requires more than %zu arguments.", v.size()
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2016-09-18 10:15:40 +02:00
|
|
|
void
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::snprintf::argument(const arg &val)
|
2016-09-18 10:15:40 +02:00
|
|
|
{
|
2018-11-28 22:34:49 +01:00
|
|
|
// The format string's front pointer is sitting on the specifier '%'
|
|
|
|
// waiting to be parsed now.
|
2016-09-18 10:15:40 +02:00
|
|
|
fmt::spec spec;
|
2020-06-05 04:53:26 +02:00
|
|
|
auto &start(begin(this->fmt));
|
2022-06-11 23:14:50 +02:00
|
|
|
const auto &stop(end(this->fmt));
|
|
|
|
if(ircd::parse<invalid_format>(start, stop, parser::spec, spec))
|
2018-11-28 22:34:49 +01:00
|
|
|
handle_specifier(this->out, idx++, spec, val);
|
|
|
|
|
2019-06-01 11:30:29 +02:00
|
|
|
string_view fmt
|
|
|
|
{
|
|
|
|
start, stop
|
|
|
|
};
|
|
|
|
|
|
|
|
if(size(fmt) >= 2 && fmt[0] == SPECIFIER && fmt[1] == SPECIFIER)
|
|
|
|
{
|
|
|
|
append({&SPECIFIER, 1});
|
2020-06-05 04:53:26 +02:00
|
|
|
consume(this->fmt, 2);
|
2019-06-01 11:30:29 +02:00
|
|
|
fmt = string_view
|
|
|
|
{
|
2020-06-05 04:53:26 +02:00
|
|
|
start, stop
|
2019-06-01 11:30:29 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-05 04:53:26 +02:00
|
|
|
const auto nextpos
|
2018-11-28 22:34:49 +01:00
|
|
|
{
|
2020-06-05 04:53:26 +02:00
|
|
|
fmt.find(SPECIFIER)
|
2018-11-28 22:34:49 +01:00
|
|
|
};
|
2020-06-05 04:53:26 +02:00
|
|
|
|
|
|
|
const string_view leg
|
|
|
|
{
|
|
|
|
fmt.substr(0, nextpos)
|
|
|
|
};
|
|
|
|
|
|
|
|
append(leg);
|
|
|
|
consume(this->fmt, size(leg));
|
2016-09-18 10:15:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2016-09-18 10:15:40 +02:00
|
|
|
void
|
2018-12-15 00:51:12 +01:00
|
|
|
ircd::fmt::snprintf::append(const string_view &src)
|
2016-09-18 10:15:40 +02:00
|
|
|
{
|
2018-12-15 00:51:12 +01:00
|
|
|
out([&src](const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return strlcpy(buf, src);
|
|
|
|
});
|
2018-05-20 04:25:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2018-05-20 04:25:41 +02:00
|
|
|
size_t
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::snprintf::remaining()
|
2022-06-14 23:15:53 +02:00
|
|
|
const noexcept
|
2018-05-20 04:25:41 +02:00
|
|
|
{
|
2018-11-28 22:34:49 +01:00
|
|
|
return out.remaining()?
|
|
|
|
out.remaining() - 1:
|
|
|
|
0;
|
2018-05-20 04:25:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2018-05-20 04:25:41 +02:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::snprintf::finished()
|
2022-06-14 23:15:53 +02:00
|
|
|
const noexcept
|
2018-05-20 04:25:41 +02:00
|
|
|
{
|
2018-11-28 22:34:49 +01:00
|
|
|
return empty(fmt) || !remaining();
|
2016-09-18 10:15:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
//
|
|
|
|
// fmt::specifier
|
|
|
|
//
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::specifier::specifier(const std::string &name)
|
2016-09-18 00:43:10 +02:00
|
|
|
:specifier{{name}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::specifier::specifier(const std::initializer_list<std::string> &names)
|
2016-09-18 00:43:10 +02:00
|
|
|
:names{names}
|
2016-09-17 08:39:44 +02:00
|
|
|
{
|
2016-09-18 00:43:10 +02:00
|
|
|
for(const auto &name : this->names)
|
2022-05-20 20:59:55 +02:00
|
|
|
if(exists(name))
|
2018-01-19 05:19:38 +01:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Specifier '%s' already registered\n", name
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
|
|
|
for(const auto &name : this->names)
|
2022-05-20 20:59:55 +02:00
|
|
|
registry.emplace(name, this);
|
2016-09-17 08:39:44 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::specifier::~specifier()
|
2016-09-17 08:39:44 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2016-09-18 00:43:10 +02:00
|
|
|
for(const auto &name : names)
|
2022-05-20 20:59:55 +02:00
|
|
|
registry.erase(name);
|
2016-09-17 08:39:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 04:48:25 +02:00
|
|
|
bool
|
2022-05-20 20:59:55 +02:00
|
|
|
ircd::fmt::specifier::exists(const string_view &name)
|
|
|
|
{
|
|
|
|
return registry.count(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::fmt::specifier &
|
|
|
|
ircd::fmt::specifier::at(const string_view &name)
|
2016-09-17 04:48:25 +02:00
|
|
|
{
|
2022-05-20 20:59:55 +02:00
|
|
|
return *registry.at(name);
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
//
|
|
|
|
// Utils
|
|
|
|
//
|
|
|
|
|
2016-09-17 04:48:25 +02:00
|
|
|
void
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::handle_specifier(mutable_buffer &out,
|
|
|
|
const uint &idx,
|
|
|
|
const spec &spec,
|
|
|
|
const arg &val)
|
2016-09-17 04:48:25 +02:00
|
|
|
try
|
|
|
|
{
|
2018-11-28 22:34:49 +01:00
|
|
|
auto &outp(std::get<0>(out));
|
2018-12-16 02:46:35 +01:00
|
|
|
assert(size(out));
|
|
|
|
const size_t max
|
|
|
|
{
|
|
|
|
size(out) - 1 // Leave room for null byte for later.
|
|
|
|
};
|
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
assert(spec.name);
|
|
|
|
const auto &type(get<1>(val));
|
|
|
|
const auto &handler(specifier::at(spec.name));
|
2018-11-28 22:34:49 +01:00
|
|
|
if(unlikely(!handler(outp, max, spec, val)))
|
2018-01-19 05:19:38 +01:00
|
|
|
throw invalid_type
|
|
|
|
{
|
|
|
|
"`%s' (%s) for format specifier '%s' for argument #%u",
|
|
|
|
demangle(type.name()),
|
|
|
|
type.name(),
|
|
|
|
spec.name,
|
|
|
|
idx
|
|
|
|
};
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
2018-01-19 05:19:38 +01:00
|
|
|
throw invalid_format
|
|
|
|
{
|
|
|
|
"Unhandled specifier `%s' for argument #%u in format string",
|
|
|
|
spec.name,
|
|
|
|
idx
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
catch(const illegal &e)
|
|
|
|
{
|
2018-01-19 05:19:38 +01:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Specifier `%s' for argument #%u: %s",
|
|
|
|
spec.name,
|
|
|
|
idx,
|
|
|
|
e.what()
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
template<class T,
|
|
|
|
class lambda>
|
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::visit_type(const arg &val,
|
2018-12-15 00:51:12 +01:00
|
|
|
lambda&& closure)
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
|
|
|
const auto &ptr(get<0>(val));
|
|
|
|
const auto &type(get<1>(val));
|
2017-03-25 00:19:12 +01:00
|
|
|
return type == typeid(T)? closure(*static_cast<const T *>(ptr)) : false;
|
2017-03-21 03:13:44 +01:00
|
|
|
}
|
|
|
|
|
2020-06-05 18:23:38 +02:00
|
|
|
template<class gen,
|
|
|
|
class... attr>
|
|
|
|
bool
|
|
|
|
ircd::fmt::generate(mutable_buffer &out,
|
|
|
|
gen&& g,
|
|
|
|
attr&&... a)
|
|
|
|
{
|
|
|
|
constexpr bool truncation
|
|
|
|
{
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
return ircd::generate<truncation>(out, std::forward<gen>(g), std::forward<attr>(a)...);
|
|
|
|
}
|
|
|
|
|
2022-05-20 20:59:55 +02:00
|
|
|
//
|
|
|
|
// Handlers
|
|
|
|
//
|
|
|
|
|
2017-03-18 05:03:15 +01:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::pointer_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
2019-04-03 20:28:02 +02:00
|
|
|
const spec &spec,
|
2018-11-28 22:47:33 +01:00
|
|
|
const arg &val)
|
2017-03-18 05:03:15 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::eps;
|
|
|
|
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2017-03-18 05:03:15 +01:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Not a pointer"
|
|
|
|
};
|
|
|
|
}};
|
2017-03-18 05:03:15 +01:00
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, uintptr_t()>
|
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, uintptr_t()> rule
|
2017-03-18 05:03:15 +01:00
|
|
|
{
|
|
|
|
lit("0x") << karma::hex
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
_r1_type width;
|
|
|
|
_r2_type pad;
|
|
|
|
karma::rule<char *, uintptr_t(ushort, char)> aligned_left
|
|
|
|
{
|
|
|
|
karma::left_align(width, pad)[rule]
|
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
karma::rule<char *, uintptr_t(ushort, char)> aligned_right
|
|
|
|
{
|
|
|
|
karma::right_align(width, pad)[rule]
|
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
karma::rule<char *, uintptr_t(ushort, char)> aligned_center
|
|
|
|
{
|
|
|
|
karma::center(width, pad)[rule]
|
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{rule} {}
|
2017-03-18 05:03:15 +01:00
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
static const auto &ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
|
|
|
|
2017-03-18 05:03:15 +01:00
|
|
|
const auto &ptr(get<0>(val));
|
|
|
|
const auto &type(get<1>(val));
|
2019-04-03 20:28:02 +02:00
|
|
|
const void *const p
|
|
|
|
{
|
|
|
|
*static_cast<const void *const *>(ptr)
|
|
|
|
};
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
bool ret;
|
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
2019-04-03 20:28:02 +02:00
|
|
|
|
|
|
|
if(!spec.width)
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, generator | ep, uintptr_t(p));
|
2019-04-03 20:28:02 +02:00
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
else if(spec.sign == '-')
|
2019-04-03 20:28:02 +02:00
|
|
|
{
|
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, uintptr_t(p));
|
2020-05-26 12:29:59 +02:00
|
|
|
} else {
|
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, uintptr_t(p));
|
2019-04-03 20:28:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2017-03-18 05:03:15 +01:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::char_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &,
|
|
|
|
const arg &val)
|
2016-09-18 00:43:10 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::eps;
|
|
|
|
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Not a printable character"
|
|
|
|
};
|
|
|
|
}};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
|
|
|
struct generator
|
2017-03-21 03:13:44 +01:00
|
|
|
:karma::grammar<char *, char()>
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
|
|
|
karma::rule<char *, char()> printable
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
karma::print
|
|
|
|
,"character"
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
generator(): generator::base_type{printable} {}
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
|
|
|
const auto &ptr(get<0>(val));
|
|
|
|
const auto &type(get<1>(val));
|
|
|
|
if(type == typeid(const char))
|
|
|
|
{
|
2020-05-26 12:29:59 +02:00
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
|
|
|
|
2017-03-25 00:19:12 +01:00
|
|
|
const auto &c(*static_cast<const char *>(ptr));
|
2020-06-05 18:23:38 +02:00
|
|
|
fmt::generate(buf, generator | eps[throw_illegal], c);
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
2016-09-18 00:43:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2017-04-03 05:53:14 +02:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::bool_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &,
|
|
|
|
const arg &val)
|
2017-04-03 05:53:14 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::eps;
|
|
|
|
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2017-04-03 05:53:14 +02:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Failed to print signed value"
|
|
|
|
};
|
|
|
|
}};
|
2017-04-03 05:53:14 +02:00
|
|
|
|
|
|
|
const auto closure([&](const bool &boolean)
|
|
|
|
{
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, bool()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, bool()> rule
|
|
|
|
{
|
|
|
|
karma::bool_
|
|
|
|
,"boolean"
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
2020-06-05 18:23:38 +02:00
|
|
|
fmt::generate(buf, generator | eps[throw_illegal], boolean)
|
2020-05-26 12:29:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2017-04-03 05:53:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::signed_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &spec,
|
|
|
|
const arg &val)
|
2016-09-18 00:43:10 +02:00
|
|
|
const
|
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Failed to print signed value"
|
|
|
|
};
|
|
|
|
}};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2018-05-31 19:23:29 +02:00
|
|
|
const auto closure([&out, &max, &spec, &val]
|
|
|
|
(const long &integer)
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
|
|
|
using karma::long_;
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, long()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, long()> rule
|
|
|
|
{
|
|
|
|
long_
|
|
|
|
,"signed long integer"
|
|
|
|
};
|
|
|
|
|
2018-05-31 19:23:29 +02:00
|
|
|
_r1_type width;
|
2019-04-03 20:28:02 +02:00
|
|
|
_r2_type pad;
|
|
|
|
karma::rule<char *, long(ushort, char)> aligned_left
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::left_align(width, pad)[rule]
|
2018-05-31 19:23:29 +02:00
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, long(ushort, char)> aligned_right
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::right_align(width, pad)[rule]
|
2018-05-31 19:23:29 +02:00
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, long(ushort, char)> aligned_center
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::center(width, pad)[rule]
|
2018-05-31 19:23:29 +02:00
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
static const auto &ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
2018-05-31 19:23:29 +02:00
|
|
|
|
|
|
|
if(!spec.width)
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, generator | ep, integer);
|
2018-05-31 19:23:29 +02:00
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
else if(spec.sign == '-')
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2020-05-26 12:29:59 +02:00
|
|
|
} else {
|
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2018-05-31 19:23:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2017-03-21 03:13:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::unsigned_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &spec,
|
|
|
|
const arg &val)
|
2017-03-21 03:13:44 +01:00
|
|
|
const
|
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Failed to print unsigned value"
|
|
|
|
};
|
|
|
|
}};
|
2017-03-21 03:13:44 +01:00
|
|
|
|
2018-05-31 19:23:29 +02:00
|
|
|
const auto closure([&out, &max, &spec, &val]
|
|
|
|
(const ulong &integer)
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
|
|
|
using karma::ulong_;
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, ulong()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, ulong()> rule
|
|
|
|
{
|
|
|
|
ulong_
|
|
|
|
,"unsigned long integer"
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2018-05-31 19:23:29 +02:00
|
|
|
_r1_type width;
|
2019-04-03 20:28:02 +02:00
|
|
|
_r2_type pad;
|
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_left
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::left_align(width, pad)[rule]
|
2018-05-31 19:23:29 +02:00
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_right
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::right_align(width, pad)[rule]
|
2018-05-31 19:23:29 +02:00
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_center
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::center(width, pad)[rule]
|
2018-05-31 19:23:29 +02:00
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
static const auto &ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
2018-05-31 19:23:29 +02:00
|
|
|
|
|
|
|
if(!spec.width)
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, generator | ep, integer);
|
2018-05-31 19:23:29 +02:00
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
else if(spec.sign == '-')
|
2018-05-31 19:23:29 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2020-05-26 12:29:59 +02:00
|
|
|
} else {
|
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2018-05-31 19:23:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2017-03-21 03:13:44 +01:00
|
|
|
});
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-04-07 03:34:44 +02:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::hex_lowercase_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
2019-04-03 19:53:53 +02:00
|
|
|
const spec &spec,
|
2018-11-28 22:47:33 +01:00
|
|
|
const arg &val)
|
2017-04-07 03:34:44 +02:00
|
|
|
const
|
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2017-04-07 03:34:44 +02:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Failed to print hexadecimal value"
|
|
|
|
};
|
|
|
|
}};
|
2017-04-07 03:34:44 +02:00
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
const auto closure([&](const ulong &integer)
|
2017-04-07 03:34:44 +02:00
|
|
|
{
|
|
|
|
struct generator
|
2019-04-03 20:28:02 +02:00
|
|
|
:karma::grammar<char *, ulong()>
|
2017-04-07 03:34:44 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, ulong()> rule
|
2017-04-07 03:34:44 +02:00
|
|
|
{
|
|
|
|
karma::lower[karma::hex]
|
|
|
|
,"unsigned lowercase hexadecimal"
|
|
|
|
};
|
|
|
|
|
2019-04-03 19:53:53 +02:00
|
|
|
_r1_type width;
|
2019-04-03 20:28:02 +02:00
|
|
|
_r2_type pad;
|
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_left
|
2019-04-03 19:53:53 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::left_align(width, pad)[rule]
|
2019-05-02 02:38:11 +02:00
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_right
|
|
|
|
{
|
|
|
|
karma::right_align(width, pad)[rule]
|
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_center
|
|
|
|
{
|
|
|
|
karma::center(width, pad)[rule]
|
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
static const auto &ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
2019-05-02 02:38:11 +02:00
|
|
|
|
|
|
|
if(!spec.width)
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, generator | ep, integer);
|
2019-05-02 02:38:11 +02:00
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
else if(spec.sign == '-')
|
2019-05-02 02:38:11 +02:00
|
|
|
{
|
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2020-05-26 12:29:59 +02:00
|
|
|
} else {
|
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2019-05-02 02:38:11 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2019-05-02 02:38:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::fmt::hex_uppercase_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &spec,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2019-05-02 02:38:11 +02:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Failed to print hexadecimal value"
|
|
|
|
};
|
|
|
|
}};
|
2019-05-02 02:38:11 +02:00
|
|
|
|
|
|
|
const auto closure([&](const ulong &integer)
|
|
|
|
{
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, ulong()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, ulong()> rule
|
|
|
|
{
|
|
|
|
karma::upper[karma::hex]
|
|
|
|
,"unsigned uppercase hexadecimal"
|
|
|
|
};
|
|
|
|
|
|
|
|
_r1_type width;
|
|
|
|
_r2_type pad;
|
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_left
|
|
|
|
{
|
|
|
|
karma::left_align(width, pad)[rule]
|
2019-04-03 19:53:53 +02:00
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_right
|
2019-04-03 19:53:53 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::right_align(width, pad)[rule]
|
2019-04-03 19:53:53 +02:00
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::rule<char *, ulong(ushort, char)> aligned_center
|
2019-04-03 19:53:53 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::center(width, pad)[rule]
|
2019-04-03 19:53:53 +02:00
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
2017-04-07 03:34:44 +02:00
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
static const auto &ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
2019-04-03 19:53:53 +02:00
|
|
|
|
|
|
|
if(!spec.width)
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, generator | ep, integer);
|
2019-04-03 19:53:53 +02:00
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
else if(spec.sign == '-')
|
2019-04-03 19:53:53 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2020-05-26 12:29:59 +02:00
|
|
|
} else {
|
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, g | ep, integer);
|
2019-04-03 19:53:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2017-04-07 03:34:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-01 03:07:16 +01:00
|
|
|
//TODO: note long double is narrowed to double for now otherwise
|
|
|
|
//TODO: valgrind loops somewhere in here and eats all the system's RAM.
|
2017-03-21 03:13:44 +01:00
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::float_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
2021-09-03 03:29:24 +02:00
|
|
|
const spec &spec,
|
2018-11-28 22:47:33 +01:00
|
|
|
const arg &val)
|
2017-03-21 03:13:44 +01:00
|
|
|
const
|
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Failed to print floating point value"
|
|
|
|
};
|
|
|
|
}};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2018-09-21 01:59:41 +02:00
|
|
|
thread_local uint _precision_;
|
2021-09-03 03:29:24 +02:00
|
|
|
_precision_ = spec.precision;
|
2018-09-21 01:59:41 +02:00
|
|
|
|
2019-03-01 03:07:16 +01:00
|
|
|
const auto closure([&](const double &floating)
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
|
|
|
using karma::double_;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
struct generator
|
2019-03-01 03:07:16 +01:00
|
|
|
:karma::grammar<char *, double()>
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
2018-09-21 01:59:41 +02:00
|
|
|
struct policy
|
2019-03-01 03:07:16 +01:00
|
|
|
:karma::real_policies<double>
|
2018-09-21 01:59:41 +02:00
|
|
|
{
|
2019-03-01 03:07:16 +01:00
|
|
|
static uint precision(const double &)
|
2018-09-21 01:59:41 +02:00
|
|
|
{
|
|
|
|
return _precision_;
|
|
|
|
}
|
2018-09-25 08:03:13 +02:00
|
|
|
|
2019-03-01 03:07:16 +01:00
|
|
|
static bool trailing_zeros(const double &)
|
2018-09-25 08:03:13 +02:00
|
|
|
{
|
|
|
|
return _precision_ > 0;
|
|
|
|
}
|
2022-04-02 22:31:29 +02:00
|
|
|
|
|
|
|
static int floatfield(const double &)
|
|
|
|
{
|
|
|
|
return _precision_ > 0?
|
|
|
|
fmtflags::fixed:
|
|
|
|
fmtflags::scientific;
|
|
|
|
}
|
2018-09-21 01:59:41 +02:00
|
|
|
};
|
|
|
|
|
2019-03-01 03:07:16 +01:00
|
|
|
karma::rule<char *, double()> rule
|
2017-03-21 03:13:44 +01:00
|
|
|
{
|
2019-03-01 03:07:16 +01:00
|
|
|
karma::real_generator<double, policy>()
|
2018-09-21 01:59:41 +02:00
|
|
|
,"floating point real"
|
2017-03-21 03:13:44 +01:00
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2021-09-03 03:29:24 +02:00
|
|
|
_r1_type width;
|
|
|
|
_r2_type pad;
|
|
|
|
karma::rule<char *, double(ushort, char)> aligned_left
|
|
|
|
{
|
|
|
|
karma::left_align(width, pad)[rule]
|
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
karma::rule<char *, double(ushort, char)> aligned_right
|
|
|
|
{
|
|
|
|
karma::right_align(width, pad)[rule]
|
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
|
|
|
karma::rule<char *, double(ushort, char)> aligned_center
|
|
|
|
{
|
|
|
|
karma::center(width, pad)[rule]
|
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2021-09-03 03:29:24 +02:00
|
|
|
static const auto ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
2020-05-26 12:29:59 +02:00
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
|
|
|
|
2021-09-03 03:29:24 +02:00
|
|
|
if(!spec.width)
|
|
|
|
ret = fmt::generate(buf, generator | ep, floating);
|
|
|
|
|
|
|
|
else if(spec.sign == '-')
|
2020-05-26 12:29:59 +02:00
|
|
|
{
|
2021-09-03 03:29:24 +02:00
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
|
|
|
ret = fmt::generate(buf, g | ep, floating);
|
|
|
|
} else {
|
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
|
|
|
ret = fmt::generate(buf, g | ep, floating);
|
|
|
|
}
|
2020-05-26 12:29:59 +02:00
|
|
|
|
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2017-03-21 03:13:44 +01:00
|
|
|
});
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
2016-09-18 00:43:10 +02:00
|
|
|
|
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::string_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &spec,
|
|
|
|
const arg &val)
|
2016-09-18 00:43:10 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::char_;
|
|
|
|
using karma::eps;
|
2017-03-18 01:00:41 +01:00
|
|
|
using karma::unused_type;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2020-05-19 05:35:02 +02:00
|
|
|
static const auto throw_illegal{[]
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2020-05-19 05:35:02 +02:00
|
|
|
throw illegal
|
|
|
|
{
|
|
|
|
"Not a printable string"
|
|
|
|
};
|
|
|
|
}};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
|
|
|
struct generator
|
2020-02-27 20:35:14 +01:00
|
|
|
:karma::grammar<char *, string_view>
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2020-02-27 20:35:14 +01:00
|
|
|
karma::rule<char *, string_view> string
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2020-10-21 06:46:24 +02:00
|
|
|
*(~ascii::cntrl)
|
2017-03-18 03:58:17 +01:00
|
|
|
,"string"
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
2018-05-31 19:00:46 +02:00
|
|
|
_r1_type width;
|
2019-04-03 20:28:02 +02:00
|
|
|
_r2_type pad;
|
2020-02-27 20:35:14 +01:00
|
|
|
karma::rule<char *, string_view (ushort, char)> aligned_left
|
2018-05-31 19:00:46 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::left_align(width, pad)[string]
|
2018-05-31 19:00:46 +02:00
|
|
|
,"left aligned"
|
|
|
|
};
|
|
|
|
|
2020-02-27 20:35:14 +01:00
|
|
|
karma::rule<char *, string_view (ushort, char)> aligned_right
|
2018-05-31 19:00:46 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::right_align(width, pad)[string]
|
2018-05-31 19:00:46 +02:00
|
|
|
,"right aligned"
|
|
|
|
};
|
|
|
|
|
2020-02-27 20:35:14 +01:00
|
|
|
karma::rule<char *, string_view (ushort, char)> aligned_center
|
2018-05-31 19:00:46 +02:00
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
karma::center(width, pad)[string]
|
2018-05-31 19:00:46 +02:00
|
|
|
,"center aligned"
|
|
|
|
};
|
|
|
|
|
2017-03-18 03:58:17 +01:00
|
|
|
generator() :generator::base_type{string} {}
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
static const generator;
|
2017-03-18 01:00:41 +01:00
|
|
|
|
2020-02-22 01:24:45 +01:00
|
|
|
static const auto ep
|
|
|
|
{
|
|
|
|
eps[throw_illegal]
|
|
|
|
};
|
2018-05-31 19:00:46 +02:00
|
|
|
|
|
|
|
if(!spec.width)
|
2020-05-26 12:29:59 +02:00
|
|
|
return generate_string(out, max, generator | ep, val);
|
2018-05-31 19:00:46 +02:00
|
|
|
|
|
|
|
if(spec.sign == '-')
|
|
|
|
{
|
2019-04-03 20:28:02 +02:00
|
|
|
const auto &g(generator.aligned_left(spec.width, spec.pad));
|
2020-05-26 12:29:59 +02:00
|
|
|
return generate_string(out, max, g | ep, val);
|
2018-05-31 19:00:46 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:28:02 +02:00
|
|
|
const auto &g(generator.aligned_right(spec.width, spec.pad));
|
2020-05-26 12:29:59 +02:00
|
|
|
return generate_string(out, max, g | ep, val);
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class generator>
|
|
|
|
bool
|
2018-11-28 22:47:33 +01:00
|
|
|
ircd::fmt::generate_string(char *&out,
|
2020-05-26 12:29:59 +02:00
|
|
|
const size_t &max,
|
2020-02-22 01:24:45 +01:00
|
|
|
generator&& gen,
|
2018-11-28 22:47:33 +01:00
|
|
|
const arg &val)
|
2016-09-17 04:48:25 +02:00
|
|
|
{
|
2016-09-18 00:43:10 +02:00
|
|
|
using karma::eps;
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
bool ret;
|
|
|
|
mutable_buffer buf
|
|
|
|
{
|
|
|
|
out, max
|
|
|
|
};
|
|
|
|
|
2016-09-17 04:48:25 +02:00
|
|
|
const auto &ptr(get<0>(val));
|
|
|
|
const auto &type(get<1>(val));
|
2017-10-16 06:20:25 +02:00
|
|
|
if(type == typeid(ircd::string_view) ||
|
|
|
|
type == typeid(ircd::json::string) ||
|
|
|
|
type == typeid(ircd::json::object) ||
|
|
|
|
type == typeid(ircd::json::array))
|
2017-03-18 01:00:41 +01:00
|
|
|
{
|
2017-03-25 00:19:12 +01:00
|
|
|
const auto &str(*static_cast<const ircd::string_view *>(ptr));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, std::forward<generator>(gen), str);
|
2017-03-18 01:00:41 +01:00
|
|
|
}
|
|
|
|
else if(type == typeid(std::string_view))
|
|
|
|
{
|
2017-03-25 00:19:12 +01:00
|
|
|
const auto &str(*static_cast<const std::string_view *>(ptr));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, std::forward<generator>(gen), str);
|
2017-03-18 01:00:41 +01:00
|
|
|
}
|
|
|
|
else if(type == typeid(std::string))
|
2016-09-17 04:48:25 +02:00
|
|
|
{
|
2017-03-25 00:19:12 +01:00
|
|
|
const auto &str(*static_cast<const std::string *>(ptr));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, std::forward<generator>(gen), string_view{str});
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
else if(type == typeid(const char *))
|
|
|
|
{
|
2019-02-16 01:47:00 +01:00
|
|
|
const char *const &str{*static_cast<const char *const *>(ptr)};
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, std::forward<generator>(gen), string_view{str});
|
2020-05-26 12:29:59 +02:00
|
|
|
} else {
|
|
|
|
// This for string literals which have unique array types depending on their size.
|
|
|
|
// There is no reasonable way to match them. The best that can be hoped for is the
|
|
|
|
// grammar will fail gracefully (most of the time) or not print something bogus when
|
|
|
|
// it happens to be legal.
|
|
|
|
const auto &str(static_cast<const char *>(ptr));
|
2020-06-05 18:23:38 +02:00
|
|
|
ret = fmt::generate(buf, std::forward<generator>(gen), string_view{str});
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:29:59 +02:00
|
|
|
out = data(buf);
|
|
|
|
return ret;
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|