Skip to content

Commit

Permalink
Add std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-huntington committed Feb 12, 2023
1 parent 655046d commit fc57abb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
38 changes: 38 additions & 0 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
# if FMT_HAS_INCLUDE(<variant>)
# include <variant>
# endif
# if FMT_HAS_INCLUDE(<optional>)
# include <optional>
# endif
#endif

// GCC 4 does not support FMT_HAS_INCLUDE.
Expand Down Expand Up @@ -91,6 +94,41 @@ template <typename Char>
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
FMT_END_NAMESPACE

#ifdef __cpp_lib_optional
FMT_BEGIN_NAMESPACE
template <class T, typename Char>
struct formatter<std::optional<T>, Char,
std::enable_if_t<is_formattable<T>::value>> {
formatter<T, Char> underlying_;
static constexpr basic_string_view<Char> OPTION = detail::string_literal<Char, 'O','p','t','i','o','n','('>{};
static constexpr basic_string_view<Char> EMPTY = detail::string_literal<Char, 'E','m','p','t','y'>{};

template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) {
return underlying_.parse(ctx);
}
template <typename FormatContext>
auto format(std::optional<T> const& opt, FormatContext& ctx) const
-> decltype(ctx.out()) {
if (opt) {
auto out = ctx.out();
out = detail::write<Char>(out, OPTION);
if constexpr (detail::is_string<T>::value)
out = detail::write_escaped_string<Char>(out, detail::to_string_view(*opt));
else if constexpr (std::is_same_v<T, char>)
out = detail::write_escaped_char<Char>(out, *opt);
else {
ctx.advance_to(out);
out = underlying_.format(*opt, ctx);
}
return detail::write(out, ')');
}
return detail::write<Char>(ctx.out(), EMPTY);
}
};
FMT_END_NAMESPACE
#endif // __cpp_lib_optional

#ifdef __cpp_lib_variant
FMT_BEGIN_NAMESPACE
template <typename Char> struct formatter<std::monostate, Char> {
Expand Down
35 changes: 35 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// For the license information refer to format.h.

#include "fmt/std.h"
#include "fmt/xchar.h"

#include <string>
#include <vector>
Expand Down Expand Up @@ -50,6 +51,40 @@ TEST(std_test, thread_id) {
EXPECT_FALSE(fmt::format("{}", std::this_thread::get_id()).empty());
}

TEST(std_test, optional) {
#ifdef __cpp_lib_optional
EXPECT_EQ(fmt::format(
L"{}", std::optional<int>{}),
L"Empty");
EXPECT_EQ(fmt::format(
"{}", std::vector{std::optional{1}, std::optional{2}, std::optional{3}}),
"[Option(1), Option(2), Option(3)]");
EXPECT_EQ(fmt::format(
"{:#x}", std::optional<std::optional<int>>{std::optional{1}}),
"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{"string"}}),
"Option(\"string\")");
EXPECT_EQ(fmt::format(
"{0:>{1}}", std::optional{'C'}),
"Option(\'C\')");
EXPECT_EQ(fmt::format(
"{:.{}f}", std::optional{3.14}, 1),
"Option(3.1)");
EXPECT_EQ(fmt::format(
"int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", std::optional{42}),
"int: Option(42); hex: Option(2a); oct: Option(52); bin: Option(101010)");

struct unformattable {};
EXPECT_FALSE((fmt::is_formattable<unformattable>::value));
EXPECT_FALSE((fmt::is_formattable<std::optional<unformattable>>::value));
EXPECT_TRUE((fmt::is_formattable<std::optional<int>>::value));
#endif
}

TEST(std_test, variant) {
#ifdef __cpp_lib_variant
EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
Expand Down

0 comments on commit fc57abb

Please sign in to comment.