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 92f98cf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
36 changes: 36 additions & 0 deletions include/fmt/std.h
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,39 @@ 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(out, '"');
else if constexpr (std::is_same_v<T, char>) out = detail::write(out, '\'');
ctx.advance_to(out);
out = underlying_.format(*opt, ctx);
if constexpr (detail::is_string<T>::value) out = detail::write(out, '"');
else if constexpr (std::is_same_v<T, char>) out = detail::write(out, '\'');
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
38 changes: 38 additions & 0 deletions test/std-test.cc
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,43 @@ 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{"left aligned"}}, 30),
"Option(\"left aligned \")");
EXPECT_EQ(fmt::format(
"{0:>{1}}", std::optional{'R'}, 30),
"Option(\' R\')");
EXPECT_EQ(fmt::format(
"{:*^30}", std::optional{"centered"}),
"Option(\"***********centered***********\")");
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 92f98cf

Please sign in to comment.