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
|
|
|
|
2017-11-30 19:29:34 +01:00
|
|
|
#include <ircd/spirit.h>
|
2016-09-17 04:48:25 +02:00
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT
|
|
|
|
(
|
|
|
|
ircd::fmt::spec,
|
2017-03-18 01:00:41 +01:00
|
|
|
( decltype(ircd::fmt::spec::sign), sign )
|
|
|
|
( decltype(ircd::fmt::spec::width), width )
|
|
|
|
( decltype(ircd::fmt::spec::name), name )
|
2016-09-17 04:48:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace fmt {
|
|
|
|
|
|
|
|
namespace qi = boost::spirit::qi;
|
|
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
|
2017-03-18 01:00:41 +01:00
|
|
|
using qi::lit;
|
|
|
|
using qi::char_;
|
2017-04-07 03:11:09 +02:00
|
|
|
using qi::ushort_;
|
2017-03-18 01:00:41 +01:00
|
|
|
using qi::int_;
|
|
|
|
using qi::eps;
|
|
|
|
using qi::raw;
|
|
|
|
using qi::repeat;
|
|
|
|
using qi::omit;
|
|
|
|
using qi::unused_type;
|
|
|
|
|
2017-04-07 03:11:09 +02:00
|
|
|
std::map<string_view, specifier *, std::less<>> _specifiers;
|
2017-03-21 03:13:44 +01:00
|
|
|
|
2017-03-18 01:00:41 +01:00
|
|
|
bool is_specifier(const string_view &name);
|
2017-03-21 03:13:44 +01:00
|
|
|
void handle_specifier(char *&out, const size_t &max, const uint &idx, const spec &, const arg &);
|
|
|
|
template<class generator> bool generate_string(char *&out, const generator &gen, const arg &val);
|
|
|
|
template<class T, class lambda> bool visit_type(const arg &val, lambda&& closure);
|
2017-03-18 01:00:41 +01:00
|
|
|
|
|
|
|
struct parser
|
|
|
|
:qi::grammar<const char *, fmt::spec>
|
|
|
|
{
|
|
|
|
template<class R = unused_type> using rule = qi::rule<const char *, R>;
|
|
|
|
|
2017-03-23 23:00:26 +01:00
|
|
|
const rule<> specsym { lit(SPECIFIER) ,"format specifier" };
|
|
|
|
const rule<> specterm { lit(SPECIFIER_TERMINATOR) ,"specifier termination" };
|
2017-03-18 01:00:41 +01:00
|
|
|
const rule<string_view> name
|
|
|
|
{
|
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"
|
|
|
|
};
|
|
|
|
|
|
|
|
rule<fmt::spec> spec;
|
|
|
|
|
|
|
|
parser()
|
|
|
|
:parser::base_type{spec}
|
|
|
|
{
|
2017-03-23 23:00:26 +01:00
|
|
|
static const auto is_valid([]
|
|
|
|
(const auto &str, auto &, auto &valid)
|
2017-03-18 01:00:41 +01:00
|
|
|
{
|
|
|
|
valid = is_specifier(str);
|
2017-03-23 23:00:26 +01:00
|
|
|
});
|
|
|
|
|
2017-04-07 03:11:09 +02:00
|
|
|
spec %= specsym >> -(char_('+') | char_('-')) >> -ushort_ >> name[is_valid] >> -specterm;
|
2017-03-18 01:00:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const parser;
|
2016-09-17 04:48:25 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
struct 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;
|
|
|
|
}
|
2017-03-21 03:13:44 +01:00
|
|
|
const 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
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
decltype(string_specifier::types)
|
|
|
|
string_specifier::types
|
|
|
|
{};
|
2016-09-17 08:39:44 +02:00
|
|
|
|
2017-04-03 05:53:14 +02:00
|
|
|
struct bool_specifier
|
|
|
|
: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;
|
|
|
|
}
|
|
|
|
const bool_specifier
|
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
{ "b"s }
|
2017-04-03 05:53:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(bool_specifier::types)
|
|
|
|
bool_specifier::types
|
|
|
|
{};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
struct 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;
|
|
|
|
}
|
2017-03-21 03:13:44 +01:00
|
|
|
const 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
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
decltype(signed_specifier::types)
|
|
|
|
signed_specifier::types
|
|
|
|
{};
|
2016-09-18 01:55:21 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
struct 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;
|
|
|
|
}
|
2017-03-21 03:13:44 +01:00
|
|
|
const 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
|
|
|
};
|
|
|
|
|
2017-04-07 03:34:44 +02:00
|
|
|
struct hex_lowercase_specifier
|
|
|
|
: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;
|
|
|
|
}
|
|
|
|
const hex_lowercase_specifier
|
|
|
|
{
|
|
|
|
{ "x"s, "lx"s }
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(hex_lowercase_specifier::types)
|
|
|
|
hex_lowercase_specifier::types
|
|
|
|
{};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
decltype(unsigned_specifier::types)
|
|
|
|
unsigned_specifier::types
|
|
|
|
{};
|
|
|
|
|
|
|
|
struct float_specifier
|
2016-09-18 00:43:10 +02:00
|
|
|
:specifier
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
static const std::tuple
|
|
|
|
<
|
|
|
|
char, unsigned char,
|
|
|
|
short, unsigned short,
|
|
|
|
int, unsigned int,
|
|
|
|
long, unsigned long,
|
|
|
|
float, double
|
|
|
|
>
|
|
|
|
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;
|
|
|
|
}
|
2017-03-21 03:13:44 +01:00
|
|
|
const 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
|
|
|
};
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
decltype(float_specifier::types)
|
|
|
|
float_specifier::types
|
|
|
|
{};
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
struct char_specifier
|
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
|
|
|
const char_specifier
|
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
"c"s
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
2017-03-18 05:03:15 +01:00
|
|
|
struct pointer_specifier
|
|
|
|
:specifier
|
|
|
|
{
|
|
|
|
bool operator()(char *&out, const size_t &max, const spec &, const arg &val) const override;
|
|
|
|
using specifier::specifier;
|
|
|
|
}
|
|
|
|
const pointer_specifier
|
|
|
|
{
|
2017-04-07 03:11:09 +02:00
|
|
|
"p"s
|
2017-03-18 05:03:15 +01:00
|
|
|
};
|
|
|
|
|
2016-09-17 04:48:25 +02:00
|
|
|
} // namespace fmt
|
|
|
|
} // namespace ircd
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2016-09-18 10:15:40 +02:00
|
|
|
fmt::snprintf::snprintf(internal_t,
|
|
|
|
char *const &out,
|
|
|
|
const size_t &max,
|
|
|
|
const char *const &fstr,
|
2017-03-18 01:36:09 +01:00
|
|
|
const va_rtti &v)
|
2016-09-18 00:43:10 +02:00
|
|
|
try
|
2016-09-18 10:15:40 +02:00
|
|
|
:fstart{strchr(fstr, SPECIFIER)}
|
|
|
|
,fstop{fstr}
|
2018-01-19 05:19:38 +01:00
|
|
|
,fend{fstr + strnlen(fstr, max)}
|
2016-09-18 10:15:40 +02:00
|
|
|
,obeg{out}
|
|
|
|
,oend{out + max}
|
|
|
|
,out{out}
|
|
|
|
,idx{0}
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
|
|
|
if(unlikely(!max))
|
|
|
|
{
|
2016-09-18 10:15:40 +02:00
|
|
|
fstart = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2016-09-18 10:15:40 +02:00
|
|
|
if(!fstart)
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2018-01-19 05:19:38 +01:00
|
|
|
const mutable_buffer dst{out, max};
|
|
|
|
const const_buffer src{fstr, fend};
|
|
|
|
this->out += strlcpy(dst, src);
|
2016-09-18 10:15:40 +02:00
|
|
|
return;
|
2016-09-18 00:43:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 10:15:40 +02:00
|
|
|
append(fstr, fstart);
|
2017-03-18 03:58:17 +01:00
|
|
|
auto it(begin(v));
|
|
|
|
for(size_t i(0); i < v.size(); ++it, i++)
|
|
|
|
{
|
2017-11-06 21:27:24 +01:00
|
|
|
assert(!finished());
|
2017-03-18 03:58:17 +01:00
|
|
|
const auto &ptr(get<0>(*it));
|
|
|
|
const auto &type(get<1>(*it));
|
|
|
|
argument(std::make_tuple(ptr, std::type_index(*type)));
|
|
|
|
}
|
2018-01-19 05:19:38 +01:00
|
|
|
|
|
|
|
assert(this->out >= obeg);
|
|
|
|
assert(this->out < oend);
|
|
|
|
*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
|
|
|
}
|
|
|
|
|
2016-09-18 10:15:40 +02:00
|
|
|
void
|
|
|
|
fmt::snprintf::argument(const arg &val)
|
|
|
|
{
|
|
|
|
if(finished())
|
|
|
|
return;
|
|
|
|
|
|
|
|
fmt::spec spec;
|
|
|
|
if(qi::parse(fstart, fend, parser, spec))
|
|
|
|
handle_specifier(out, remaining(), idx++, spec, val);
|
|
|
|
|
2017-03-23 23:00:26 +01:00
|
|
|
fstop = fstart;
|
2018-01-19 05:19:38 +01:00
|
|
|
if(fstart >= fend)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fstart = strchr(fstart, SPECIFIER);
|
|
|
|
append(fstop, fstart?: fend);
|
2016-09-18 10:15:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fmt::snprintf::append(const char *const &begin,
|
|
|
|
const char *const &end)
|
|
|
|
{
|
2018-01-19 05:19:38 +01:00
|
|
|
const size_t &len(std::distance(begin, end));
|
|
|
|
const size_t &cpsz(std::min(len, remaining()));
|
2016-09-18 10:15:40 +02:00
|
|
|
memcpy(out, begin, cpsz);
|
|
|
|
out += cpsz;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
const decltype(fmt::_specifiers) &
|
|
|
|
fmt::specifiers()
|
|
|
|
{
|
|
|
|
return _specifiers;
|
|
|
|
}
|
|
|
|
|
2017-04-07 03:11:09 +02:00
|
|
|
fmt::specifier::specifier(const std::string &name)
|
2016-09-18 00:43:10 +02:00
|
|
|
:specifier{{name}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-07 03:11:09 +02:00
|
|
|
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)
|
|
|
|
if(is_specifier(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)
|
|
|
|
_specifiers.emplace(name, this);
|
2016-09-17 08:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt::specifier::~specifier()
|
|
|
|
noexcept
|
|
|
|
{
|
2016-09-18 00:43:10 +02:00
|
|
|
for(const auto &name : names)
|
|
|
|
_specifiers.erase(name);
|
2016-09-17 08:39:44 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 04:48:25 +02:00
|
|
|
bool
|
2017-03-18 01:00:41 +01:00
|
|
|
fmt::is_specifier(const string_view &name)
|
2016-09-17 04:48:25 +02:00
|
|
|
{
|
2016-09-17 08:39:44 +02:00
|
|
|
return specifiers().count(name);
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fmt::handle_specifier(char *&out,
|
|
|
|
const size_t &max,
|
2016-09-18 00:43:10 +02:00
|
|
|
const uint &idx,
|
2016-09-17 04:48:25 +02:00
|
|
|
const spec &spec,
|
|
|
|
const arg &val)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &type(get<1>(val));
|
2016-09-17 08:39:44 +02:00
|
|
|
const auto &handler(*specifiers().at(spec.name));
|
2017-09-08 10:58:59 +02:00
|
|
|
if(unlikely(!handler(out, 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
|
|
|
|
fmt::visit_type(const arg &val,
|
|
|
|
lambda&& closure)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-18 05:03:15 +01:00
|
|
|
bool
|
|
|
|
fmt::pointer_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::ulong_;
|
|
|
|
using karma::eps;
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Not a pointer");
|
|
|
|
});
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, uintptr_t()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, uintptr_t()> pointer_hex
|
|
|
|
{
|
|
|
|
lit("0x") << karma::hex
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{pointer_hex} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
|
|
|
const auto &ptr(get<0>(val));
|
|
|
|
const auto &type(get<1>(val));
|
2017-03-25 00:19:12 +01:00
|
|
|
const void *const p(*static_cast<const void *const *>(ptr));
|
2017-03-18 05:03:15 +01:00
|
|
|
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], uintptr_t(p));
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
bool
|
|
|
|
fmt::char_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::eps;
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Not a printable character");
|
|
|
|
});
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2017-03-25 00:19:12 +01:00
|
|
|
const auto &c(*static_cast<const char *>(ptr));
|
2016-09-18 00:43:10 +02:00
|
|
|
karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], c);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2017-04-03 05:53:14 +02:00
|
|
|
bool
|
|
|
|
fmt::bool_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::eps;
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Failed to print signed value");
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto closure([&](const bool &boolean)
|
|
|
|
{
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, bool()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, bool()> rule
|
|
|
|
{
|
|
|
|
karma::bool_
|
|
|
|
,"boolean"
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
|
|
|
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], boolean);
|
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
bool
|
2017-03-21 03:13:44 +01:00
|
|
|
fmt::signed_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &s,
|
|
|
|
const arg &val)
|
2016-09-18 00:43:10 +02:00
|
|
|
const
|
|
|
|
{
|
2017-03-21 03:13:44 +01:00
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Failed to print signed value");
|
|
|
|
});
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
const auto closure([&](const long &integer)
|
|
|
|
{
|
|
|
|
using karma::long_;
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, long()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, long()> rule
|
|
|
|
{
|
|
|
|
long_
|
|
|
|
,"signed long integer"
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
|
|
|
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], integer);
|
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fmt::unsigned_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &s,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Failed to print unsigned value");
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto closure([&](const ulong &integer)
|
|
|
|
{
|
|
|
|
using karma::ulong_;
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, ulong()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, ulong()> rule
|
|
|
|
{
|
|
|
|
ulong_
|
|
|
|
,"unsigned long integer"
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], integer);
|
|
|
|
});
|
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
|
|
|
|
fmt::hex_lowercase_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &s,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Failed to print hexadecimal value");
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto closure([&](const uint &integer)
|
|
|
|
{
|
|
|
|
using karma::maxwidth;
|
|
|
|
|
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, uint()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, uint()> rule
|
|
|
|
{
|
|
|
|
karma::lower[karma::hex]
|
|
|
|
,"unsigned lowercase hexadecimal"
|
|
|
|
};
|
|
|
|
|
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
|
|
|
|
|
|
|
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], integer);
|
|
|
|
});
|
|
|
|
|
|
|
|
return !until(types, [&](auto type)
|
|
|
|
{
|
|
|
|
return !visit_type<decltype(type)>(val, closure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
bool
|
|
|
|
fmt::float_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &s,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Failed to print floating point value");
|
|
|
|
});
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
const auto closure([&](const double &floating)
|
|
|
|
{
|
|
|
|
using karma::double_;
|
|
|
|
using karma::maxwidth;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
struct generator
|
|
|
|
:karma::grammar<char *, double()>
|
|
|
|
{
|
|
|
|
karma::rule<char *, double()> rule
|
|
|
|
{
|
|
|
|
double_
|
|
|
|
,"floating point integer"
|
|
|
|
};
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
generator(): generator::base_type{rule} {}
|
|
|
|
}
|
|
|
|
static const generator;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
2017-03-21 03:13:44 +01:00
|
|
|
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], floating);
|
|
|
|
});
|
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-03-21 03:13:44 +01:00
|
|
|
/*
|
2016-09-18 00:43:10 +02:00
|
|
|
if(type == typeid(const char[]))
|
|
|
|
{
|
|
|
|
const auto &i(reinterpret_cast<const char *>(ptr));
|
2017-03-18 01:00:41 +01:00
|
|
|
if(!try_lex_cast<ssize_t>(i))
|
2016-09-18 00:43:10 +02:00
|
|
|
throw illegal("The string literal value for integer specifier is not a valid integer");
|
|
|
|
|
2017-03-18 01:00:41 +01:00
|
|
|
const auto len(std::min(max, strlen(i)));
|
2016-09-18 00:43:10 +02:00
|
|
|
memcpy(out, i, len);
|
|
|
|
out += len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(type == typeid(const char *))
|
|
|
|
{
|
|
|
|
const auto &i(*reinterpret_cast<const char *const *>(ptr));
|
2017-03-18 01:00:41 +01:00
|
|
|
if(!try_lex_cast<ssize_t>(i))
|
2016-09-18 00:43:10 +02:00
|
|
|
throw illegal("The character buffer for integer specifier is not a valid integer");
|
|
|
|
|
2017-03-18 01:00:41 +01:00
|
|
|
const auto len(std::min(max, strlen(i)));
|
2016-09-18 00:43:10 +02:00
|
|
|
memcpy(out, i, len);
|
|
|
|
out += len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(type == typeid(const std::string))
|
|
|
|
{
|
|
|
|
const auto &i(*reinterpret_cast<const std::string *>(ptr));
|
2017-03-18 01:00:41 +01:00
|
|
|
if(!try_lex_cast<ssize_t>(i))
|
|
|
|
throw illegal("The string argument for integer specifier is not a valid integer");
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
const auto len(std::min(max, i.size()));
|
2017-03-18 01:00:41 +01:00
|
|
|
memcpy(out, i.data(), len);
|
|
|
|
out += len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(type == typeid(const string_view) || type == typeid(const std::string_view))
|
|
|
|
{
|
|
|
|
const auto &i(*reinterpret_cast<const std::string_view *>(ptr));
|
|
|
|
if(!try_lex_cast<ssize_t>(i))
|
2016-09-18 00:43:10 +02:00
|
|
|
throw illegal("The string argument for integer specifier is not a valid integer");
|
|
|
|
|
2017-03-18 01:00:41 +01:00
|
|
|
const auto len(std::min(max, i.size()));
|
2016-09-18 00:43:10 +02:00
|
|
|
memcpy(out, i.data(), len);
|
|
|
|
out += len;
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-21 03:13:44 +01:00
|
|
|
*/
|
2016-09-18 00:43:10 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
fmt::string_specifier::operator()(char *&out,
|
|
|
|
const size_t &max,
|
|
|
|
const spec &,
|
|
|
|
const arg &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
using karma::char_;
|
|
|
|
using karma::eps;
|
|
|
|
using karma::maxwidth;
|
2017-03-18 01:00:41 +01:00
|
|
|
using karma::unused_type;
|
2016-09-18 00:43:10 +02:00
|
|
|
|
|
|
|
static const auto throw_illegal([]
|
|
|
|
{
|
|
|
|
throw illegal("Not a printable string");
|
|
|
|
});
|
|
|
|
|
|
|
|
struct generator
|
2017-03-18 03:58:17 +01:00
|
|
|
:karma::grammar<char *, const string_view &>
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2017-03-18 03:58:17 +01:00
|
|
|
karma::rule<char *, const string_view &> string
|
2016-09-18 00:43:10 +02:00
|
|
|
{
|
2017-03-18 03:58:17 +01:00
|
|
|
*(karma::print)
|
|
|
|
,"string"
|
2016-09-18 00:43:10 +02:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
return generate_string(out, maxwidth(max)[generator] | eps[throw_illegal], val);
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class generator>
|
|
|
|
bool
|
|
|
|
fmt::generate_string(char *&out,
|
|
|
|
const generator &gen,
|
|
|
|
const arg &val)
|
|
|
|
{
|
2016-09-18 00:43:10 +02:00
|
|
|
using karma::eps;
|
|
|
|
|
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));
|
2017-03-21 03:13:44 +01:00
|
|
|
return karma::generate(out, 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));
|
2017-03-21 03:13:44 +01:00
|
|
|
return karma::generate(out, 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));
|
2017-03-21 03:13:44 +01:00
|
|
|
return karma::generate(out, gen, string_view{str});
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
else if(type == typeid(const char *))
|
|
|
|
{
|
2017-03-25 00:19:12 +01:00
|
|
|
const char *const &str{*static_cast<const char *const *const>(ptr)};
|
2017-03-21 03:13:44 +01:00
|
|
|
return karma::generate(out, gen, string_view{str});
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:43:10 +02:00
|
|
|
// 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.
|
2017-03-25 00:19:12 +01:00
|
|
|
const auto &str(static_cast<const char *>(ptr));
|
2017-03-18 03:58:17 +01:00
|
|
|
return karma::generate(out, gen, string_view{str});
|
2016-09-17 04:48:25 +02:00
|
|
|
}
|