0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-24 17:38:54 +02:00

ircd::fmt: Introduce the format string literal operator.

This commit is contained in:
Jason Volk 2019-10-02 17:39:32 -07:00
parent 77cd60b970
commit a3cf1dfa30

View file

@ -25,9 +25,19 @@ namespace ircd::fmt
struct vsnprintf;
struct snstringf;
struct vsnstringf;
struct literal_snstringf;
struct literal_snstringf__max;
template<size_t MAX> struct bsprintf;
using arg = std::tuple<const void *, const std::type_index &>;
literal_snstringf operator ""_snstringf(const char *text, const size_t len);
literal_snstringf__max operator,(const literal_snstringf, const ulong max);
template<class... args> std::string operator,(const literal_snstringf__max, args&&...);
}
namespace ircd
{
using fmt::operator ""_snstringf;
}
/// Typesafe snprintf() from formal grammar and RTTI.
@ -173,3 +183,48 @@ struct ircd::fmt::bsprintf
buf.data(), size_t(static_cast<snprintf &>(*this))
}{}
};
struct ircd::fmt::literal_snstringf
{
string_view format;
};
struct ircd::fmt::literal_snstringf__max
{
literal_snstringf format;
size_t max;
};
template<class... args>
inline std::string
ircd::fmt::operator,(const literal_snstringf__max format,
args&&... a)
{
return fmt::snstringf
{
format.max, format.format.format, std::forward<args>(a)...
};
}
inline ircd::fmt::literal_snstringf__max
ircd::fmt::operator,(const literal_snstringf format,
const ulong max)
{
return literal_snstringf__max
{
format, max
};
}
inline ircd::fmt::literal_snstringf
ircd::fmt::operator ""_snstringf(const char *const format,
const size_t len)
{
return literal_snstringf
{
string_view
{
format, len
}
};
}