Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output Ranges for Functionality & Additional Safety #3756

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,86 @@ inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
fmt::make_format_args<buffer_context<Char>>(args...));
}

#if FMT_OUTPUT_RANGES
/**
Formats a string with the given text_style and writes the output to ``out``.
*/
template <typename Output, typename Char,
FMT_ENABLE_IF(std::ranges::output_range<Output, Char> ||
std::output_iterator<remove_cvref_t<Output>, Char>)>
auto vformat_to(Output&& out, const text_style& ts,
basic_string_view<Char> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
auto&& buf = detail::get_appendable_buffer<Char>(std::forward<Output>(out));
detail::vformat_to(buf, ts, format_str, args);
return detail::get_iterator(buf, out);
}

/**
\rst
Formats arguments with the given text_style, writes the result to the output
iterator or range ``out`` and returns the iterator past the end of the output
range.

**Example**::

std::vector<char> out;
fmt::format_to(std::back_inserter(out),
fmt::emphasis::bold | fg(fmt::color::red), "{}", 42);
\endrst
*/
template <typename Output, typename S, typename... Args,
bool enable =
(std::ranges::output_range<Output, char_t<S>> ||
std::output_iterator<remove_cvref_t<Output>,
char_t<S>>)&&detail::is_string<S>::value,
FMT_ENABLE_IF(enable)>
inline auto format_to(Output&& out, const text_style& ts, const S& format_str,
Args&&... args) {
return vformat_to(std::forward<Output>(out), ts,
detail::to_string_view(format_str),
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
}

/**
Formats a string with the given text_style and writes the output to ``out``.
*/
template <typename Output, typename Char,
FMT_ENABLE_IF(std::ranges::output_range<Output, Char> ||
std::output_iterator<remove_cvref_t<Output>, Char>)>
auto vformat_into(
Output&& out, const text_style& ts, basic_string_view<Char> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
auto&& buf = detail::get_buffer<Char>(std::forward<Output>(out));
detail::vformat_to(buf, ts, format_str, args);
return detail::get_iterator(buf, out);
}

/**
\rst
Formats arguments with the given text_style, writes the result to the output
iterator ``out`` and returns the iterator past the end of the output range.

**Example**::

std::vector<char> out;
fmt::format_to(std::back_inserter(out),
fmt::emphasis::bold | fg(fmt::color::red), "{}", 42);
\endrst
*/
template <typename Output, typename S, typename... Args,
bool enable =
(std::ranges::output_range<Output, char_t<S>> ||
std::output_iterator<remove_cvref_t<Output>,
char_t<S>>)&&detail::is_string<S>::value,
FMT_ENABLE_IF(enable)>
inline auto format_into(Output&& out, const text_style& ts, const S& format_str,
Args&&... args) {
return vformat_into(
std::forward<Output>(out), ts, detail::to_string_view(format_str),
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
}
#else
/**
Formats a string with the given text_style and writes the output to ``out``.
*/
Expand Down Expand Up @@ -573,6 +653,7 @@ inline auto format_to(OutputIt out, const text_style& ts, const S& format_str,
return vformat_to(out, ts, detail::to_string_view(format_str),
fmt::make_format_args<buffer_context<char_t<S>>>(args...));
}
#endif

template <typename T, typename Char>
struct formatter<detail::styled_arg<T>, Char> : formatter<T, Char> {
Expand Down
34 changes: 34 additions & 0 deletions include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,39 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
}
}

# if FMT_OUTPUT_RANGES
template <typename Output, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
FMT_CONSTEXPR auto format_to(Output&& out, const S&, Args&&... args) {
constexpr auto compiled = detail::compile<Args...>(S());
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
detail::unknown_format>()) {
return fmt::format_to(
std::forward<Output>(out),
static_cast<basic_string_view<typename S::char_type>>(S()),
std::forward<Args>(args)...);
} else {
return fmt::format_to(std::forward<Output>(out), compiled,
std::forward<Args>(args)...);
}
}

template <typename Output, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
FMT_CONSTEXPR auto format_into(Output&& out, const S&, Args&&... args) {
constexpr auto compiled = detail::compile<Args...>(S());
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
detail::unknown_format>()) {
return fmt::format_into(
std::forward<Output>(out),
static_cast<basic_string_view<typename S::char_type>>(S()),
std::forward<Args>(args)...);
} else {
return fmt::format_into(std::forward<Output>(out), compiled,
std::forward<Args>(args)...);
}
}
# else
template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
Expand All @@ -484,6 +517,7 @@ FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
return fmt::format_to(out, compiled, std::forward<Args>(args)...);
}
}
# endif
#endif

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