Skip to content

Commit 12fca71

Browse files
committed
Added range_format::string formatter
Either the basic_string_view<Char> range constructor or a user-defined conversion operator will be used.
1 parent b817610 commit 12fca71

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Diff for: include/fmt/ranges.h

+26-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ struct formatter<
515515
enable_if_t<conjunction<
516516
bool_constant<range_format_kind<R, Char>::value !=
517517
range_format::disabled &&
518-
range_format_kind<R, Char>::value != range_format::map>
518+
range_format_kind<R, Char>::value != range_format::map &&
519+
range_format_kind<R, Char>::value != range_format::string>
519520
// Workaround a bug in MSVC 2015 and earlier.
520521
#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910
521522
,
@@ -604,6 +605,30 @@ struct formatter<
604605
}
605606
};
606607

608+
// A string formatter.
609+
template <typename R, typename Char>
610+
struct formatter<
611+
R, Char,
612+
enable_if_t<range_format_kind<R, Char>::value == range_format::string>> {
613+
private:
614+
using range_type = detail::maybe_const_range<R>;
615+
formatter<std::basic_string_view<Char>, Char> range_formatter_;
616+
617+
public:
618+
FMT_CONSTEXPR formatter() {}
619+
620+
template <typename ParseContext>
621+
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
622+
return range_formatter_.parse(ctx);
623+
}
624+
625+
template <typename FormatContext>
626+
auto format(range_type& range, FormatContext& ctx) const
627+
-> decltype(ctx.out()) {
628+
return range_formatter_.format(std::basic_string_view<Char>(range), ctx);
629+
}
630+
};
631+
607632
template <typename It, typename Sentinel, typename Char = char>
608633
struct join_view : detail::view {
609634
It begin;

0 commit comments

Comments
 (0)