uri: add fmt::formatter for uri and component

This commit is contained in:
Danny Robson 2023-11-19 09:36:13 +10:00
parent b0adff1c87
commit da8df2297d
2 changed files with 30 additions and 26 deletions

44
uri.cpp
View File

@ -8,6 +8,8 @@
#include "./debug/warn.hpp"
#include <fmt/ostream.h>
#include <fmt/compile.h>
#include <fmt/format.h>
using cruft::uri;
@ -341,7 +343,7 @@ cruft::uri::percent_decode (view<const char*> s)
///////////////////////////////////////////////////////////////////////////////
static constexpr
char const*
std::string_view
to_string (cruft::uri::component val)
{
switch (val) {
@ -361,7 +363,27 @@ to_string (cruft::uri::component val)
}
////-----------------------------------------------------------------------------
fmt::format_context::iterator
fmt::formatter<cruft::uri::component>::format (
cruft::uri::component val,
format_context &ctx
) const {
return formatter<string_view>::format(::to_string (val), ctx);
}
////-----------------------------------------------------------------------------
fmt::format_context::iterator
fmt::formatter<cruft::uri>::format (
cruft::uri const &val,
fmt::format_context &ctx
) const {
return formatter<string_view>::format (val.value (), ctx);
}
//-----------------------------------------------------------------------------
std::ostream&
cruft::operator<< (std::ostream &os, cruft::uri::component c)
{
@ -378,26 +400,6 @@ cruft::operator<< (std::ostream &os, cruft::uri const &val)
return os;
}
//-----------------------------------------------------------------------------
fmt::format_context::iterator
fmt::formatter<cruft::uri::component>::format (
cruft::uri::component val,
fmt::format_context &ctx
) const {
return fmt::formatter<std::string>::format (::to_string (val), ctx);
}
//-----------------------------------------------------------------------------
fmt::format_context::iterator
fmt::formatter<cruft::uri>::format (
cruft::uri const &val,
fmt::format_context &ctx
) const {
return fmt::formatter<std::string>::format (val.value (), ctx);
}
///////////////////////////////////////////////////////////////////////////////
static std::string
merge (std::string_view base, std::string_view relative)

12
uri.hpp
View File

@ -160,15 +160,17 @@ namespace cruft {
}
// Don't use std::string_view here, fmtlib reimplements it as fmt::string_view.
template <>
struct fmt::formatter<cruft::uri> : public fmt::formatter<std::string> {
struct fmt::formatter<cruft::uri> : public fmt::formatter<string_view> {
format_context::iterator
format (cruft::uri const&, format_context &ctx) const;
};
template <>
struct fmt::formatter<cruft::uri::component> : public fmt::formatter<std::string> {
format_context::iterator
format (cruft::uri::component, format_context &ctx) const;
// Don't use std::string_view here, fmtlib reimplements it as fmt::string_view.
template <> struct fmt::formatter<cruft::uri::component>: formatter<string_view> {
// parse is inherited from formatter<string_view>.
fmt::format_context::iterator
format(cruft::uri::component c, format_context& ctx) const;
};