This commit is contained in:
Victor Zverovich 2024-09-08 11:07:00 -07:00
parent 047bf75c24
commit 02c5d637c5
5 changed files with 27 additions and 30 deletions

View File

@ -15,7 +15,7 @@
#ifndef FMT_MODULE #ifndef FMT_MODULE
# include <limits.h> // CHAR_BIT # include <limits.h> // CHAR_BIT
# include <stdio.h> // FILE # include <stdio.h> // FILE
# include <string.h> // strlen # include <string.h> // memcmp
// <cstddef> is also included transitively from <type_traits>. // <cstddef> is also included transitively from <type_traits>.
# include <cstddef> // std::byte # include <cstddef> // std::byte
@ -2956,7 +2956,7 @@ template <typename OutputIt, typename... T,
char>::value)> char>::value)>
FMT_INLINE auto format_to(OutputIt&& out, format_string<T...> fmt, T&&... args) FMT_INLINE auto format_to(OutputIt&& out, format_string<T...> fmt, T&&... args)
-> remove_cvref_t<OutputIt> { -> remove_cvref_t<OutputIt> {
return vformat_to(FMT_FWD(out), fmt, vargs<T...>{{args...}}); return vformat_to(FMT_FWD(out), fmt.str, vargs<T...>{{args...}});
} }
template <typename OutputIt> struct format_to_n_result { template <typename OutputIt> struct format_to_n_result {
@ -2986,7 +2986,7 @@ template <typename OutputIt, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)> FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string<T...> fmt, FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string<T...> fmt,
T&&... args) -> format_to_n_result<OutputIt> { T&&... args) -> format_to_n_result<OutputIt> {
return vformat_to_n(out, n, fmt, vargs<T...>{{args...}}); return vformat_to_n(out, n, fmt.str, vargs<T...>{{args...}});
} }
struct format_to_result { struct format_to_result {
@ -3041,8 +3041,9 @@ FMT_API void vprintln(FILE* f, string_view fmt, format_args args);
template <typename... T> template <typename... T>
FMT_INLINE void print(format_string<T...> fmt, T&&... args) { FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
fmt::vargs<T...> vargs = {{args...}}; fmt::vargs<T...> vargs = {{args...}};
if (!FMT_USE_UTF8) return detail::vprint_mojibake(stdout, fmt, vargs, false); if (!FMT_USE_UTF8)
return detail::is_locking<T...>() ? vprint_buffered(stdout, fmt, vargs) return detail::vprint_mojibake(stdout, fmt.str, vargs, false);
return detail::is_locking<T...>() ? vprint_buffered(stdout, fmt.str, vargs)
: vprint(fmt.str, vargs); : vprint(fmt.str, vargs);
} }
@ -3057,8 +3058,8 @@ FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
template <typename... T> template <typename... T>
FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) { FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) {
fmt::vargs<T...> vargs = {{args...}}; fmt::vargs<T...> vargs = {{args...}};
if (!FMT_USE_UTF8) return detail::vprint_mojibake(f, fmt, vargs, false); if (!FMT_USE_UTF8) return detail::vprint_mojibake(f, fmt.str, vargs, false);
return detail::is_locking<T...>() ? vprint_buffered(f, fmt, vargs) return detail::is_locking<T...>() ? vprint_buffered(f, fmt.str, vargs)
: vprint(f, fmt.str, vargs); : vprint(f, fmt.str, vargs);
} }
@ -3067,8 +3068,8 @@ FMT_INLINE void print(FILE* f, format_string<T...> fmt, T&&... args) {
template <typename... T> template <typename... T>
FMT_INLINE void println(FILE* f, format_string<T...> fmt, T&&... args) { FMT_INLINE void println(FILE* f, format_string<T...> fmt, T&&... args) {
fmt::vargs<T...> vargs = {{args...}}; fmt::vargs<T...> vargs = {{args...}};
return FMT_USE_UTF8 ? vprintln(f, fmt, vargs) return FMT_USE_UTF8 ? vprintln(f, fmt.str, vargs)
: detail::vprint_mojibake(f, fmt, vargs, true); : detail::vprint_mojibake(f, fmt.str, vargs, true);
} }
/// Formats `args` according to specifications in `fmt` and writes the output /// Formats `args` according to specifications in `fmt` and writes the output

View File

@ -484,7 +484,7 @@ inline void vprint(FILE* f, const text_style& ts, string_view fmt,
template <typename... T> template <typename... T>
void print(FILE* f, const text_style& ts, format_string<T...> fmt, void print(FILE* f, const text_style& ts, format_string<T...> fmt,
T&&... args) { T&&... args) {
vprint(f, ts, fmt, vargs<T...>{{args...}}); vprint(f, ts, fmt.str, vargs<T...>{{args...}});
} }
/** /**
@ -523,7 +523,7 @@ inline auto vformat(const text_style& ts, string_view fmt, format_args args)
template <typename... T> template <typename... T>
inline auto format(const text_style& ts, format_string<T...> fmt, T&&... args) inline auto format(const text_style& ts, format_string<T...> fmt, T&&... args)
-> std::string { -> std::string {
return fmt::vformat(ts, fmt, vargs<T...>{{args...}}); return fmt::vformat(ts, fmt.str, vargs<T...>{{args...}});
} }
/// Formats a string with the given text_style and writes the output to `out`. /// Formats a string with the given text_style and writes the output to `out`.
@ -550,7 +550,7 @@ template <typename OutputIt, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)> FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
inline auto format_to(OutputIt out, const text_style& ts, inline auto format_to(OutputIt out, const text_style& ts,
format_string<T...> fmt, T&&... args) -> OutputIt { format_string<T...> fmt, T&&... args) -> OutputIt {
return vformat_to(out, ts, fmt, vargs<T...>{{args...}}); return vformat_to(out, ts, fmt.str, vargs<T...>{{args...}});
} }
template <typename T, typename Char> template <typename T, typename Char>

View File

@ -3919,7 +3919,7 @@ FMT_API auto vsystem_error(int error_code, string_view format_str,
template <typename... T> template <typename... T>
auto system_error(int error_code, format_string<T...> fmt, T&&... args) auto system_error(int error_code, format_string<T...> fmt, T&&... args)
-> std::system_error { -> std::system_error {
return vsystem_error(error_code, fmt, vargs<T...>{{args...}}); return vsystem_error(error_code, fmt.str, vargs<T...>{{args...}});
} }
/** /**
@ -4327,7 +4327,7 @@ FMT_API auto vformat(string_view fmt, format_args args) -> std::string;
template <typename... T> template <typename... T>
FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args) FMT_NODISCARD FMT_INLINE auto format(format_string<T...> fmt, T&&... args)
-> std::string { -> std::string {
return vformat(fmt, vargs<T...>{{args...}}); return vformat(fmt.str, vargs<T...>{{args...}});
} }
template <typename Locale, FMT_ENABLE_IF(detail::is_locale<Locale>::value)> template <typename Locale, FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
@ -4340,7 +4340,7 @@ template <typename Locale, typename... T,
FMT_ENABLE_IF(detail::is_locale<Locale>::value)> FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
inline auto format(const Locale& loc, format_string<T...> fmt, T&&... args) inline auto format(const Locale& loc, format_string<T...> fmt, T&&... args)
-> std::string { -> std::string {
return fmt::vformat(loc, string_view(fmt), vargs<T...>{{args...}}); return fmt::vformat(loc, fmt.str, vargs<T...>{{args...}});
} }
template <typename OutputIt, typename Locale, template <typename OutputIt, typename Locale,
@ -4358,7 +4358,7 @@ template <typename OutputIt, typename Locale, typename... T,
detail::is_locale<Locale>::value)> detail::is_locale<Locale>::value)>
FMT_INLINE auto format_to(OutputIt out, const Locale& loc, FMT_INLINE auto format_to(OutputIt out, const Locale& loc,
format_string<T...> fmt, T&&... args) -> OutputIt { format_string<T...> fmt, T&&... args) -> OutputIt {
return vformat_to(out, loc, fmt, vargs<T...>{{args...}}); return vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
} }
template <typename Locale, typename... T, template <typename Locale, typename... T,
@ -4367,7 +4367,8 @@ FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
format_string<T...> fmt, format_string<T...> fmt,
T&&... args) -> size_t { T&&... args) -> size_t {
auto buf = detail::counting_buffer<>(); auto buf = detail::counting_buffer<>();
detail::vformat_to(buf, fmt, vargs<T...>{{args...}}, detail::locale_ref(loc)); detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}},
detail::locale_ref(loc));
return buf.count(); return buf.count();
} }

View File

@ -398,7 +398,7 @@ class FMT_API ostream : private detail::buffer<char> {
/// Formats `args` according to specifications in `fmt` and writes the /// Formats `args` according to specifications in `fmt` and writes the
/// output to the file. /// output to the file.
template <typename... T> void print(format_string<T...> fmt, T&&... args) { template <typename... T> void print(format_string<T...> fmt, T&&... args) {
vformat_to(appender(*this), fmt, vargs<T...>{{args...}}); vformat_to(appender(*this), fmt.str, vargs<T...>{{args...}});
} }
}; };

View File

@ -57,15 +57,6 @@ void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
} while (size != 0); } while (size != 0);
} }
template <typename Char, typename T>
void format_value(buffer<Char>& buf, const T& value) {
auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
auto&& output = std::basic_ostream<Char>(&format_buf);
output.imbue(std::locale::classic()); // The default is always unlocalized.
output << value;
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
}
template <typename T> struct streamed_view { template <typename T> struct streamed_view {
const T& value; const T& value;
}; };
@ -79,7 +70,11 @@ struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
template <typename T, typename Context> template <typename T, typename Context>
auto format(const T& value, Context& ctx) const -> decltype(ctx.out()) { auto format(const T& value, Context& ctx) const -> decltype(ctx.out()) {
auto buffer = basic_memory_buffer<Char>(); auto buffer = basic_memory_buffer<Char>();
detail::format_value(buffer, value); auto&& formatbuf = detail::formatbuf<std::basic_streambuf<Char>>(buffer);
auto&& output = std::basic_ostream<Char>(&formatbuf);
output.imbue(std::locale::classic()); // The default is always unlocalized.
output << value;
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
return formatter<basic_string_view<Char>, Char>::format( return formatter<basic_string_view<Char>, Char>::format(
{buffer.data(), buffer.size()}, ctx); {buffer.data(), buffer.size()}, ctx);
} }
@ -147,9 +142,9 @@ inline void vprint(std::ostream& os, string_view fmt, format_args args) {
FMT_EXPORT template <typename... T> FMT_EXPORT template <typename... T>
void print(std::ostream& os, format_string<T...> fmt, T&&... args) { void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
fmt::vargs<T...> vargs = {{args...}}; fmt::vargs<T...> vargs = {{args...}};
if (FMT_USE_UTF8) return vprint(os, fmt, vargs); if (FMT_USE_UTF8) return vprint(os, fmt.str, vargs);
auto buffer = memory_buffer(); auto buffer = memory_buffer();
detail::vformat_to(buffer, fmt, vargs); detail::vformat_to(buffer, fmt.str, vargs);
detail::write_buffer(os, buffer); detail::write_buffer(os, buffer);
} }