address feedback

This commit is contained in:
Walter Gray 2020-12-23 19:35:47 -08:00
parent 06a576c9dd
commit 99ed2a8464
2 changed files with 12 additions and 29 deletions

View File

@ -37,19 +37,13 @@ struct formatting_range : formatting_base<Char> {
FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the
// range.
Char prefix = '{';
Char delimiter = ',';
Char postfix = '}';
static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
};
template <typename Char, typename Enable = void>
struct formatting_tuple : formatting_base<Char> {
Char prefix = '(';
Char delimiter = ',';
Char postfix = ')';
static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
};
namespace detail {
@ -247,23 +241,15 @@ template <typename Range>
using value_type =
remove_cvref_t<decltype(*detail::range_begin(std::declval<Range>()))>;
template <typename OutputIt, typename Formatting>
OutputIt add_range_formatting_spaces(OutputIt out,
const Formatting& formatting) {
if (const_check(formatting.add_prepostfix_space)) {
*out++ = ' ';
}
out = detail::copy(formatting.delimiter, out);
if (const_check(formatting.add_delimiter_spaces)) {
*out++ = ' ';
}
template <typename OutputIt> OutputIt write_delimiter(OutputIt out) {
*out++ = ',';
*out++ = ' ';
return out;
}
template <
typename Char, typename OutputIt, typename Arg,
FMT_ENABLE_IF(std::is_same<Arg, const char*>::value ||
is_like_std_string<typename std::decay<Arg>::type>::value)>
FMT_ENABLE_IF(is_like_std_string<typename std::decay<Arg>::type>::value)>
OutputIt write_range_entry(OutputIt out, const Arg& v) {
*out++ = '"';
out = write<Char>(out, v);
@ -282,8 +268,7 @@ OutputIt write_range_entry(OutputIt out, const Arg v) {
template <
typename Char, typename OutputIt, typename Arg,
FMT_ENABLE_IF(!std::is_same<Arg, const char*>::value &&
!is_like_std_string<typename std::decay<Arg>::type>::value &&
FMT_ENABLE_IF(!is_like_std_string<typename std::decay<Arg>::type>::value &&
!std::is_same<Arg, Char>::value)>
OutputIt write_range_entry(OutputIt out, const Arg& v) {
return write<Char>(out, v);
@ -303,7 +288,7 @@ struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
template <typename FormatContext> struct format_each {
template <typename T> void operator()(const T& v) {
if (i > 0) {
out = add_range_formatting_spaces(out, formatting);
out = write_delimiter(out);
}
out = detail::write_range_entry<Char>(out, v);
@ -328,12 +313,9 @@ struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) {
auto out = ctx.out();
size_t i = 0;
detail::copy(formatting.prefix, out);
detail::copy(formatting.prefix, out);
detail::for_each(values, format_each<FormatContext>{formatting, i, out});
if (formatting.add_prepostfix_space) {
*out++ = ' ';
}
detail::copy(formatting.postfix, out);
return ctx.out();
@ -375,7 +357,7 @@ struct formatter<
auto end = view.end();
for (; it != end; ++it) {
if (i > 0) {
out = detail::add_range_formatting_spaces(out, formatting);
out = detail::write_delimiter(out);
}
out = detail::write_range_entry<Char>(out, *it);
@ -385,9 +367,6 @@ struct formatter<
break;
}
}
if (formatting.add_prepostfix_space) {
*out++ = ' ';
}
return detail::copy(formatting.postfix, out);
}
};

View File

@ -243,6 +243,10 @@ TEST(RangesTest, Range) {
const std::vector<int> z(3u, 0);
EXPECT_EQ("{0, 0, 0}", fmt::format("{}", z));
const char* array_of_string_literals[] = {"1234", "abcd"};
EXPECT_EQ("{\"1234\", \"abcd\"}",
fmt::format("{}", array_of_string_literals));
}
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927