Skip to content

Commit

Permalink
change Some(...)/None to Option(...)/Empty
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-huntington committed Feb 11, 2023
1 parent aedc396 commit 7756f40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ struct formatter<std::optional<T>, Char,
-> decltype(ctx.out()) {
if (opt) {
constexpr auto SORROUND = []() -> std::array<const char*, 2> {
if constexpr (detail::is_string<T>::value) return {"Some(\"", "\")"};
if constexpr (std::is_same_v<T, char>) return {"Some(\'", "\')"};
return {"Some(", ")"};
if constexpr (detail::is_string<T>::value) return {"Option(\"", "\")"};
if constexpr (std::is_same_v<T, char>) return {"Option(\'", "\')"};
return {"Option(", ")"};
}();
auto out = ctx.out();
out = detail::write<Char>(out, SORROUND[0]);
ctx.advance_to(out);
out = underlying.format(*opt, ctx);
return detail::write<Char>(out, SORROUND[1]);
}
return format_to(ctx.out(), "None");
return format_to(ctx.out(), "Empty");
}
};
FMT_END_NAMESPACE
Expand Down
15 changes: 8 additions & 7 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,31 @@ TEST(std_test, thread_id) {

TEST(std_test, optional) {
#if __cpp_lib_optional
EXPECT_EQ(fmt::format("{}", std::optional<int>{}), "Empty");
EXPECT_EQ(fmt::format(
"{}", std::vector{std::optional{1}, std::optional{2}, std::optional{3}}),
"[Some(1), Some(2), Some(3)]");
"[Option(1), Option(2), Option(3)]");
EXPECT_EQ(fmt::format(
"{:#x}", std::optional<std::optional<int>>{std::optional{1}}),
"Some(Some(0x1))");
"Option(Option(0x1))");
EXPECT_EQ(fmt::format(
"{::d}", std::vector{'h', 'e', 'l', 'l', 'o'}),
"[104, 101, 108, 108, 111]");
EXPECT_EQ(fmt::format(
"{:<{}}", std::optional{std::string{"left aligned"}}, 30),
"Some(\"left aligned \")");
"Option(\"left aligned \")");
EXPECT_EQ(fmt::format(
"{0:>{1}}", std::optional{'R'}, 30),
"Some(\' R\')");
"Option(\' R\')");
EXPECT_EQ(fmt::format(
"{:*^30}", std::optional{"centered"}),
"Some(\"***********centered***********\")");
"Option(\"***********centered***********\")");
EXPECT_EQ(fmt::format(
"{:.{}f}", std::optional{3.14}, 1),
"Some(3.1)");
"Option(3.1)");
EXPECT_EQ(fmt::format(
"int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", std::optional{42}),
"int: Some(42); hex: Some(2a); oct: Some(52); bin: Some(101010)");
"int: Option(42); hex: Option(2a); oct: Option(52); bin: Option(101010)");

struct unformattable {};
EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
Expand Down

0 comments on commit 7756f40

Please sign in to comment.