Skip to content

Commit

Permalink
Add a formatter specialization for std::error_code.
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed May 8, 2021
1 parent 84feeb0 commit 41ebf7f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,24 @@ template <> struct formatter<bytes> {
}
};

// A formatter specialization for std::error_code.
template <typename Char> struct formatter<std::error_code, Char> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

template <typename FormatContext>
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
-> decltype(ctx.out()) {
auto out = ctx.out();
out = detail::write<Char>(out, ec.category().name());
out = detail::write<Char>(out, Char(':'));
out = detail::write<Char>(out, ec.value());
return out;
}
};

template <typename It, typename Sentinel, typename Char>
struct arg_join : detail::view {
It begin;
Expand Down
24 changes: 24 additions & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,30 @@ TEST(compile_test, format_to_n) {
EXPECT_STREQ("2a", buffer);
}

TEST(compile_test, format_std_error_code) {
EXPECT_EQ("generic:42",
fmt::format(FMT_COMPILE("{0}"),
std::error_code(42, std::generic_category())));
EXPECT_EQ("system:42",
fmt::format(FMT_COMPILE("{0}"),
std::error_code(42, std::system_category())));
EXPECT_EQ("system:-42",
fmt::format(FMT_COMPILE("{0}"),
std::error_code(-42, std::system_category())));
}

TEST(compile_test, format_std_error_code_wide) {
EXPECT_EQ(L"generic:42",
fmt::format(FMT_COMPILE(L"{0}"),
std::error_code(42, std::generic_category())));
EXPECT_EQ(L"system:42",
fmt::format(FMT_COMPILE(L"{0}"),
std::error_code(42, std::system_category())));
EXPECT_EQ(L"system:-42",
fmt::format(FMT_COMPILE(L"{0}"),
std::error_code(-42, std::system_category())));
}

TEST(compile_test, formatted_size) {
EXPECT_EQ(2, fmt::formatted_size(FMT_COMPILE("{0}"), 42));
EXPECT_EQ(5, fmt::formatted_size(FMT_COMPILE("{0:<4.2f}"), 42.0));
Expand Down
18 changes: 18 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,24 @@ TEST(format_test, test_formatters_enabled) {
const wchar_t*, std::wstring, std::nullptr_t>();
}

TEST(format_test, format_std_error_code) {
EXPECT_EQ("generic:42",
fmt::format("{0}", std::error_code(42, std::generic_category())));
EXPECT_EQ("system:42",
fmt::format("{0}", std::error_code(42, std::system_category())));
EXPECT_EQ("system:-42",
fmt::format("{0}", std::error_code(-42, std::system_category())));
}

TEST(format_test, format_std_error_code_wide) {
EXPECT_EQ(L"generic:42",
fmt::format(L"{0}", std::error_code(42, std::generic_category())));
EXPECT_EQ(L"system:42",
fmt::format(L"{0}", std::error_code(42, std::system_category())));
EXPECT_EQ(L"system:-42",
fmt::format(L"{0}", std::error_code(-42, std::system_category())));
}

TEST(format_int_test, data) {
fmt::format_int format_int(42);
EXPECT_EQ("42", std::string(format_int.data(), format_int.size()));
Expand Down

0 comments on commit 41ebf7f

Please sign in to comment.