Skip to content

Commit

Permalink
Supporting nested format specs for ranges. (#2673)
Browse files Browse the repository at this point in the history
* Supporting nested format specs for ranges.

* I dedicate this commit to Eric Niebler.

* clang-format

* PR comments.

* throw -> FMT_THROW

* Need to map every element too.

* Clarifying uncvref_type

* Trying to add a workaround for MSVC.
  • Loading branch information
brevzin authored Jan 8, 2022
1 parent 0102101 commit 6e0f139
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 20 deletions.
108 changes: 88 additions & 20 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,28 @@ template <class Tuple, class F> void for_each(Tuple&& tup, F&& f) {
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
}

#if FMT_MSC_VER
// older MSVC doesn't get the reference type correctly for arrays
template <typename R> struct range_reference_type_impl {
using type = decltype(*detail::range_begin(std::declval<R&>()));
};

template <typename T, std::size_t N> struct range_reference_type_impl<T[N]> {
using type = T&;
};

template <typename T>
using range_reference_type = typename range_reference_type_impl<T>::type;
#else
template <typename Range>
using range_reference_type =
decltype(*detail::range_begin(std::declval<Range&>()));
#endif

// We don't use the Range's value_type for anything, but we do need the Range's
// reference type, with cv-ref stripped
template <typename Range>
using value_type =
remove_cvref_t<decltype(*detail::range_begin(std::declval<Range>()))>;
using uncvref_type = remove_cvref_t<range_reference_type<Range>>;

template <typename OutputIt> OutputIt write_delimiter(OutputIt out) {
*out++ = ',';
Expand Down Expand Up @@ -582,43 +601,92 @@ template <typename T, typename Char> struct is_range {
!std::is_constructible<detail::std_string_view<Char>, T>::value;
};

template <typename T, typename Char>
namespace detail {
template <typename Context, typename Element> struct range_mapper {
using mapper = arg_mapper<Context>;

template <typename T,
FMT_ENABLE_IF(has_formatter<remove_cvref_t<T>, Context>::value)>
static auto map(T&& value) -> T&& {
return static_cast<T&&>(value);
}
template <typename T,
FMT_ENABLE_IF(!has_formatter<remove_cvref_t<T>, Context>::value)>
static auto map(T&& value)
-> decltype(mapper().map(static_cast<T&&>(value))) {
return mapper().map(static_cast<T&&>(value));
}
};

template <typename Char, typename Element>
using range_formatter_type =
conditional_t<is_formattable<Element, Char>::value,
formatter<remove_cvref_t<decltype(
range_mapper<buffer_context<Char>, Element>{}
.map(std::declval<Element>()))>,
Char>,
fallback_formatter<Element, Char>>;
} // namespace detail

template <typename R, typename Char>
struct formatter<
T, Char,
enable_if_t<
fmt::is_range<T, Char>::value
R, Char,
enable_if_t<fmt::is_range<R, Char>::value
// Workaround a bug in MSVC 2019 and earlier.
#if !FMT_MSC_VER
&& (is_formattable<detail::value_type<T>, Char>::value ||
detail::has_fallback_formatter<detail::value_type<T>, Char>::value)
&& (is_formattable<detail::uncvref_type<R>, Char>::value ||
detail::has_fallback_formatter<detail::uncvref_type<R>,
Char>::value)
#endif
>> {
>> {

using formatter_type =
detail::range_formatter_type<Char, detail::uncvref_type<R>>;
formatter_type underlying_;
bool custom_specs_ = false;

template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
auto it = ctx.begin();
auto end = ctx.end();
if (it == end || *it == '}') return it;

if (*it != ':')
FMT_THROW(format_error("no top-level range formatters supported"));

custom_specs_ = true;
++it;
ctx.advance_to(it);
return underlying_.parse(ctx);
}

template <
typename FormatContext, typename U,
FMT_ENABLE_IF(
std::is_same<U, conditional_t<detail::has_const_begin_end<T>::value,
const T, T>>::value)>
std::is_same<U, conditional_t<detail::has_const_begin_end<R>::value,
const R, R>>::value)>
auto format(U& range, FormatContext& ctx) -> decltype(ctx.out()) {
#ifdef FMT_DEPRECATED_BRACED_RANGES
Char prefix = '{';
Char postfix = '}';
#else
Char prefix = detail::is_set<T>::value ? '{' : '[';
Char postfix = detail::is_set<T>::value ? '}' : ']';
Char prefix = detail::is_set<R>::value ? '{' : '[';
Char postfix = detail::is_set<R>::value ? '}' : ']';
#endif
detail::range_mapper<buffer_context<Char>, detail::uncvref_type<R>> mapper;
auto out = ctx.out();
*out++ = prefix;
int i = 0;
auto it = std::begin(range);
auto end = std::end(range);
for (; it != end; ++it) {
if (i > 0) out = detail::write_delimiter(out);
out = detail::write_range_entry<Char>(out, *it);
if (custom_specs_) {
ctx.advance_to(out);
out = underlying_.format(mapper.map(*it), ctx);
} else {
out = detail::write_range_entry<Char>(out, *it);
}
++i;
}
*out++ = postfix;
Expand All @@ -629,14 +697,14 @@ struct formatter<
template <typename T, typename Char>
struct formatter<
T, Char,
enable_if_t<
detail::is_map<T>::value
enable_if_t<detail::is_map<T>::value
// Workaround a bug in MSVC 2019 and earlier.
#if !FMT_MSC_VER
&& (is_formattable<detail::value_type<T>, Char>::value ||
detail::has_fallback_formatter<detail::value_type<T>, Char>::value)
&& (is_formattable<detail::uncvref_type<T>, Char>::value ||
detail::has_fallback_formatter<detail::uncvref_type<T>,
Char>::value)
#endif
>> {
>> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
Expand Down
3 changes: 3 additions & 0 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ TEST(ranges_test, format_array_of_literals) {
TEST(ranges_test, format_vector) {
auto v = std::vector<int>{1, 2, 3, 5, 7, 11};
EXPECT_EQ(fmt::format("{}", v), "[1, 2, 3, 5, 7, 11]");
EXPECT_EQ(fmt::format("{::#x}", v), "[0x1, 0x2, 0x3, 0x5, 0x7, 0xb]");
}

TEST(ranges_test, format_vector2) {
auto v = std::vector<std::vector<int>>{{1, 2}, {3, 5}, {7, 11}};
EXPECT_EQ(fmt::format("{}", v), "[[1, 2], [3, 5], [7, 11]]");
EXPECT_EQ(fmt::format("{:::#x}", v), "[[0x1, 0x2], [0x3, 0x5], [0x7, 0xb]]");
}

TEST(ranges_test, format_map) {
Expand Down Expand Up @@ -296,6 +298,7 @@ static_assert(std::input_iterator<cpp20_only_range::iterator>);
TEST(ranges_test, join_sentinel) {
auto hello = zstring{"hello"};
EXPECT_EQ(fmt::format("{}", hello), "['h', 'e', 'l', 'l', 'o']");
EXPECT_EQ(fmt::format("{::}", hello), "[h, e, l, l, o]");
EXPECT_EQ(fmt::format("{}", fmt::join(hello, "_")), "h_e_l_l_o");
}

Expand Down

0 comments on commit 6e0f139

Please sign in to comment.