Skip to content

Commit

Permalink
Optimize writing to buffers via back_insert_iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 12, 2022
1 parent e2f6d76 commit 192859c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ auto write(OutputIt out, const std::tm& time, const std::locale& loc,
char format, char modifier = 0) -> OutputIt {
auto&& buf = get_buffer<Char>(out);
do_write<Char>(buf, time, loc, format, modifier);
return get_iterator(buf);
return get_iterator(buf, out);
}

template <typename Char, typename OutputIt,
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ OutputIt vformat_to(
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
auto&& buf = detail::get_buffer<Char>(out);
detail::vformat_to(buf, ts, format_str, args);
return detail::get_iterator(buf);
return detail::get_iterator(buf, out);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1123,13 +1123,19 @@ template <typename T, typename OutputIt>
auto get_buffer(OutputIt out) -> iterator_buffer<OutputIt, T> {
return iterator_buffer<OutputIt, T>(out);
}
template <typename T, typename Buf,
FMT_ENABLE_IF(std::is_base_of<buffer<char>, Buf>::value)>
auto get_buffer(std::back_insert_iterator<Buf> out) -> buffer<char>& {
return get_container(out);
}

template <typename Buffer>
FMT_INLINE auto get_iterator(Buffer& buf) -> decltype(buf.out()) {
template <typename Buf, typename OutputIt>
FMT_INLINE auto get_iterator(Buf& buf, OutputIt) -> decltype(buf.out()) {
return buf.out();
}
template <typename T> auto get_iterator(buffer<T>& buf) -> buffer_appender<T> {
return buffer_appender<T>(buf);
template <typename T, typename OutputIt>
auto get_iterator(buffer<T>&, OutputIt out) -> OutputIt {
return out;
}

template <typename T, typename Char = char, typename Enable = void>
Expand Down Expand Up @@ -1556,11 +1562,6 @@ FMT_END_DETAIL_NAMESPACE
class appender : public std::back_insert_iterator<detail::buffer<char>> {
using base = std::back_insert_iterator<detail::buffer<char>>;

template <typename T>
friend auto get_buffer(appender out) -> detail::buffer<char>& {
return detail::get_container(out);
}

public:
using std::back_insert_iterator<detail::buffer<char>>::back_insert_iterator;
appender(base it) noexcept : base(it) {}
Expand Down Expand Up @@ -3224,10 +3225,9 @@ FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args)
template <typename OutputIt,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
auto vformat_to(OutputIt out, string_view fmt, format_args args) -> OutputIt {
using detail::get_buffer;
auto&& buf = get_buffer<char>(out);
auto&& buf = detail::get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, {});
return detail::get_iterator(buf);
return detail::get_iterator(buf, out);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4276,7 +4276,7 @@ auto vformat_to(OutputIt out, const Locale& loc, string_view fmt,
using detail::get_buffer;
auto&& buf = get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc));
return detail::get_iterator(buf);
return detail::get_iterator(buf, out);
}

template <typename OutputIt, typename Locale, typename... T,
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ auto vformat_to(OutputIt out, const S& format_str,
-> OutputIt {
auto&& buf = detail::get_buffer<Char>(out);
detail::vformat_to(buf, detail::to_string_view(format_str), args);
return detail::get_iterator(buf);
return detail::get_iterator(buf, out);
}

template <typename OutputIt, typename S, typename... Args,
Expand Down

0 comments on commit 192859c

Please sign in to comment.