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 27, 2024
1 parent b817610 commit 9a7743a
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ template <typename T, typename C> class is_tuple_formattable_<T, C, true> {
static auto all_true(...) -> std::false_type;

template <size_t... Is>
static auto check(index_sequence<Is...>) -> decltype(all_true(
index_sequence<Is...>{},
integer_sequence<bool,
(is_formattable<typename std::tuple_element<Is, T>::type,
C>::value)...>{}));
static auto check(index_sequence<Is...>)
-> decltype(all_true(
index_sequence<Is...>{},
integer_sequence<
bool, (is_formattable<typename std::tuple_element<Is, T>::type,
C>::value)...>{}));

public:
static constexpr const bool value =
Expand Down Expand Up @@ -334,8 +335,8 @@ struct formatter<Tuple, Char,
}

template <typename FormatContext>
auto format(const Tuple& value, FormatContext& ctx) const
-> decltype(ctx.out()) {
auto format(const Tuple& value,
FormatContext& ctx) const -> decltype(ctx.out()) {
ctx.advance_to(detail::copy<Char>(opening_bracket_, ctx.out()));
detail::for_each2(
formatters_, value,
Expand Down Expand Up @@ -445,7 +446,15 @@ struct range_formatter<
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin();
auto end = ctx.end();
detail::maybe_set_debug_format(underlying_, true);
switch (range_format_kind<T, Char>::value) {
case range_format::debug_string:
case range_format::string:
break;
default:
detail::maybe_set_debug_format(underlying_, true);
break;
}

if (it == end) return underlying_.parse(ctx);

switch (detail::to_ascii(*it)) {
Expand Down Expand Up @@ -528,11 +537,27 @@ struct formatter<

public:
FMT_CONSTEXPR formatter() {
if (detail::const_check(range_format_kind<R, Char>::value !=
range_format::set))
return;
range_formatter_.set_brackets(detail::string_literal<Char, '{'>{},
detail::string_literal<Char, '}'>{});
switch (range_format_kind<R, Char>::value) {
case range_format::debug_string: {
range_formatter_.set_brackets(detail::string_literal<Char, '"'>{},
detail::string_literal<Char, '"'>{});
range_formatter_.set_separator({});
break;
}
case range_format::set: {
range_formatter_.set_brackets(detail::string_literal<Char, '{'>{},
detail::string_literal<Char, '}'>{});
break;
}
case range_format::string: {
range_formatter_.set_brackets({}, {});
range_formatter_.set_separator({});
break;
}
default: {
break;
}
}
}

template <typename ParseContext>
Expand Down

0 comments on commit 9a7743a

Please sign in to comment.