Merge pull request from tchaikov/wip-fmt-containers

include/types_fmt: use fmt::join() to format containers

Reviewed-by: Ronen Friedman <rfriedma@redhat.com>
This commit is contained in:
Kefu Chai 2022-07-25 23:41:03 +08:00 committed by GitHub
commit f85f60f366
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 35 deletions

View File

@ -296,7 +296,7 @@ include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/src/xxHash")
include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/src/rapidjson/include")
option(WITH_FMT_HEADER_ONLY "use header-only version of fmt library" OFF)
find_package(fmt 6.0.0 QUIET)
find_package(fmt 7.0.0 QUIET)
if(fmt_FOUND)
include_directories(SYSTEM "${fmt_INCLUDE_DIR}")
else()
@ -306,6 +306,8 @@ else()
add_subdirectory(fmt)
set(BUILD_SHARED_LIBS ${old_BUILD_SHARED_LIBS})
unset(old_BUILD_SHARED_LIBS)
target_compile_definitions(fmt PUBLIC
$<$<BOOL:${WIN32}>:FMT_USE_TZSET=0>)
include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/src/fmt/include")
endif()

@ -1 +1 @@
Subproject commit 427b53405442872decb69d9735db0fa64106ebe2
Subproject commit b6f4ceaed0a0a24ccf575fab6c56dd50ccf6f1a9

View File

@ -11,6 +11,17 @@
#include "include/types.h"
template <class Key, class T>
struct fmt::formatter<std::pair<const Key, T>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::pair<const Key, T>& p, FormatContext& ctx)
{
return fmt::format_to(ctx.out(), "{}={}", p.first, p.second);
}
};
template <class A, class B, class Comp, class Alloc>
struct fmt::formatter<std::map<A, B, Comp, Alloc>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
@ -18,59 +29,39 @@ struct fmt::formatter<std::map<A, B, Comp, Alloc>> {
template <typename FormatContext>
auto format(const std::map<A, B, Comp, Alloc>& m, FormatContext& ctx)
{
std::string_view sep = "{";
for (const auto& [k, v] : m) {
fmt::format_to(ctx.out(), "{}{}={}", sep, k, v);
sep = ",";
}
return fmt::format_to(ctx.out(), "}}");
return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(m, ","));
}
};
template <class A>
struct fmt::formatter<std::list<A>> {
template <class... Args>
struct fmt::formatter<std::list<Args...>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::list<A>& l, FormatContext& ctx)
auto format(const std::list<Args...>& l, FormatContext& ctx)
{
std::string_view sep = "";
for (const auto& e : l) {
fmt::format_to(ctx.out(), "{}{}", sep, e);
sep = ",";
}
return ctx.out();
return fmt::format_to(ctx.out(), "{}", fmt::join(l, ","));
}
};
template <class A>
struct fmt::formatter<std::vector<A>> {
template <class... Args>
struct fmt::formatter<std::vector<Args...>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::vector<A>& l, FormatContext& ctx)
auto format(const std::vector<Args...>& v, FormatContext& ctx)
{
std::string_view sep = "[";
for (const auto& e : l) {
fmt::format_to(ctx.out(), "{}{}", sep, e);
sep = ",";
}
return fmt::format_to(ctx.out(), "]");
return fmt::format_to(ctx.out(), "[{}]", fmt::join(v, ","));
}
};
template <class A>
struct fmt::formatter<std::set<A>> {
template <class... Args>
struct fmt::formatter<std::set<Args...>> {
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const std::set<A>& l, FormatContext& ctx)
auto format(const std::set<Args...>& s, FormatContext& ctx)
{
std::string_view sep = "";
for (const auto& e : l) {
fmt::format_to(ctx.out(), "{}{}", sep, e);
sep = ",";
}
return ctx.out();
return fmt::format_to(ctx.out(), "{}", fmt::join(s, ","));
}
};