Skip to content

Commit

Permalink
Added range_format::string formatter
Browse files Browse the repository at this point in the history
Either the basic_string_view<Char> range constructor or a user-defined conversion operator will be used.
  • Loading branch information
matt77hias committed May 23, 2024
1 parent b817610 commit 4684c03
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ struct formatter<
enable_if_t<conjunction<
bool_constant<range_format_kind<R, Char>::value !=
range_format::disabled &&
range_format_kind<R, Char>::value != range_format::map>
range_format_kind<R, Char>::value != range_format::map &&
range_format_kind<R, Char>::value != range_format::string>
// Workaround a bug in MSVC 2015 and earlier.
#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910
,
Expand Down Expand Up @@ -604,6 +605,30 @@ struct formatter<
}
};

// A string formatter.
template <typename R, typename Char>
struct formatter<
R, Char,
enable_if_t<range_format_kind<R, Char>::value == range_format::string>> {
private:
using range_type = detail::maybe_const_range<R>;
formatter<detail::std_string_view<Char>, Char> range_formatter_;

public:
FMT_CONSTEXPR formatter() {}

template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return range_formatter_.parse(ctx);
}

template <typename FormatContext>
auto format(range_type& range, FormatContext& ctx) const
-> decltype(ctx.out()) {
return range_formatter_.format(detail::std_string_view<Char>(range), ctx);
}
};

template <typename It, typename Sentinel, typename Char = char>
struct join_view : detail::view {
It begin;
Expand Down

0 comments on commit 4684c03

Please sign in to comment.