Skip to content

Commit

Permalink
use detail::write instead of re-implementing it
Browse files Browse the repository at this point in the history
  • Loading branch information
rimathia committed Nov 12, 2020
1 parent fd16204 commit 104b3de
Showing 1 changed file with 3 additions and 37 deletions.
40 changes: 3 additions & 37 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,40 +330,6 @@ template <typename T> struct printf_formatter {
}
};

namespace detail {
template <typename, typename = void>
struct has_container_append : std::false_type {};

template <typename OutputIt>
struct has_container_append<
OutputIt,
void_t<decltype(
get_container(std::declval<OutputIt>())
.append(std::declval<
const typename OutputIt::container_type::value_type*>(),
std::declval<const typename OutputIt::container_type::
value_type*>()))>> : std::true_type {};

template <typename OutputIt>
enable_if_t<has_container_append<OutputIt>::value, OutputIt>
container_aware_copy(const typename OutputIt::container_type::value_type* first,
const typename OutputIt::container_type::value_type* last,
OutputIt out) {
detail::get_container(out).append(first, last);
return out;
}

static_assert(has_container_append<buffer_appender<char>>::value,
"has_container_append doesn't work");

template <typename OutputIt, typename Char>
enable_if_t<!has_container_append<OutputIt>::value, OutputIt>
container_aware_copy(const Char* first, const Char* last, OutputIt out) {
return std::copy(first, last, out);
}

} // namespace detail

/**
This template formats data and writes the output through an output iterator.
*/
Expand Down Expand Up @@ -514,11 +480,11 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
}
char_type c = *it++;
if (it != end && *it == c) {
out = container_aware_copy(start, it, out);
out = detail::write(out, basic_string_view<Char>(start, it - start));
start = ++it;
continue;
}
out = container_aware_copy(start, it - 1, out);
out = detail::write(out, basic_string_view<Char>(start, it - 1 - start));

format_specs specs;
specs.align = align::right;
Expand Down Expand Up @@ -630,7 +596,7 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
// Format argument.
out = visit_format_arg(ArgFormatter(out, specs, *this), arg);
}
return container_aware_copy(start, it, out);
return detail::write(out, basic_string_view<Char>(start, it - start));
}

template <typename Char>
Expand Down

0 comments on commit 104b3de

Please sign in to comment.