mirror of
https://github.com/matrix-construct/construct
synced 2024-11-29 10:12:39 +01:00
ircd::fmt: Add the %p format specifier for pointer types.
This commit is contained in:
parent
3c38aeb96e
commit
d3e5cb7f73
1 changed files with 45 additions and 0 deletions
45
ircd/fmt.cc
45
ircd/fmt.cc
|
@ -189,6 +189,17 @@ const char_specifier
|
|||
"c"s
|
||||
};
|
||||
|
||||
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
|
||||
{
|
||||
"p"s
|
||||
};
|
||||
|
||||
} // namespace fmt
|
||||
} // namespace ircd
|
||||
|
||||
|
@ -335,6 +346,40 @@ catch(const illegal &e)
|
|||
e.what());
|
||||
}
|
||||
|
||||
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));
|
||||
const void *const p(*reinterpret_cast<const void *const *>(ptr));
|
||||
return karma::generate(out, maxwidth(max)[generator] | eps[throw_illegal], uintptr_t(p));
|
||||
}
|
||||
|
||||
bool
|
||||
fmt::char_specifier::operator()(char *&out,
|
||||
const size_t &max,
|
||||
|
|
Loading…
Reference in a new issue