Skip to content

Commit

Permalink
🎉 Array overload safety for base.h only
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Jan 14, 2024
1 parent 123e058 commit acc60e6
Showing 1 changed file with 54 additions and 5 deletions.
59 changes: 54 additions & 5 deletions include/fmt/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@
# define FMT_UNICODE !FMT_MSC_VERSION
#endif

#define FMT_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)

// Enable minimal optimizations for more compact code in debug mode.
FMT_GCC_PRAGMA("GCC push_options")
#if !defined(__OPTIMIZE__) && !defined(__CUDACC__)
Expand Down Expand Up @@ -2809,15 +2811,43 @@ using format_string = basic_format_string<char, type_identity_t<Args>...>;
inline auto runtime(string_view s) -> runtime_format_string<> { return {{s}}; }
#endif

template <typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;
/** Formats a string and writes the output to ``out``. */
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 {
FMT_ENABLE_IF(detail::is_output_iterator<remove_cvref_t<OutputIt>,
char>::value)>
auto vformat_to(OutputIt&& out, string_view fmt, format_args args)
-> remove_cvref_t<OutputIt> {
auto&& buf = detail::get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, {});
return detail::get_iterator(buf, out);
}

template <typename OutputIt, typename OutputSen = OutputIt>
struct format_to_result {
/** Iterator past the end of the last write. */
OutputIt out;
/** Iterator to the end of the output range. */
OutputSen out_last;

FMT_CONSTEXPR operator OutputIt&() & noexcept { return out; }
FMT_CONSTEXPR operator const OutputIt&() const& noexcept { return out; }
FMT_CONSTEXPR operator OutputIt&&() && noexcept {
return static_cast<OutputIt&&>(out);
}
};

/** Formats a string and writes the output to ``out``. */
template <size_t Size>
auto vformat_to(char (&out)[Size], string_view fmt, format_args args)
-> format_to_result<char*> {
using traits = detail::fixed_buffer_traits;
auto buf = detail::iterator_buffer<char*, char, traits>(out, Size);
detail::vformat_to(buf, fmt, args, {});
return {out + buf.count(), out + Size};
}

/**
\rst
Formats ``args`` according to specifications in ``fmt``, writes the result to
Expand All @@ -2831,9 +2861,28 @@ auto vformat_to(OutputIt out, string_view fmt, format_args args) -> OutputIt {
\endrst
*/
template <typename OutputIt, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
FMT_INLINE auto format_to(OutputIt out, format_string<T...> fmt, T&&... args)
-> OutputIt {
FMT_ENABLE_IF(detail::is_output_iterator<remove_cvref_t<OutputIt>,
char>::value)>
FMT_INLINE auto format_to(OutputIt&& out, format_string<T...> fmt, T&&... args)
-> remove_cvref_t<OutputIt> {
return vformat_to(FMT_FWD(out), fmt, fmt::make_format_args(args...));
}

/**
\rst
Formats ``args`` according to specifications in ``fmt``, writes the result to
the output iterator ``out`` and returns the iterator past the end of the output
range. `format_to` does not append a terminating null character.
**Example**::
auto out = std::vector<char>();
fmt::format_to(std::back_inserter(out), "{}", 42);
\endrst
*/
template <size_t Size, typename... T>
FMT_INLINE auto format_to(char (&out)[Size], format_string<T...> fmt,
T&&... args) -> format_to_result<char*> {
return vformat_to(out, fmt, fmt::make_format_args(args...));
}

Expand Down

0 comments on commit acc60e6

Please sign in to comment.