Update: fmt to 10.2.0

pull/661/head
Rubidium 4 months ago committed by rubidium42
parent 80ebcc72fb
commit 79b684b8ac

@ -18,7 +18,7 @@
#include <ostream> #include <ostream>
#include <type_traits> #include <type_traits>
#include "format.h" #include "ostream.h" // formatbuf
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
@ -72,7 +72,8 @@ template <typename To, typename From,
FMT_ENABLE_IF(!std::is_same<From, To>::value && FMT_ENABLE_IF(!std::is_same<From, To>::value &&
std::numeric_limits<From>::is_signed == std::numeric_limits<From>::is_signed ==
std::numeric_limits<To>::is_signed)> std::numeric_limits<To>::is_signed)>
FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) { FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec)
-> To {
ec = 0; ec = 0;
using F = std::numeric_limits<From>; using F = std::numeric_limits<From>;
using T = std::numeric_limits<To>; using T = std::numeric_limits<To>;
@ -101,7 +102,8 @@ template <typename To, typename From,
FMT_ENABLE_IF(!std::is_same<From, To>::value && FMT_ENABLE_IF(!std::is_same<From, To>::value &&
std::numeric_limits<From>::is_signed != std::numeric_limits<From>::is_signed !=
std::numeric_limits<To>::is_signed)> std::numeric_limits<To>::is_signed)>
FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) { FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec)
-> To {
ec = 0; ec = 0;
using F = std::numeric_limits<From>; using F = std::numeric_limits<From>;
using T = std::numeric_limits<To>; using T = std::numeric_limits<To>;
@ -133,7 +135,8 @@ FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) {
template <typename To, typename From, template <typename To, typename From,
FMT_ENABLE_IF(std::is_same<From, To>::value)> FMT_ENABLE_IF(std::is_same<From, To>::value)>
FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) { FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec)
-> To {
ec = 0; ec = 0;
return from; return from;
} // function } // function
@ -154,7 +157,7 @@ FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) {
// clang-format on // clang-format on
template <typename To, typename From, template <typename To, typename From,
FMT_ENABLE_IF(!std::is_same<From, To>::value)> FMT_ENABLE_IF(!std::is_same<From, To>::value)>
FMT_CONSTEXPR To safe_float_conversion(const From from, int& ec) { FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To {
ec = 0; ec = 0;
using T = std::numeric_limits<To>; using T = std::numeric_limits<To>;
static_assert(std::is_floating_point<From>::value, "From must be floating"); static_assert(std::is_floating_point<From>::value, "From must be floating");
@ -176,7 +179,7 @@ FMT_CONSTEXPR To safe_float_conversion(const From from, int& ec) {
template <typename To, typename From, template <typename To, typename From,
FMT_ENABLE_IF(std::is_same<From, To>::value)> FMT_ENABLE_IF(std::is_same<From, To>::value)>
FMT_CONSTEXPR To safe_float_conversion(const From from, int& ec) { FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To {
ec = 0; ec = 0;
static_assert(std::is_floating_point<From>::value, "From must be floating"); static_assert(std::is_floating_point<From>::value, "From must be floating");
return from; return from;
@ -188,8 +191,8 @@ FMT_CONSTEXPR To safe_float_conversion(const From from, int& ec) {
template <typename To, typename FromRep, typename FromPeriod, template <typename To, typename FromRep, typename FromPeriod,
FMT_ENABLE_IF(std::is_integral<FromRep>::value), FMT_ENABLE_IF(std::is_integral<FromRep>::value),
FMT_ENABLE_IF(std::is_integral<typename To::rep>::value)> FMT_ENABLE_IF(std::is_integral<typename To::rep>::value)>
To safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from, auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
int& ec) { int& ec) -> To {
using From = std::chrono::duration<FromRep, FromPeriod>; using From = std::chrono::duration<FromRep, FromPeriod>;
ec = 0; ec = 0;
// the basic idea is that we need to convert from count() in the from type // the basic idea is that we need to convert from count() in the from type
@ -240,8 +243,8 @@ To safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
template <typename To, typename FromRep, typename FromPeriod, template <typename To, typename FromRep, typename FromPeriod,
FMT_ENABLE_IF(std::is_floating_point<FromRep>::value), FMT_ENABLE_IF(std::is_floating_point<FromRep>::value),
FMT_ENABLE_IF(std::is_floating_point<typename To::rep>::value)> FMT_ENABLE_IF(std::is_floating_point<typename To::rep>::value)>
To safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from, auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
int& ec) { int& ec) -> To {
using From = std::chrono::duration<FromRep, FromPeriod>; using From = std::chrono::duration<FromRep, FromPeriod>;
ec = 0; ec = 0;
if (std::isnan(from.count())) { if (std::isnan(from.count())) {
@ -321,12 +324,12 @@ To safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
namespace detail { namespace detail {
template <typename T = void> struct null {}; template <typename T = void> struct null {};
inline null<> localtime_r FMT_NOMACRO(...) { return null<>(); } inline auto localtime_r FMT_NOMACRO(...) -> null<> { return null<>(); }
inline null<> localtime_s(...) { return null<>(); } inline auto localtime_s(...) -> null<> { return null<>(); }
inline null<> gmtime_r(...) { return null<>(); } inline auto gmtime_r(...) -> null<> { return null<>(); }
inline null<> gmtime_s(...) { return null<>(); } inline auto gmtime_s(...) -> null<> { return null<>(); }
inline const std::locale& get_classic_locale() { inline auto get_classic_locale() -> const std::locale& {
static const auto& locale = std::locale::classic(); static const auto& locale = std::locale::classic();
return locale; return locale;
} }
@ -336,8 +339,6 @@ template <typename CodeUnit> struct codecvt_result {
CodeUnit buf[max_size]; CodeUnit buf[max_size];
CodeUnit* end; CodeUnit* end;
}; };
template <typename CodeUnit>
constexpr const size_t codecvt_result<CodeUnit>::max_size;
template <typename CodeUnit> template <typename CodeUnit>
void write_codecvt(codecvt_result<CodeUnit>& out, string_view in_buf, void write_codecvt(codecvt_result<CodeUnit>& out, string_view in_buf,
@ -377,8 +378,8 @@ auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc)
unit_t unit; unit_t unit;
write_codecvt(unit, in, loc); write_codecvt(unit, in, loc);
// In UTF-8 is used one to four one-byte code units. // In UTF-8 is used one to four one-byte code units.
unicode_to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>> auto u =
u; to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>();
if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)})) if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)}))
FMT_THROW(format_error("failed to format time")); FMT_THROW(format_error("failed to format time"));
return copy_str<char>(u.c_str(), u.c_str() + u.size(), out); return copy_str<char>(u.c_str(), u.c_str() + u.size(), out);
@ -408,8 +409,7 @@ inline void do_write(buffer<Char>& buf, const std::tm& time,
auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf); auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
auto&& os = std::basic_ostream<Char>(&format_buf); auto&& os = std::basic_ostream<Char>(&format_buf);
os.imbue(loc); os.imbue(loc);
using iterator = std::ostreambuf_iterator<Char>; const auto& facet = std::use_facet<std::time_put<Char>>(loc);
const auto& facet = std::use_facet<std::time_put<Char, iterator>>(loc);
auto end = facet.put(os, os, Char(' '), &time, format, modifier); auto end = facet.put(os, os, Char(' '), &time, format, modifier);
if (end.failed()) FMT_THROW(format_error("failed to format time")); if (end.failed()) FMT_THROW(format_error("failed to format time"));
} }
@ -432,6 +432,51 @@ auto write(OutputIt out, const std::tm& time, const std::locale& loc,
return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc); return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc);
} }
template <typename Rep1, typename Rep2>
struct is_same_arithmetic_type
: public std::integral_constant<bool,
(std::is_integral<Rep1>::value &&
std::is_integral<Rep2>::value) ||
(std::is_floating_point<Rep1>::value &&
std::is_floating_point<Rep2>::value)> {
};
template <
typename To, typename FromRep, typename FromPeriod,
FMT_ENABLE_IF(is_same_arithmetic_type<FromRep, typename To::rep>::value)>
auto fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
#if FMT_SAFE_DURATION_CAST
// Throwing version of safe_duration_cast is only available for
// integer to integer or float to float casts.
int ec;
To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
if (ec) FMT_THROW(format_error("cannot format duration"));
return to;
#else
// Standard duration cast, may overflow.
return std::chrono::duration_cast<To>(from);
#endif
}
template <
typename To, typename FromRep, typename FromPeriod,
FMT_ENABLE_IF(!is_same_arithmetic_type<FromRep, typename To::rep>::value)>
auto fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
// Mixed integer <-> float cast is not supported by safe_duration_cast.
return std::chrono::duration_cast<To>(from);
}
template <typename Duration>
auto to_time_t(
std::chrono::time_point<std::chrono::system_clock, Duration> time_point)
-> std::time_t {
// Cannot use std::chrono::system_clock::to_time_t since this would first
// require a cast to std::chrono::system_clock::time_point, which could
// overflow.
return fmt_duration_cast<std::chrono::duration<std::time_t>>(
time_point.time_since_epoch())
.count();
}
} // namespace detail } // namespace detail
FMT_BEGIN_EXPORT FMT_BEGIN_EXPORT
@ -441,29 +486,29 @@ FMT_BEGIN_EXPORT
expressed in local time. Unlike ``std::localtime``, this function is expressed in local time. Unlike ``std::localtime``, this function is
thread-safe on most platforms. thread-safe on most platforms.
*/ */
inline std::tm localtime(std::time_t time) { inline auto localtime(std::time_t time) -> std::tm {
struct dispatcher { struct dispatcher {
std::time_t time_; std::time_t time_;
std::tm tm_; std::tm tm_;
dispatcher(std::time_t t) : time_(t) {} dispatcher(std::time_t t) : time_(t) {}
bool run() { auto run() -> bool {
using namespace fmt::detail; using namespace fmt::detail;
return handle(localtime_r(&time_, &tm_)); return handle(localtime_r(&time_, &tm_));
} }
bool handle(std::tm* tm) { return tm != nullptr; } auto handle(std::tm* tm) -> bool { return tm != nullptr; }
bool handle(detail::null<>) { auto handle(detail::null<>) -> bool {
using namespace fmt::detail; using namespace fmt::detail;
return fallback(localtime_s(&tm_, &time_)); return fallback(localtime_s(&tm_, &time_));
} }
bool fallback(int res) { return res == 0; } auto fallback(int res) -> bool { return res == 0; }
#if !FMT_MSC_VERSION #if !FMT_MSC_VERSION
bool fallback(detail::null<>) { auto fallback(detail::null<>) -> bool {
using namespace fmt::detail; using namespace fmt::detail;
std::tm* tm = std::localtime(&time_); std::tm* tm = std::localtime(&time_);
if (tm) tm_ = *tm; if (tm) tm_ = *tm;
@ -480,8 +525,8 @@ inline std::tm localtime(std::time_t time) {
#if FMT_USE_LOCAL_TIME #if FMT_USE_LOCAL_TIME
template <typename Duration> template <typename Duration>
inline auto localtime(std::chrono::local_time<Duration> time) -> std::tm { inline auto localtime(std::chrono::local_time<Duration> time) -> std::tm {
return localtime(std::chrono::system_clock::to_time_t( return localtime(
std::chrono::current_zone()->to_sys(time))); detail::to_time_t(std::chrono::current_zone()->to_sys(time)));
} }
#endif #endif
@ -490,90 +535,49 @@ inline auto localtime(std::chrono::local_time<Duration> time) -> std::tm {
expressed in Coordinated Universal Time (UTC). Unlike ``std::gmtime``, this expressed in Coordinated Universal Time (UTC). Unlike ``std::gmtime``, this
function is thread-safe on most platforms. function is thread-safe on most platforms.
*/ */
inline std::tm gmtime(std::time_t time) { inline auto gmtime(std::time_t time) -> std::tm {
struct dispatcher { struct dispatcher {
std::time_t time_; std::time_t time_;
std::tm tm_; std::tm tm_;
dispatcher(std::time_t t) : time_(t) {} dispatcher(std::time_t t) : time_(t) {}
bool run() { auto run() -> bool {
using namespace fmt::detail; using namespace fmt::detail;
return handle(gmtime_r(&time_, &tm_)); return handle(gmtime_r(&time_, &tm_));
} }
bool handle(std::tm* tm) { return tm != nullptr; } auto handle(std::tm* tm) -> bool { return tm != nullptr; }
bool handle(detail::null<>) { auto handle(detail::null<>) -> bool {
using namespace fmt::detail; using namespace fmt::detail;
return fallback(gmtime_s(&tm_, &time_)); return fallback(gmtime_s(&tm_, &time_));
} }
bool fallback(int res) { return res == 0; } auto fallback(int res) -> bool { return res == 0; }
#if !FMT_MSC_VERSION #if !FMT_MSC_VERSION
bool fallback(detail::null<>) { auto fallback(detail::null<>) -> bool {
std::tm* tm = std::gmtime(&time_); std::tm* tm = std::gmtime(&time_);
if (tm) tm_ = *tm; if (tm) tm_ = *tm;
return tm != nullptr; return tm != nullptr;
} }
#endif #endif
}; };
dispatcher gt(time); auto gt = dispatcher(time);
// Too big time values may be unsupported. // Too big time values may be unsupported.
if (!gt.run()) FMT_THROW(format_error("time_t value out of range")); if (!gt.run()) FMT_THROW(format_error("time_t value out of range"));
return gt.tm_; return gt.tm_;
} }
inline std::tm gmtime( template <typename Duration>
std::chrono::time_point<std::chrono::system_clock> time_point) { inline auto gmtime(
return gmtime(std::chrono::system_clock::to_time_t(time_point)); std::chrono::time_point<std::chrono::system_clock, Duration> time_point)
-> std::tm {
return gmtime(detail::to_time_t(time_point));
} }
FMT_BEGIN_DETAIL_NAMESPACE namespace detail {
// DEPRECATED!
template <typename Char>
FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end,
format_specs<Char>& specs) -> const Char* {
FMT_ASSERT(begin != end, "");
auto align = align::none;
auto p = begin + code_point_length(begin);
if (end - p <= 0) p = begin;
for (;;) {
switch (to_ascii(*p)) {
case '<':
align = align::left;
break;
case '>':
align = align::right;
break;
case '^':
align = align::center;
break;
}
if (align != align::none) {
if (p != begin) {
auto c = *begin;
if (c == '}') return begin;
if (c == '{') {
throw_format_error("invalid fill character '{'");
return begin;
}
specs.fill = {begin, to_unsigned(p - begin)};
begin = p + 1;
} else {
++begin;
}
break;
} else if (p == begin) {
break;
}
p = begin;
}
specs.align = align;
return begin;
}
// Writes two-digit numbers a, b and c separated by sep to buf. // Writes two-digit numbers a, b and c separated by sep to buf.
// The method by Pavel Novikov based on // The method by Pavel Novikov based on
@ -609,7 +613,8 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b,
} }
} }
template <typename Period> FMT_CONSTEXPR inline const char* get_units() { template <typename Period>
FMT_CONSTEXPR inline auto get_units() -> const char* {
if (std::is_same<Period, std::atto>::value) return "as"; if (std::is_same<Period, std::atto>::value) return "as";
if (std::is_same<Period, std::femto>::value) return "fs"; if (std::is_same<Period, std::femto>::value) return "fs";
if (std::is_same<Period, std::pico>::value) return "ps"; if (std::is_same<Period, std::pico>::value) return "ps";
@ -627,8 +632,9 @@ template <typename Period> FMT_CONSTEXPR inline const char* get_units() {
if (std::is_same<Period, std::tera>::value) return "Ts"; if (std::is_same<Period, std::tera>::value) return "Ts";
if (std::is_same<Period, std::peta>::value) return "Ps"; if (std::is_same<Period, std::peta>::value) return "Ps";
if (std::is_same<Period, std::exa>::value) return "Es"; if (std::is_same<Period, std::exa>::value) return "Es";
if (std::is_same<Period, std::ratio<60>>::value) return "m"; if (std::is_same<Period, std::ratio<60>>::value) return "min";
if (std::is_same<Period, std::ratio<3600>>::value) return "h"; if (std::is_same<Period, std::ratio<3600>>::value) return "h";
if (std::is_same<Period, std::ratio<86400>>::value) return "d";
return nullptr; return nullptr;
} }
@ -664,9 +670,8 @@ auto write_padding(OutputIt out, pad_type pad) -> OutputIt {
// Parses a put_time-like format string and invokes handler actions. // Parses a put_time-like format string and invokes handler actions.
template <typename Char, typename Handler> template <typename Char, typename Handler>
FMT_CONSTEXPR const Char* parse_chrono_format(const Char* begin, FMT_CONSTEXPR auto parse_chrono_format(const Char* begin, const Char* end,
const Char* end, Handler&& handler) -> const Char* {
Handler&& handler) {
if (begin == end || *begin == '}') return begin; if (begin == end || *begin == '}') return begin;
if (*begin != '%') FMT_THROW(format_error("invalid format")); if (*begin != '%') FMT_THROW(format_error("invalid format"));
auto ptr = begin; auto ptr = begin;
@ -997,25 +1002,25 @@ struct tm_format_checker : null_chrono_spec_handler<tm_format_checker> {
FMT_CONSTEXPR void on_tz_name() {} FMT_CONSTEXPR void on_tz_name() {}
}; };
inline const char* tm_wday_full_name(int wday) { inline auto tm_wday_full_name(int wday) -> const char* {
static constexpr const char* full_name_list[] = { static constexpr const char* full_name_list[] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"}; "Thursday", "Friday", "Saturday"};
return wday >= 0 && wday <= 6 ? full_name_list[wday] : "?"; return wday >= 0 && wday <= 6 ? full_name_list[wday] : "?";
} }
inline const char* tm_wday_short_name(int wday) { inline auto tm_wday_short_name(int wday) -> const char* {
static constexpr const char* short_name_list[] = {"Sun", "Mon", "Tue", "Wed", static constexpr const char* short_name_list[] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"}; "Thu", "Fri", "Sat"};
return wday >= 0 && wday <= 6 ? short_name_list[wday] : "???"; return wday >= 0 && wday <= 6 ? short_name_list[wday] : "???";
} }
inline const char* tm_mon_full_name(int mon) { inline auto tm_mon_full_name(int mon) -> const char* {
static constexpr const char* full_name_list[] = { static constexpr const char* full_name_list[] = {
"January", "February", "March", "April", "May", "June", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"}; "July", "August", "September", "October", "November", "December"};
return mon >= 0 && mon <= 11 ? full_name_list[mon] : "?"; return mon >= 0 && mon <= 11 ? full_name_list[mon] : "?";
} }
inline const char* tm_mon_short_name(int mon) { inline auto tm_mon_short_name(int mon) -> const char* {
static constexpr const char* short_name_list[] = { static constexpr const char* short_name_list[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
@ -1047,21 +1052,21 @@ inline void tzset_once() {
// Converts value to Int and checks that it's in the range [0, upper). // Converts value to Int and checks that it's in the range [0, upper).
template <typename T, typename Int, FMT_ENABLE_IF(std::is_integral<T>::value)> template <typename T, typename Int, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline Int to_nonnegative_int(T value, Int upper) { inline auto to_nonnegative_int(T value, Int upper) -> Int {
FMT_ASSERT(std::is_unsigned<Int>::value || if (!std::is_unsigned<Int>::value &&
(value >= 0 && to_unsigned(value) <= to_unsigned(upper)), (value < 0 || to_unsigned(value) > to_unsigned(upper))) {
"invalid value"); FMT_THROW(fmt::format_error("chrono value is out of range"));
(void)upper; }
return static_cast<Int>(value); return static_cast<Int>(value);
} }
template <typename T, typename Int, FMT_ENABLE_IF(!std::is_integral<T>::value)> template <typename T, typename Int, FMT_ENABLE_IF(!std::is_integral<T>::value)>
inline Int to_nonnegative_int(T value, Int upper) { inline auto to_nonnegative_int(T value, Int upper) -> Int {
if (value < 0 || value > static_cast<T>(upper)) if (value < 0 || value > static_cast<T>(upper))
FMT_THROW(format_error("invalid value")); FMT_THROW(format_error("invalid value"));
return static_cast<Int>(value); return static_cast<Int>(value);
} }
constexpr long long pow10(std::uint32_t n) { constexpr auto pow10(std::uint32_t n) -> long long {
return n == 0 ? 1 : 10 * pow10(n - 1); return n == 0 ? 1 : 10 * pow10(n - 1);
} }
@ -1095,13 +1100,12 @@ void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) {
std::chrono::seconds::rep>::type, std::chrono::seconds::rep>::type,
std::ratio<1, detail::pow10(num_fractional_digits)>>; std::ratio<1, detail::pow10(num_fractional_digits)>>;
const auto fractional = const auto fractional = d - fmt_duration_cast<std::chrono::seconds>(d);
d - std::chrono::duration_cast<std::chrono::seconds>(d);
const auto subseconds = const auto subseconds =
std::chrono::treat_as_floating_point< std::chrono::treat_as_floating_point<
typename subsecond_precision::rep>::value typename subsecond_precision::rep>::value
? fractional.count() ? fractional.count()
: std::chrono::duration_cast<subsecond_precision>(fractional).count(); : fmt_duration_cast<subsecond_precision>(fractional).count();
auto n = static_cast<uint32_or_64_or_128_t<long long>>(subseconds); auto n = static_cast<uint32_or_64_or_128_t<long long>>(subseconds);
const int num_digits = detail::count_digits(n); const int num_digits = detail::count_digits(n);
@ -1152,11 +1156,11 @@ void write_floating_seconds(memory_buffer& buf, Duration duration,
num_fractional_digits = 6; num_fractional_digits = 6;
} }
format_to(std::back_inserter(buf), FMT_STRING("{:.{}f}"), fmt::format_to(std::back_inserter(buf), FMT_STRING("{:.{}f}"),
std::fmod(val * static_cast<rep>(Duration::period::num) / std::fmod(val * static_cast<rep>(Duration::period::num) /
static_cast<rep>(Duration::period::den), static_cast<rep>(Duration::period::den),
static_cast<rep>(60)), static_cast<rep>(60)),
num_fractional_digits); num_fractional_digits);
} }
template <typename OutputIt, typename Char, template <typename OutputIt, typename Char,
@ -1217,8 +1221,7 @@ class tm_writer {
return static_cast<int>(l); return static_cast<int>(l);
} }
// Algorithm: // Algorithm: https://en.wikipedia.org/wiki/ISO_week_date.
// https://en.wikipedia.org/wiki/ISO_week_date#Calculating_the_week_number_from_a_month_and_day_of_the_month_or_ordinal_date
auto iso_year_weeks(long long curr_year) const noexcept -> int { auto iso_year_weeks(long long curr_year) const noexcept -> int {
const auto prev_year = curr_year - 1; const auto prev_year = curr_year - 1;
const auto curr_p = const auto curr_p =
@ -1358,7 +1361,7 @@ class tm_writer {
subsecs_(subsecs), subsecs_(subsecs),
tm_(tm) {} tm_(tm) {}
OutputIt out() const { return out_; } auto out() const -> OutputIt { return out_; }
FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) { FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
out_ = copy_str<Char>(begin, end, out_); out_ = copy_str<Char>(begin, end, out_);
@ -1622,6 +1625,7 @@ struct chrono_format_checker : null_chrono_spec_handler<chrono_format_checker> {
template <typename Char> template <typename Char>
FMT_CONSTEXPR void on_text(const Char*, const Char*) {} FMT_CONSTEXPR void on_text(const Char*, const Char*) {}
FMT_CONSTEXPR void on_day_of_year() {}
FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {} FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {}
FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {} FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {}
FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {} FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {}
@ -1640,16 +1644,16 @@ struct chrono_format_checker : null_chrono_spec_handler<chrono_format_checker> {
template <typename T, template <typename T,
FMT_ENABLE_IF(std::is_integral<T>::value&& has_isfinite<T>::value)> FMT_ENABLE_IF(std::is_integral<T>::value&& has_isfinite<T>::value)>
inline bool isfinite(T) { inline auto isfinite(T) -> bool {
return true; return true;
} }
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline T mod(T x, int y) { inline auto mod(T x, int y) -> T {
return x % static_cast<T>(y); return x % static_cast<T>(y);
} }
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)> template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
inline T mod(T x, int y) { inline auto mod(T x, int y) -> T {
return std::fmod(x, static_cast<T>(y)); return std::fmod(x, static_cast<T>(y));
} }
@ -1664,49 +1668,38 @@ template <typename T> struct make_unsigned_or_unchanged<T, true> {
using type = typename std::make_unsigned<T>::type; using type = typename std::make_unsigned<T>::type;
}; };
#if FMT_SAFE_DURATION_CAST
// throwing version of safe_duration_cast
template <typename To, typename FromRep, typename FromPeriod>
To fmt_safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
int ec;
To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
if (ec) FMT_THROW(format_error("cannot format duration"));
return to;
}
#endif
template <typename Rep, typename Period, template <typename Rep, typename Period,
FMT_ENABLE_IF(std::is_integral<Rep>::value)> FMT_ENABLE_IF(std::is_integral<Rep>::value)>
inline std::chrono::duration<Rep, std::milli> get_milliseconds( inline auto get_milliseconds(std::chrono::duration<Rep, Period> d)
std::chrono::duration<Rep, Period> d) { -> std::chrono::duration<Rep, std::milli> {
// this may overflow and/or the result may not fit in the // this may overflow and/or the result may not fit in the
// target type. // target type.
#if FMT_SAFE_DURATION_CAST #if FMT_SAFE_DURATION_CAST
using CommonSecondsType = using CommonSecondsType =
typename std::common_type<decltype(d), std::chrono::seconds>::type; typename std::common_type<decltype(d), std::chrono::seconds>::type;
const auto d_as_common = fmt_safe_duration_cast<CommonSecondsType>(d); const auto d_as_common = fmt_duration_cast<CommonSecondsType>(d);
const auto d_as_whole_seconds = const auto d_as_whole_seconds =
fmt_safe_duration_cast<std::chrono::seconds>(d_as_common); fmt_duration_cast<std::chrono::seconds>(d_as_common);
// this conversion should be nonproblematic // this conversion should be nonproblematic
const auto diff = d_as_common - d_as_whole_seconds; const auto diff = d_as_common - d_as_whole_seconds;
const auto ms = const auto ms =
fmt_safe_duration_cast<std::chrono::duration<Rep, std::milli>>(diff); fmt_duration_cast<std::chrono::duration<Rep, std::milli>>(diff);
return ms; return ms;
#else #else
auto s = std::chrono::duration_cast<std::chrono::seconds>(d); auto s = fmt_duration_cast<std::chrono::seconds>(d);
return std::chrono::duration_cast<std::chrono::milliseconds>(d - s); return fmt_duration_cast<std::chrono::milliseconds>(d - s);
#endif #endif
} }
template <typename Char, typename Rep, typename OutputIt, template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_integral<Rep>::value)> FMT_ENABLE_IF(std::is_integral<Rep>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int) { auto format_duration_value(OutputIt out, Rep val, int) -> OutputIt {
return write<Char>(out, val); return write<Char>(out, val);
} }
template <typename Char, typename Rep, typename OutputIt, template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_floating_point<Rep>::value)> FMT_ENABLE_IF(std::is_floating_point<Rep>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int precision) { auto format_duration_value(OutputIt out, Rep val, int precision) -> OutputIt {
auto specs = format_specs<Char>(); auto specs = format_specs<Char>();
specs.precision = precision; specs.precision = precision;
specs.type = precision >= 0 ? presentation_type::fixed_lower specs.type = precision >= 0 ? presentation_type::fixed_lower
@ -1715,12 +1708,12 @@ OutputIt format_duration_value(OutputIt out, Rep val, int precision) {
} }
template <typename Char, typename OutputIt> template <typename Char, typename OutputIt>
OutputIt copy_unit(string_view unit, OutputIt out, Char) { auto copy_unit(string_view unit, OutputIt out, Char) -> OutputIt {
return std::copy(unit.begin(), unit.end(), out); return std::copy(unit.begin(), unit.end(), out);
} }
template <typename OutputIt> template <typename OutputIt>
OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) { auto copy_unit(string_view unit, OutputIt out, wchar_t) -> OutputIt {
// This works when wchar_t is UTF-32 because units only contain characters // This works when wchar_t is UTF-32 because units only contain characters
// that have the same representation in UTF-16 and UTF-32. // that have the same representation in UTF-16 and UTF-32.
utf8_to_utf16 u(unit); utf8_to_utf16 u(unit);
@ -1728,7 +1721,7 @@ OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) {
} }
template <typename Char, typename Period, typename OutputIt> template <typename Char, typename Period, typename OutputIt>
OutputIt format_duration_unit(OutputIt out) { auto format_duration_unit(OutputIt out) -> OutputIt {
if (const char* unit = get_units<Period>()) if (const char* unit = get_units<Period>())
return copy_unit(string_view(unit), out, Char()); return copy_unit(string_view(unit), out, Char());
*out++ = '['; *out++ = '[';
@ -1795,18 +1788,12 @@ struct chrono_formatter {
// this may overflow and/or the result may not fit in the // this may overflow and/or the result may not fit in the
// target type. // target type.
#if FMT_SAFE_DURATION_CAST
// might need checked conversion (rep!=Rep) // might need checked conversion (rep!=Rep)
auto tmpval = std::chrono::duration<rep, Period>(val); s = fmt_duration_cast<seconds>(std::chrono::duration<rep, Period>(val));
s = fmt_safe_duration_cast<seconds>(tmpval);
#else
s = std::chrono::duration_cast<seconds>(
std::chrono::duration<rep, Period>(val));
#endif
} }
// returns true if nan or inf, writes to out. // returns true if nan or inf, writes to out.
bool handle_nan_inf() { auto handle_nan_inf() -> bool {
if (isfinite(val)) { if (isfinite(val)) {
return false; return false;
} }
@ -1823,17 +1810,22 @@ struct chrono_formatter {
return true; return true;
} }
Rep hour() const { return static_cast<Rep>(mod((s.count() / 3600), 24)); } auto days() const -> Rep { return static_cast<Rep>(s.count() / 86400); }
auto hour() const -> Rep {
return static_cast<Rep>(mod((s.count() / 3600), 24));
}
Rep hour12() const { auto hour12() const -> Rep {
Rep hour = static_cast<Rep>(mod((s.count() / 3600), 12)); Rep hour = static_cast<Rep>(mod((s.count() / 3600), 12));
return hour <= 0 ? 12 : hour; return hour <= 0 ? 12 : hour;
} }
Rep minute() const { return static_cast<Rep>(mod((s.count() / 60), 60)); } auto minute() const -> Rep {
Rep second() const { return static_cast<Rep>(mod(s.count(), 60)); } return static_cast<Rep>(mod((s.count() / 60), 60));
}
auto second() const -> Rep { return static_cast<Rep>(mod(s.count(), 60)); }
std::tm time() const { auto time() const -> std::tm {
auto time = std::tm(); auto time = std::tm();
time.tm_hour = to_nonnegative_int(hour(), 24); time.tm_hour = to_nonnegative_int(hour(), 24);
time.tm_min = to_nonnegative_int(minute(), 60); time.tm_min = to_nonnegative_int(minute(), 60);
@ -1901,10 +1893,14 @@ struct chrono_formatter {
void on_dec0_week_of_year(numeric_system) {} void on_dec0_week_of_year(numeric_system) {}
void on_dec1_week_of_year(numeric_system) {} void on_dec1_week_of_year(numeric_system) {}
void on_iso_week_of_year(numeric_system) {} void on_iso_week_of_year(numeric_system) {}
void on_day_of_year() {}
void on_day_of_month(numeric_system) {} void on_day_of_month(numeric_system) {}
void on_day_of_month_space(numeric_system) {} void on_day_of_month_space(numeric_system) {}
void on_day_of_year() {
if (handle_nan_inf()) return;
write(days(), 0);
}
void on_24_hour(numeric_system ns, pad_type pad) { void on_24_hour(numeric_system ns, pad_type pad) {
if (handle_nan_inf()) return; if (handle_nan_inf()) return;
@ -1997,7 +1993,7 @@ struct chrono_formatter {
} }
}; };
FMT_END_DETAIL_NAMESPACE } // namespace detail
#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907 #if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907
using weekday = std::chrono::weekday; using weekday = std::chrono::weekday;
@ -2011,7 +2007,7 @@ class weekday {
weekday() = default; weekday() = default;
explicit constexpr weekday(unsigned wd) noexcept explicit constexpr weekday(unsigned wd) noexcept
: value(static_cast<unsigned char>(wd != 7 ? wd : 0)) {} : value(static_cast<unsigned char>(wd != 7 ? wd : 0)) {}
constexpr unsigned c_encoding() const noexcept { return value; } constexpr auto c_encoding() const noexcept -> unsigned { return value; }
}; };
class year_month_day {}; class year_month_day {};
@ -2047,80 +2043,67 @@ template <typename Char> struct formatter<weekday, Char> {
template <typename Rep, typename Period, typename Char> template <typename Rep, typename Period, typename Char>
struct formatter<std::chrono::duration<Rep, Period>, Char> { struct formatter<std::chrono::duration<Rep, Period>, Char> {
private: private:
format_specs<Char> specs; format_specs<Char> specs_;
int precision = -1; detail::arg_ref<Char> width_ref_;
using arg_ref_type = detail::arg_ref<Char>; detail::arg_ref<Char> precision_ref_;
arg_ref_type width_ref; bool localized_ = false;
arg_ref_type precision_ref; basic_string_view<Char> format_str_;
bool localized = false;
basic_string_view<Char> format_str;
using duration = std::chrono::duration<Rep, Period>;
using iterator = typename basic_format_parse_context<Char>::iterator;
struct parse_range {
iterator begin;
iterator end;
};
FMT_CONSTEXPR parse_range do_parse(basic_format_parse_context<Char>& ctx) { public:
auto begin = ctx.begin(), end = ctx.end(); FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
if (begin == end || *begin == '}') return {begin, begin}; -> decltype(ctx.begin()) {
auto it = ctx.begin(), end = ctx.end();
if (it == end || *it == '}') return it;
begin = detail::parse_align(begin, end, specs); it = detail::parse_align(it, end, specs_);
if (begin == end) return {begin, begin}; if (it == end) return it;
begin = detail::parse_dynamic_spec(begin, end, specs.width, width_ref, ctx); it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
if (begin == end) return {begin, begin}; if (it == end) return it;
auto checker = detail::chrono_format_checker(); auto checker = detail::chrono_format_checker();
if (*begin == '.') { if (*it == '.') {
checker.has_precision_integral = !std::is_floating_point<Rep>::value; checker.has_precision_integral = !std::is_floating_point<Rep>::value;
begin = it = detail::parse_precision(it, end, specs_.precision, precision_ref_,
detail::parse_precision(begin, end, precision, precision_ref, ctx); ctx);
} }
if (begin != end && *begin == 'L') { if (it != end && *it == 'L') {
++begin; localized_ = true;
localized = true; ++it;
} }
end = detail::parse_chrono_format(begin, end, checker); end = detail::parse_chrono_format(it, end, checker);
return {begin, end}; format_str_ = {it, detail::to_unsigned(end - it)};
} return end;
public:
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
-> decltype(ctx.begin()) {
auto range = do_parse(ctx);
format_str = basic_string_view<Char>(
&*range.begin, detail::to_unsigned(range.end - range.begin));
return range.end;
} }
template <typename FormatContext> template <typename FormatContext>
auto format(const duration& d, FormatContext& ctx) const auto format(std::chrono::duration<Rep, Period> d, FormatContext& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
auto specs_copy = specs; auto specs = specs_;
auto precision_copy = precision; auto precision = specs.precision;
auto begin = format_str.begin(), end = format_str.end(); specs.precision = -1;
auto begin = format_str_.begin(), end = format_str_.end();
// As a possible future optimization, we could avoid extra copying if width // As a possible future optimization, we could avoid extra copying if width
// is not specified. // is not specified.
basic_memory_buffer<Char> buf; auto buf = basic_memory_buffer<Char>();
auto out = std::back_inserter(buf); auto out = std::back_inserter(buf);
detail::handle_dynamic_spec<detail::width_checker>(specs_copy.width, detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
width_ref, ctx); ctx);
detail::handle_dynamic_spec<detail::precision_checker>(precision_copy, detail::handle_dynamic_spec<detail::precision_checker>(precision,
precision_ref, ctx); precision_ref_, ctx);
if (begin == end || *begin == '}') { if (begin == end || *begin == '}') {
out = detail::format_duration_value<Char>(out, d.count(), precision_copy); out = detail::format_duration_value<Char>(out, d.count(), precision);
detail::format_duration_unit<Char, Period>(out); detail::format_duration_unit<Char, Period>(out);
} else { } else {
detail::chrono_formatter<FormatContext, decltype(out), Rep, Period> f( using chrono_formatter =
ctx, out, d); detail::chrono_formatter<FormatContext, decltype(out), Rep, Period>;
f.precision = precision_copy; auto f = chrono_formatter(ctx, out, d);
f.localized = localized; f.precision = precision;
f.localized = localized_;
detail::parse_chrono_format(begin, end, f); detail::parse_chrono_format(begin, end, f);
} }
return detail::write( return detail::write(
ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs_copy); ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
} }
}; };
@ -2128,34 +2111,33 @@ template <typename Char, typename Duration>
struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>, struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
Char> : formatter<std::tm, Char> { Char> : formatter<std::tm, Char> {
FMT_CONSTEXPR formatter() { FMT_CONSTEXPR formatter() {
this->format_str = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>{}; this->format_str_ = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>{};
} }
template <typename FormatContext> template <typename FormatContext>
auto format(std::chrono::time_point<std::chrono::system_clock, Duration> val, auto format(std::chrono::time_point<std::chrono::system_clock, Duration> val,
FormatContext& ctx) const -> decltype(ctx.out()) { FormatContext& ctx) const -> decltype(ctx.out()) {
using period = typename Duration::period; using period = typename Duration::period;
if (period::num != 1 || period::den != 1 || if (detail::const_check(
std::is_floating_point<typename Duration::rep>::value) { period::num != 1 || period::den != 1 ||
std::is_floating_point<typename Duration::rep>::value)) {
const auto epoch = val.time_since_epoch(); const auto epoch = val.time_since_epoch();
auto subsecs = std::chrono::duration_cast<Duration>( auto subsecs = detail::fmt_duration_cast<Duration>(
epoch - std::chrono::duration_cast<std::chrono::seconds>(epoch)); epoch - detail::fmt_duration_cast<std::chrono::seconds>(epoch));
if (subsecs.count() < 0) { if (subsecs.count() < 0) {
auto second = std::chrono::seconds(1); auto second =
detail::fmt_duration_cast<Duration>(std::chrono::seconds(1));
if (epoch.count() < ((Duration::min)() + second).count()) if (epoch.count() < ((Duration::min)() + second).count())
FMT_THROW(format_error("duration is too small")); FMT_THROW(format_error("duration is too small"));
subsecs += second; subsecs += second;
val -= second; val -= second;
} }
return formatter<std::tm, Char>::do_format( return formatter<std::tm, Char>::do_format(gmtime(val), ctx, &subsecs);
gmtime(std::chrono::time_point_cast<std::chrono::seconds>(val)), ctx,
&subsecs);
} }
return formatter<std::tm, Char>::format( return formatter<std::tm, Char>::format(gmtime(val), ctx);
gmtime(std::chrono::time_point_cast<std::chrono::seconds>(val)), ctx);
} }
}; };
@ -2164,7 +2146,7 @@ template <typename Char, typename Duration>
struct formatter<std::chrono::local_time<Duration>, Char> struct formatter<std::chrono::local_time<Duration>, Char>
: formatter<std::tm, Char> { : formatter<std::tm, Char> {
FMT_CONSTEXPR formatter() { FMT_CONSTEXPR formatter() {
this->format_str = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>{}; this->format_str_ = detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>{};
} }
template <typename FormatContext> template <typename FormatContext>
@ -2174,17 +2156,13 @@ struct formatter<std::chrono::local_time<Duration>, Char>
if (period::num != 1 || period::den != 1 || if (period::num != 1 || period::den != 1 ||
std::is_floating_point<typename Duration::rep>::value) { std::is_floating_point<typename Duration::rep>::value) {
const auto epoch = val.time_since_epoch(); const auto epoch = val.time_since_epoch();
const auto subsecs = std::chrono::duration_cast<Duration>( const auto subsecs = detail::fmt_duration_cast<Duration>(
epoch - std::chrono::duration_cast<std::chrono::seconds>(epoch)); epoch - detail::fmt_duration_cast<std::chrono::seconds>(epoch));
return formatter<std::tm, Char>::do_format( return formatter<std::tm, Char>::do_format(localtime(val), ctx, &subsecs);
localtime(std::chrono::time_point_cast<std::chrono::seconds>(val)),
ctx, &subsecs);
} }
return formatter<std::tm, Char>::format( return formatter<std::tm, Char>::format(localtime(val), ctx);
localtime(std::chrono::time_point_cast<std::chrono::seconds>(val)),
ctx);
} }
}; };
#endif #endif
@ -2207,51 +2185,46 @@ struct formatter<std::chrono::time_point<std::chrono::utc_clock, Duration>,
template <typename Char> struct formatter<std::tm, Char> { template <typename Char> struct formatter<std::tm, Char> {
private: private:
format_specs<Char> specs; format_specs<Char> specs_;
detail::arg_ref<Char> width_ref; detail::arg_ref<Char> width_ref_;
protected: protected:
basic_string_view<Char> format_str; basic_string_view<Char> format_str_;
FMT_CONSTEXPR auto do_parse(basic_format_parse_context<Char>& ctx)
-> decltype(ctx.begin()) {
auto begin = ctx.begin(), end = ctx.end();
if (begin == end || *begin == '}') return begin;
begin = detail::parse_align(begin, end, specs);
if (begin == end) return end;
begin = detail::parse_dynamic_spec(begin, end, specs.width, width_ref, ctx);
if (begin == end) return end;
end = detail::parse_chrono_format(begin, end, detail::tm_format_checker());
// Replace default format_str only if the new spec is not empty.
if (end != begin) format_str = {begin, detail::to_unsigned(end - begin)};
return end;
}
template <typename FormatContext, typename Duration> template <typename FormatContext, typename Duration>
auto do_format(const std::tm& tm, FormatContext& ctx, auto do_format(const std::tm& tm, FormatContext& ctx,
const Duration* subsecs) const -> decltype(ctx.out()) { const Duration* subsecs) const -> decltype(ctx.out()) {
auto specs_copy = specs; auto specs = specs_;
basic_memory_buffer<Char> buf; auto buf = basic_memory_buffer<Char>();
auto out = std::back_inserter(buf); auto out = std::back_inserter(buf);
detail::handle_dynamic_spec<detail::width_checker>(specs_copy.width, detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
width_ref, ctx); ctx);
const auto loc_ref = ctx.locale(); auto loc_ref = ctx.locale();
detail::get_locale loc(static_cast<bool>(loc_ref), loc_ref); detail::get_locale loc(static_cast<bool>(loc_ref), loc_ref);
auto w = auto w =
detail::tm_writer<decltype(out), Char, Duration>(loc, out, tm, subsecs); detail::tm_writer<decltype(out), Char, Duration>(loc, out, tm, subsecs);
detail::parse_chrono_format(format_str.begin(), format_str.end(), w); detail::parse_chrono_format(format_str_.begin(), format_str_.end(), w);
return detail::write( return detail::write(
ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs_copy); ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
} }
public: public:
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx) FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
-> decltype(ctx.begin()) { -> decltype(ctx.begin()) {
return this->do_parse(ctx); auto it = ctx.begin(), end = ctx.end();
if (it == end || *it == '}') return it;
it = detail::parse_align(it, end, specs_);
if (it == end) return it;
it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
if (it == end) return it;
end = detail::parse_chrono_format(it, end, detail::tm_format_checker());
// Replace the default format_str only if the new spec is not empty.
if (end != it) format_str_ = {it, detail::to_unsigned(end - it)};
return end;
} }
template <typename FormatContext> template <typename FormatContext>

File diff suppressed because it is too large Load Diff

@ -18,7 +18,7 @@
# include <locale> # include <locale>
#endif #endif
#ifdef _WIN32 #if defined(_WIN32) && !defined(FMT_WINDOWS_NO_WCHAR)
# include <io.h> // _isatty # include <io.h> // _isatty
#endif #endif
@ -58,8 +58,8 @@ FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
error_code_size += detail::to_unsigned(detail::count_digits(abs_value)); error_code_size += detail::to_unsigned(detail::count_digits(abs_value));
auto it = buffer_appender<char>(out); auto it = buffer_appender<char>(out);
if (message.size() <= inline_buffer_size - error_code_size) if (message.size() <= inline_buffer_size - error_code_size)
format_to(it, FMT_STRING("{}{}"), message, SEP); fmt::format_to(it, FMT_STRING("{}{}"), message, SEP);
format_to(it, FMT_STRING("{}{}"), ERROR_STR, error_code); fmt::format_to(it, FMT_STRING("{}{}"), ERROR_STR, error_code);
FMT_ASSERT(out.size() <= inline_buffer_size, ""); FMT_ASSERT(out.size() <= inline_buffer_size, "");
} }
@ -73,9 +73,8 @@ FMT_FUNC void report_error(format_func func, int error_code,
} }
// A wrapper around fwrite that throws on error. // A wrapper around fwrite that throws on error.
inline void fwrite_fully(const void* ptr, size_t size, size_t count, inline void fwrite_fully(const void* ptr, size_t count, FILE* stream) {
FILE* stream) { size_t written = std::fwrite(ptr, 1, count, stream);
size_t written = std::fwrite(ptr, size, count, stream);
if (written < count) if (written < count)
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
} }
@ -86,7 +85,7 @@ locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
static_assert(std::is_same<Locale, std::locale>::value, ""); static_assert(std::is_same<Locale, std::locale>::value, "");
} }
template <typename Locale> Locale locale_ref::get() const { template <typename Locale> auto locale_ref::get() const -> Locale {
static_assert(std::is_same<Locale, std::locale>::value, ""); static_assert(std::is_same<Locale, std::locale>::value, "");
return locale_ ? *static_cast<const std::locale*>(locale_) : std::locale(); return locale_ ? *static_cast<const std::locale*>(locale_) : std::locale();
} }
@ -98,7 +97,8 @@ FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char> {
auto thousands_sep = grouping.empty() ? Char() : facet.thousands_sep(); auto thousands_sep = grouping.empty() ? Char() : facet.thousands_sep();
return {std::move(grouping), thousands_sep}; return {std::move(grouping), thousands_sep};
} }
template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref loc) { template <typename Char>
FMT_FUNC auto decimal_point_impl(locale_ref loc) -> Char {
return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>()) return std::use_facet<std::numpunct<Char>>(loc.get<std::locale>())
.decimal_point(); .decimal_point();
} }
@ -144,24 +144,25 @@ FMT_API FMT_FUNC auto format_facet<std::locale>::do_put(
} }
#endif #endif
FMT_FUNC std::system_error vsystem_error(int error_code, string_view fmt, FMT_FUNC auto vsystem_error(int error_code, string_view fmt, format_args args)
format_args args) { -> std::system_error {
auto ec = std::error_code(error_code, std::generic_category()); auto ec = std::error_code(error_code, std::generic_category());
return std::system_error(ec, vformat(fmt, args)); return std::system_error(ec, vformat(fmt, args));
} }
namespace detail { namespace detail {
template <typename F> inline bool operator==(basic_fp<F> x, basic_fp<F> y) { template <typename F>
inline auto operator==(basic_fp<F> x, basic_fp<F> y) -> bool {
return x.f == y.f && x.e == y.e; return x.f == y.f && x.e == y.e;
} }
// Compilers should be able to optimize this into the ror instruction. // Compilers should be able to optimize this into the ror instruction.
FMT_CONSTEXPR inline uint32_t rotr(uint32_t n, uint32_t r) noexcept { FMT_CONSTEXPR inline auto rotr(uint32_t n, uint32_t r) noexcept -> uint32_t {
r &= 31; r &= 31;
return (n >> r) | (n << (32 - r)); return (n >> r) | (n << (32 - r));
} }
FMT_CONSTEXPR inline uint64_t rotr(uint64_t n, uint32_t r) noexcept { FMT_CONSTEXPR inline auto rotr(uint64_t n, uint32_t r) noexcept -> uint64_t {
r &= 63; r &= 63;
return (n >> r) | (n << (64 - r)); return (n >> r) | (n << (64 - r));
} }
@ -170,14 +171,14 @@ FMT_CONSTEXPR inline uint64_t rotr(uint64_t n, uint32_t r) noexcept {
namespace dragonbox { namespace dragonbox {
// Computes upper 64 bits of multiplication of a 32-bit unsigned integer and a // Computes upper 64 bits of multiplication of a 32-bit unsigned integer and a
// 64-bit unsigned integer. // 64-bit unsigned integer.
inline uint64_t umul96_upper64(uint32_t x, uint64_t y) noexcept { inline auto umul96_upper64(uint32_t x, uint64_t y) noexcept -> uint64_t {
return umul128_upper64(static_cast<uint64_t>(x) << 32, y); return umul128_upper64(static_cast<uint64_t>(x) << 32, y);
} }
// Computes lower 128 bits of multiplication of a 64-bit unsigned integer and a // Computes lower 128 bits of multiplication of a 64-bit unsigned integer and a
// 128-bit unsigned integer. // 128-bit unsigned integer.
inline uint128_fallback umul192_lower128(uint64_t x, inline auto umul192_lower128(uint64_t x, uint128_fallback y) noexcept
uint128_fallback y) noexcept { -> uint128_fallback {
uint64_t high = x * y.high(); uint64_t high = x * y.high();
uint128_fallback high_low = umul128(x, y.low()); uint128_fallback high_low = umul128(x, y.low());
return {high + high_low.high(), high_low.low()}; return {high + high_low.high(), high_low.low()};
@ -185,12 +186,12 @@ inline uint128_fallback umul192_lower128(uint64_t x,
// Computes lower 64 bits of multiplication of a 32-bit unsigned integer and a // Computes lower 64 bits of multiplication of a 32-bit unsigned integer and a
// 64-bit unsigned integer. // 64-bit unsigned integer.
inline uint64_t umul96_lower64(uint32_t x, uint64_t y) noexcept { inline auto umul96_lower64(uint32_t x, uint64_t y) noexcept -> uint64_t {
return x * y; return x * y;
} }
// Various fast log computations. // Various fast log computations.
inline int floor_log10_pow2_minus_log10_4_over_3(int e) noexcept { inline auto floor_log10_pow2_minus_log10_4_over_3(int e) noexcept -> int {
FMT_ASSERT(e <= 2936 && e >= -2985, "too large exponent"); FMT_ASSERT(e <= 2936 && e >= -2985, "too large exponent");
return (e * 631305 - 261663) >> 21; return (e * 631305 - 261663) >> 21;
} }
@ -204,7 +205,7 @@ FMT_INLINE_VARIABLE constexpr struct {
// divisible by pow(10, N). // divisible by pow(10, N).
// Precondition: n <= pow(10, N + 1). // Precondition: n <= pow(10, N + 1).
template <int N> template <int N>
bool check_divisibility_and_divide_by_pow10(uint32_t& n) noexcept { auto check_divisibility_and_divide_by_pow10(uint32_t& n) noexcept -> bool {
// The numbers below are chosen such that: // The numbers below are chosen such that:
// 1. floor(n/d) = floor(nm / 2^k) where d=10 or d=100, // 1. floor(n/d) = floor(nm / 2^k) where d=10 or d=100,
// 2. nm mod 2^k < m if and only if n is divisible by d, // 2. nm mod 2^k < m if and only if n is divisible by d,
@ -229,7 +230,7 @@ bool check_divisibility_and_divide_by_pow10(uint32_t& n) noexcept {
// Computes floor(n / pow(10, N)) for small n and N. // Computes floor(n / pow(10, N)) for small n and N.
// Precondition: n <= pow(10, N + 1). // Precondition: n <= pow(10, N + 1).
template <int N> uint32_t small_division_by_pow10(uint32_t n) noexcept { template <int N> auto small_division_by_pow10(uint32_t n) noexcept -> uint32_t {
constexpr auto info = div_small_pow10_infos[N - 1]; constexpr auto info = div_small_pow10_infos[N - 1];
FMT_ASSERT(n <= info.divisor * 10, "n is too large"); FMT_ASSERT(n <= info.divisor * 10, "n is too large");
constexpr uint32_t magic_number = constexpr uint32_t magic_number =
@ -238,12 +239,12 @@ template <int N> uint32_t small_division_by_pow10(uint32_t n) noexcept {
} }
// Computes floor(n / 10^(kappa + 1)) (float) // Computes floor(n / 10^(kappa + 1)) (float)
inline uint32_t divide_by_10_to_kappa_plus_1(uint32_t n) noexcept { inline auto divide_by_10_to_kappa_plus_1(uint32_t n) noexcept -> uint32_t {
// 1374389535 = ceil(2^37/100) // 1374389535 = ceil(2^37/100)
return static_cast<uint32_t>((static_cast<uint64_t>(n) * 1374389535) >> 37); return static_cast<uint32_t>((static_cast<uint64_t>(n) * 1374389535) >> 37);
} }
// Computes floor(n / 10^(kappa + 1)) (double) // Computes floor(n / 10^(kappa + 1)) (double)
inline uint64_t divide_by_10_to_kappa_plus_1(uint64_t n) noexcept { inline auto divide_by_10_to_kappa_plus_1(uint64_t n) noexcept -> uint64_t {
// 2361183241434822607 = ceil(2^(64+7)/1000) // 2361183241434822607 = ceil(2^(64+7)/1000)
return umul128_upper64(n, 2361183241434822607ull) >> 7; return umul128_upper64(n, 2361183241434822607ull) >> 7;
} }
@ -255,7 +256,7 @@ template <> struct cache_accessor<float> {
using carrier_uint = float_info<float>::carrier_uint; using carrier_uint = float_info<float>::carrier_uint;
using cache_entry_type = uint64_t; using cache_entry_type = uint64_t;
static uint64_t get_cached_power(int k) noexcept { static auto get_cached_power(int k) noexcept -> uint64_t {
FMT_ASSERT(k >= float_info<float>::min_k && k <= float_info<float>::max_k, FMT_ASSERT(k >= float_info<float>::min_k && k <= float_info<float>::max_k,
"k is out of range"); "k is out of range");
static constexpr const uint64_t pow10_significands[] = { static constexpr const uint64_t pow10_significands[] = {
@ -297,20 +298,23 @@ template <> struct cache_accessor<float> {
bool is_integer; bool is_integer;
}; };
static compute_mul_result compute_mul( static auto compute_mul(carrier_uint u,
carrier_uint u, const cache_entry_type& cache) noexcept { const cache_entry_type& cache) noexcept
-> compute_mul_result {
auto r = umul96_upper64(u, cache); auto r = umul96_upper64(u, cache);
return {static_cast<carrier_uint>(r >> 32), return {static_cast<carrier_uint>(r >> 32),
static_cast<carrier_uint>(r) == 0}; static_cast<carrier_uint>(r) == 0};
} }
static uint32_t compute_delta(const cache_entry_type& cache, static auto compute_delta(const cache_entry_type& cache, int beta) noexcept
int beta) noexcept { -> uint32_t {
return static_cast<uint32_t>(cache >> (64 - 1 - beta)); return static_cast<uint32_t>(cache >> (64 - 1 - beta));
} }
static compute_mul_parity_result compute_mul_parity( static auto compute_mul_parity(carrier_uint two_f,
carrier_uint two_f, const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache,
int beta) noexcept
-> compute_mul_parity_result {
FMT_ASSERT(beta >= 1, ""); FMT_ASSERT(beta >= 1, "");
FMT_ASSERT(beta < 64, ""); FMT_ASSERT(beta < 64, "");
@ -319,22 +323,22 @@ template <> struct cache_accessor<float> {
static_cast<uint32_t>(r >> (32 - beta)) == 0}; static_cast<uint32_t>(r >> (32 - beta)) == 0};
} }
static carrier_uint compute_left_endpoint_for_shorter_interval_case( static auto compute_left_endpoint_for_shorter_interval_case(
const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache, int beta) noexcept -> carrier_uint {
return static_cast<carrier_uint>( return static_cast<carrier_uint>(
(cache - (cache >> (num_significand_bits<float>() + 2))) >> (cache - (cache >> (num_significand_bits<float>() + 2))) >>
(64 - num_significand_bits<float>() - 1 - beta)); (64 - num_significand_bits<float>() - 1 - beta));
} }
static carrier_uint compute_right_endpoint_for_shorter_interval_case( static auto compute_right_endpoint_for_shorter_interval_case(
const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache, int beta) noexcept -> carrier_uint {
return static_cast<carrier_uint>( return static_cast<carrier_uint>(
(cache + (cache >> (num_significand_bits<float>() + 1))) >> (cache + (cache >> (num_significand_bits<float>() + 1))) >>
(64 - num_significand_bits<float>() - 1 - beta)); (64 - num_significand_bits<float>() - 1 - beta));
} }
static carrier_uint compute_round_up_for_shorter_interval_case( static auto compute_round_up_for_shorter_interval_case(
const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache, int beta) noexcept -> carrier_uint {
return (static_cast<carrier_uint>( return (static_cast<carrier_uint>(
cache >> (64 - num_significand_bits<float>() - 2 - beta)) + cache >> (64 - num_significand_bits<float>() - 2 - beta)) +
1) / 1) /
@ -346,7 +350,7 @@ template <> struct cache_accessor<double> {
using carrier_uint = float_info<double>::carrier_uint; using carrier_uint = float_info<double>::carrier_uint;
using cache_entry_type = uint128_fallback; using cache_entry_type = uint128_fallback;
static uint128_fallback get_cached_power(int k) noexcept { static auto get_cached_power(int k) noexcept -> uint128_fallback {
FMT_ASSERT(k >= float_info<double>::min_k && k <= float_info<double>::max_k, FMT_ASSERT(k >= float_info<double>::min_k && k <= float_info<double>::max_k,
"k is out of range"); "k is out of range");
@ -985,8 +989,7 @@ template <> struct cache_accessor<double> {
{0xe0accfa875af45a7, 0x93eb1b80a33b8606}, {0xe0accfa875af45a7, 0x93eb1b80a33b8606},
{0x8c6c01c9498d8b88, 0xbc72f130660533c4}, {0x8c6c01c9498d8b88, 0xbc72f130660533c4},
{0xaf87023b9bf0ee6a, 0xeb8fad7c7f8680b5}, {0xaf87023b9bf0ee6a, 0xeb8fad7c7f8680b5},
{ 0xdb68c2ca82ed2a05, {0xdb68c2ca82ed2a05, 0xa67398db9f6820e2},
0xa67398db9f6820e2 }
#else #else
{0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b}, {0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b},
{0xce5d73ff402d98e3, 0xfb0a3d212dc81290}, {0xce5d73ff402d98e3, 0xfb0a3d212dc81290},
@ -1071,19 +1074,22 @@ template <> struct cache_accessor<double> {
bool is_integer; bool is_integer;
}; };
static compute_mul_result compute_mul( static auto compute_mul(carrier_uint u,
carrier_uint u, const cache_entry_type& cache) noexcept { const cache_entry_type& cache) noexcept
-> compute_mul_result {
auto r = umul192_upper128(u, cache); auto r = umul192_upper128(u, cache);
return {r.high(), r.low() == 0}; return {r.high(), r.low() == 0};
} }
static uint32_t compute_delta(cache_entry_type const& cache, static auto compute_delta(cache_entry_type const& cache, int beta) noexcept
int beta) noexcept { -> uint32_t {
return static_cast<uint32_t>(cache.high() >> (64 - 1 - beta)); return static_cast<uint32_t>(cache.high() >> (64 - 1 - beta));
} }
static compute_mul_parity_result compute_mul_parity( static auto compute_mul_parity(carrier_uint two_f,
carrier_uint two_f, const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache,
int beta) noexcept
-> compute_mul_parity_result {
FMT_ASSERT(beta >= 1, ""); FMT_ASSERT(beta >= 1, "");
FMT_ASSERT(beta < 64, ""); FMT_ASSERT(beta < 64, "");
@ -1092,35 +1098,35 @@ template <> struct cache_accessor<double> {
((r.high() << beta) | (r.low() >> (64 - beta))) == 0}; ((r.high() << beta) | (r.low() >> (64 - beta))) == 0};
} }
static carrier_uint compute_left_endpoint_for_shorter_interval_case( static auto compute_left_endpoint_for_shorter_interval_case(
const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache, int beta) noexcept -> carrier_uint {
return (cache.high() - return (cache.high() -
(cache.high() >> (num_significand_bits<double>() + 2))) >> (cache.high() >> (num_significand_bits<double>() + 2))) >>
(64 - num_significand_bits<double>() - 1 - beta); (64 - num_significand_bits<double>() - 1 - beta);
} }
static carrier_uint compute_right_endpoint_for_shorter_interval_case( static auto compute_right_endpoint_for_shorter_interval_case(
const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache, int beta) noexcept -> carrier_uint {
return (cache.high() + return (cache.high() +
(cache.high() >> (num_significand_bits<double>() + 1))) >> (cache.high() >> (num_significand_bits<double>() + 1))) >>
(64 - num_significand_bits<double>() - 1 - beta); (64 - num_significand_bits<double>() - 1 - beta);
} }
static carrier_uint compute_round_up_for_shorter_interval_case( static auto compute_round_up_for_shorter_interval_case(
const cache_entry_type& cache, int beta) noexcept { const cache_entry_type& cache, int beta) noexcept -> carrier_uint {
return ((cache.high() >> (64 - num_significand_bits<double>() - 2 - beta)) + return ((cache.high() >> (64 - num_significand_bits<double>() - 2 - beta)) +
1) / 1) /
2; 2;
} }
}; };
FMT_FUNC uint128_fallback get_cached_power(int k) noexcept { FMT_FUNC auto get_cached_power(int k) noexcept -> uint128_fallback {
return cache_accessor<double>::get_cached_power(k); return cache_accessor<double>::get_cached_power(k);
} }
// Various integer checks // Various integer checks
template <typename T> template <typename T>
bool is_left_endpoint_integer_shorter_interval(int exponent) noexcept { auto is_left_endpoint_integer_shorter_interval(int exponent) noexcept -> bool {
const int case_shorter_interval_left_endpoint_lower_threshold = 2; const int case_shorter_interval_left_endpoint_lower_threshold = 2;
const int case_shorter_interval_left_endpoint_upper_threshold = 3; const int case_shorter_interval_left_endpoint_upper_threshold = 3;
return exponent >= case_shorter_interval_left_endpoint_lower_threshold && return exponent >= case_shorter_interval_left_endpoint_lower_threshold &&
@ -1128,16 +1134,12 @@ bool is_left_endpoint_integer_shorter_interval(int exponent) noexcept {
} }
// Remove trailing zeros from n and return the number of zeros removed (float) // Remove trailing zeros from n and return the number of zeros removed (float)
FMT_INLINE int remove_trailing_zeros(uint32_t& n) noexcept { FMT_INLINE int remove_trailing_zeros(uint32_t& n, int s = 0) noexcept {
FMT_ASSERT(n != 0, ""); FMT_ASSERT(n != 0, "");
// Modular inverse of 5 (mod 2^32): (mod_inv_5 * 5) mod 2^32 = 1. // Modular inverse of 5 (mod 2^32): (mod_inv_5 * 5) mod 2^32 = 1.
// See https://github.com/fmtlib/fmt/issues/3163 for more details. constexpr uint32_t mod_inv_5 = 0xcccccccd;
const uint32_t mod_inv_5 = 0xcccccccd; constexpr uint32_t mod_inv_25 = 0xc28f5c29; // = mod_inv_5 * mod_inv_5
// Casts are needed to workaround a bug in MSVC 19.22 and older.
const uint32_t mod_inv_25 =
static_cast<uint32_t>(uint64_t(mod_inv_5) * mod_inv_5);
int s = 0;
while (true) { while (true) {
auto q = rotr(n * mod_inv_25, 2); auto q = rotr(n * mod_inv_25, 2);
if (q > max_value<uint32_t>() / 100) break; if (q > max_value<uint32_t>() / 100) break;
@ -1162,32 +1164,17 @@ FMT_INLINE int remove_trailing_zeros(uint64_t& n) noexcept {
// Is n is divisible by 10^8? // Is n is divisible by 10^8?
if ((nm.high() & ((1ull << (90 - 64)) - 1)) == 0 && nm.low() < magic_number) { if ((nm.high() & ((1ull << (90 - 64)) - 1)) == 0 && nm.low() < magic_number) {
// If yes, work with the quotient. // If yes, work with the quotient...
auto n32 = static_cast<uint32_t>(nm.high() >> (90 - 64)); auto n32 = static_cast<uint32_t>(nm.high() >> (90 - 64));
// ... and use the 32 bit variant of the function
const uint32_t mod_inv_5 = 0xcccccccd; int s = remove_trailing_zeros(n32, 8);
const uint32_t mod_inv_25 = mod_inv_5 * mod_inv_5;
int s = 8;
while (true) {
auto q = rotr(n32 * mod_inv_25, 2);
if (q > max_value<uint32_t>() / 100) break;
n32 = q;
s += 2;
}
auto q = rotr(n32 * mod_inv_5, 1);
if (q <= max_value<uint32_t>() / 10) {
n32 = q;
s |= 1;
}
n = n32; n = n32;
return s; return s;
} }
// If n is not divisible by 10^8, work with n itself. // If n is not divisible by 10^8, work with n itself.
const uint64_t mod_inv_5 = 0xcccccccccccccccd; constexpr uint64_t mod_inv_5 = 0xcccccccccccccccd;
const uint64_t mod_inv_25 = mod_inv_5 * mod_inv_5; constexpr uint64_t mod_inv_25 = 0x8f5c28f5c28f5c29; // mod_inv_5 * mod_inv_5
int s = 0; int s = 0;
while (true) { while (true) {
@ -1253,7 +1240,7 @@ FMT_INLINE decimal_fp<T> shorter_interval_case(int exponent) noexcept {
return ret_value; return ret_value;
} }
template <typename T> decimal_fp<T> to_decimal(T x) noexcept { template <typename T> auto to_decimal(T x) noexcept -> decimal_fp<T> {
// Step 1: integer promotion & Schubfach multiplier calculation. // Step 1: integer promotion & Schubfach multiplier calculation.
using carrier_uint = typename float_info<T>::carrier_uint; using carrier_uint = typename float_info<T>::carrier_uint;
@ -1392,15 +1379,15 @@ template <> struct formatter<detail::bigint> {
for (auto i = n.bigits_.size(); i > 0; --i) { for (auto i = n.bigits_.size(); i > 0; --i) {
auto value = n.bigits_[i - 1u]; auto value = n.bigits_[i - 1u];
if (first) { if (first) {
out = format_to(out, FMT_STRING("{:x}"), value); out = fmt::format_to(out, FMT_STRING("{:x}"), value);
first = false; first = false;
continue; continue;
} }
out = format_to(out, FMT_STRING("{:08x}"), value); out = fmt::format_to(out, FMT_STRING("{:08x}"), value);
} }
if (n.exp_ > 0) if (n.exp_ > 0)
out = format_to(out, FMT_STRING("p{}"), out = fmt::format_to(out, FMT_STRING("p{}"),
n.exp_ * detail::bigint::bigit_bits); n.exp_ * detail::bigint::bigit_bits);
return out; return out;
} }
}; };
@ -1436,7 +1423,7 @@ FMT_FUNC void report_system_error(int error_code,
report_error(format_system_error, error_code, message); report_error(format_system_error, error_code, message);
} }
FMT_FUNC std::string vformat(string_view fmt, format_args args) { FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string {
// Don't optimize the "{}" case to keep the binary size small and because it // Don't optimize the "{}" case to keep the binary size small and because it
// can be better optimized in fmt::format anyway. // can be better optimized in fmt::format anyway.
auto buffer = memory_buffer(); auto buffer = memory_buffer();
@ -1445,33 +1432,38 @@ FMT_FUNC std::string vformat(string_view fmt, format_args args) {
} }
namespace detail { namespace detail {
#ifndef _WIN32 #if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
FMT_FUNC bool write_console(std::FILE*, string_view) { return false; } FMT_FUNC auto write_console(int, string_view) -> bool { return false; }
#else #else
using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>; using dword = conditional_t<sizeof(long) == 4, unsigned long, unsigned>;
extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( //
void*, const void*, dword, dword*, void*); void*, const void*, dword, dword*, void*);
FMT_FUNC bool write_console(std::FILE* f, string_view text) { FMT_FUNC bool write_console(int fd, string_view text) {
auto fd = _fileno(f);
if (!_isatty(fd)) return false;
auto u16 = utf8_to_utf16(text); auto u16 = utf8_to_utf16(text);
auto written = dword();
return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(), return WriteConsoleW(reinterpret_cast<void*>(_get_osfhandle(fd)), u16.c_str(),
static_cast<uint32_t>(u16.size()), &written, nullptr); static_cast<dword>(u16.size()), nullptr, nullptr) != 0;
} }
#endif
#ifdef _WIN32
// Print assuming legacy (non-Unicode) encoding. // Print assuming legacy (non-Unicode) encoding.
FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) { FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args) {
auto buffer = memory_buffer(); auto buffer = memory_buffer();
detail::vformat_to(buffer, fmt, detail::vformat_to(buffer, fmt, args);
basic_format_args<buffer_context<char>>(args)); fwrite_fully(buffer.data(), buffer.size(), f);
fwrite_fully(buffer.data(), 1, buffer.size(), f);
} }
#endif #endif
FMT_FUNC void print(std::FILE* f, string_view text) { FMT_FUNC void print(std::FILE* f, string_view text) {
if (!write_console(f, text)) fwrite_fully(text.data(), 1, text.size(), f); #ifdef _WIN32
int fd = _fileno(f);
if (_isatty(fd)) {
std::fflush(f);
if (write_console(fd, text)) return;
}
#endif
fwrite_fully(text.data(), text.size(), f);
} }
} // namespace detail } // namespace detail

File diff suppressed because it is too large Load Diff

@ -10,19 +10,50 @@
#include <fstream> // std::filebuf #include <fstream> // std::filebuf
#if defined(_WIN32) && defined(__GLIBCXX__) #ifdef _WIN32
# include <ext/stdio_filebuf.h> # ifdef __GLIBCXX__
# include <ext/stdio_sync_filebuf.h> # include <ext/stdio_filebuf.h>
#elif defined(_WIN32) && defined(_LIBCPP_VERSION) # include <ext/stdio_sync_filebuf.h>
# include <__std_stream> # endif
# include <io.h>
#endif #endif
#include "format.h" #include "format.h"
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
template <typename Streambuf> class formatbuf : public Streambuf {
private:
using char_type = typename Streambuf::char_type;
using streamsize = decltype(std::declval<Streambuf>().sputn(nullptr, 0));
using int_type = typename Streambuf::int_type;
using traits_type = typename Streambuf::traits_type;
buffer<char_type>& buffer_;
public:
explicit formatbuf(buffer<char_type>& buf) : buffer_(buf) {}
protected:
// The put area is always empty. This makes the implementation simpler and has
// the advantage that the streambuf and the buffer are always in sync and
// sputc never writes into uninitialized memory. A disadvantage is that each
// call to sputc always results in a (virtual) call to overflow. There is no
// disadvantage here for sputn since this always results in a call to xsputn.
auto overflow(int_type ch) -> int_type override {
if (!traits_type::eq_int_type(ch, traits_type::eof()))
buffer_.push_back(static_cast<char_type>(ch));
return ch;
}
auto xsputn(const char_type* s, streamsize count) -> streamsize override {
buffer_.append(s, s + count);
return count;
}
};
// Generate a unique explicit instantion in every translation unit using a tag // Generate a unique explicit instantion in every translation unit using a tag
// type in an anonymous namespace. // type in an anonymous namespace.
namespace { namespace {
@ -37,36 +68,40 @@ class file_access {
template class file_access<file_access_tag, std::filebuf, template class file_access<file_access_tag, std::filebuf,
&std::filebuf::_Myfile>; &std::filebuf::_Myfile>;
auto get_file(std::filebuf&) -> FILE*; auto get_file(std::filebuf&) -> FILE*;
#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
template class file_access<file_access_tag, std::__stdoutbuf<char>,
&std::__stdoutbuf<char>::__file_>;
auto get_file(std::__stdoutbuf<char>&) -> FILE*;
#endif #endif
inline bool write_ostream_unicode(std::ostream& os, fmt::string_view data) { inline auto write_ostream_unicode(std::ostream& os, fmt::string_view data)
-> bool {
FILE* f = nullptr;
#if FMT_MSC_VERSION #if FMT_MSC_VERSION
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf())) if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
if (FILE* f = get_file(*buf)) return write_console(f, data); f = get_file(*buf);
else
return false;
#elif defined(_WIN32) && defined(__GLIBCXX__) #elif defined(_WIN32) && defined(__GLIBCXX__)
auto* rdbuf = os.rdbuf(); auto* rdbuf = os.rdbuf();
FILE* c_file;
if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf)) if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
c_file = sfbuf->file(); f = sfbuf->file();
else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf)) else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
c_file = fbuf->file(); f = fbuf->file();
else else
return false; return false;
if (c_file) return write_console(c_file, data);
#elif defined(_WIN32) && defined(_LIBCPP_VERSION)
if (auto* buf = dynamic_cast<std::__stdoutbuf<char>*>(os.rdbuf()))
if (FILE* f = get_file(*buf)) return write_console(f, data);
#else #else
ignore_unused(os, data); ignore_unused(os, data, f);
#endif
#ifdef _WIN32
if (f) {
int fd = _fileno(f);
if (_isatty(fd)) {
os.flush();
return write_console(fd, data);
}
}
#endif #endif
return false; return false;
} }
inline bool write_ostream_unicode(std::wostream&, inline auto write_ostream_unicode(std::wostream&,
fmt::basic_string_view<wchar_t>) { fmt::basic_string_view<wchar_t>) -> bool {
return false; return false;
} }
@ -87,18 +122,19 @@ void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
} }
template <typename Char, typename T> template <typename Char, typename T>
void format_value(buffer<Char>& buf, const T& value, void format_value(buffer<Char>& buf, const T& value) {
locale_ref loc = locale_ref()) {
auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf); auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
auto&& output = std::basic_ostream<Char>(&format_buf); auto&& output = std::basic_ostream<Char>(&format_buf);
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
if (loc) output.imbue(loc.get<std::locale>()); output.imbue(std::locale::classic()); // The default is always unlocalized.
#endif #endif
output << value; output << value;
output.exceptions(std::ios_base::failbit | std::ios_base::badbit); output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
} }
template <typename T> struct streamed_view { const T& value; }; template <typename T> struct streamed_view {
const T& value;
};
} // namespace detail } // namespace detail
@ -111,7 +147,7 @@ struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const auto format(const T& value, basic_format_context<OutputIt, Char>& ctx) const
-> OutputIt { -> OutputIt {
auto buffer = basic_memory_buffer<Char>(); auto buffer = basic_memory_buffer<Char>();
detail::format_value(buffer, value, ctx.locale()); detail::format_value(buffer, value);
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);
} }
@ -140,7 +176,7 @@ struct formatter<detail::streamed_view<T>, Char>
\endrst \endrst
*/ */
template <typename T> template <typename T>
auto streamed(const T& value) -> detail::streamed_view<T> { constexpr auto streamed(const T& value) -> detail::streamed_view<T> {
return {value}; return {value};
} }
@ -155,7 +191,7 @@ inline void vprint_directly(std::ostream& os, string_view format_str,
} // namespace detail } // namespace detail
FMT_MODULE_EXPORT template <typename Char> FMT_EXPORT template <typename Char>
void vprint(std::basic_ostream<Char>& os, void vprint(std::basic_ostream<Char>& os,
basic_string_view<type_identity_t<Char>> format_str, basic_string_view<type_identity_t<Char>> format_str,
basic_format_args<buffer_context<type_identity_t<Char>>> args) { basic_format_args<buffer_context<type_identity_t<Char>>> args) {
@ -174,7 +210,7 @@ void vprint(std::basic_ostream<Char>& os,
fmt::print(cerr, "Don't {}!", "panic"); fmt::print(cerr, "Don't {}!", "panic");
\endrst \endrst
*/ */
FMT_MODULE_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) {
const auto& vargs = fmt::make_format_args(args...); const auto& vargs = fmt::make_format_args(args...);
if (detail::is_utf8()) if (detail::is_utf8())
@ -183,7 +219,7 @@ void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
detail::vprint_directly(os, fmt, vargs); detail::vprint_directly(os, fmt, vargs);
} }
FMT_MODULE_EXPORT FMT_EXPORT
template <typename... Args> template <typename... Args>
void print(std::wostream& os, void print(std::wostream& os,
basic_format_string<wchar_t, type_identity_t<Args>...> fmt, basic_format_string<wchar_t, type_identity_t<Args>...> fmt,
@ -191,12 +227,12 @@ void print(std::wostream& os,
vprint(os, fmt, fmt::make_format_args<buffer_context<wchar_t>>(args...)); vprint(os, fmt, fmt::make_format_args<buffer_context<wchar_t>>(args...));
} }
FMT_MODULE_EXPORT template <typename... T> FMT_EXPORT template <typename... T>
void println(std::ostream& os, format_string<T...> fmt, T&&... args) { void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...)); fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
} }
FMT_MODULE_EXPORT FMT_EXPORT
template <typename... Args> template <typename... Args>
void println(std::wostream& os, void println(std::wostream& os,
basic_format_string<wchar_t, type_identity_t<Args>...> fmt, basic_format_string<wchar_t, type_identity_t<Args>...> fmt,

@ -1,13 +1,9 @@
// Formatting library for C++ - experimental range support // Formatting library for C++ - range and tuple support
// //
// Copyright (c) 2012 - present, Victor Zverovich // Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors
// All rights reserved. // All rights reserved.
// //
// For the license information refer to format.h. // For the license information refer to format.h.
//
// Copyright (c) 2018 - present, Remotion (Igor Schulz)
// All Rights Reserved
// {fmt} support for ranges, containers and types tuple interface.
#ifndef FMT_RANGES_H_ #ifndef FMT_RANGES_H_
#define FMT_RANGES_H_ #define FMT_RANGES_H_
@ -187,7 +183,7 @@ template <size_t N> using make_index_sequence = std::make_index_sequence<N>;
template <typename T, T... N> struct integer_sequence { template <typename T, T... N> struct integer_sequence {
using value_type = T; using value_type = T;
static FMT_CONSTEXPR size_t size() { return sizeof...(N); } static FMT_CONSTEXPR auto size() -> size_t { return sizeof...(N); }
}; };
template <size_t... N> using index_sequence = integer_sequence<size_t, N...>; template <size_t... N> using index_sequence = integer_sequence<size_t, N...>;
@ -211,15 +207,15 @@ class is_tuple_formattable_ {
}; };
template <typename T, typename C> class is_tuple_formattable_<T, C, true> { template <typename T, typename C> class is_tuple_formattable_<T, C, true> {
template <std::size_t... Is> template <std::size_t... Is>
static std::true_type check2(index_sequence<Is...>, static auto check2(index_sequence<Is...>,
integer_sequence<bool, (Is == Is)...>); integer_sequence<bool, (Is == Is)...>) -> std::true_type;
static std::false_type check2(...); static auto check2(...) -> std::false_type;
template <std::size_t... Is> template <std::size_t... Is>
static decltype(check2( static auto check(index_sequence<Is...>) -> decltype(check2(
index_sequence<Is...>{}, index_sequence<Is...>{},
integer_sequence< integer_sequence<bool,
bool, (is_formattable<typename std::tuple_element<Is, T>::type, (is_formattable<typename std::tuple_element<Is, T>::type,
C>::value)...>{})) check(index_sequence<Is...>); C>::value)...>{}));
public: public:
static constexpr const bool value = static constexpr const bool value =
@ -421,6 +417,12 @@ struct is_formattable_delayed
#endif #endif
} // namespace detail } // namespace detail
template <typename...> struct conjunction : std::true_type {};
template <typename P> struct conjunction<P> : P {};
template <typename P1, typename... Pn>
struct conjunction<P1, Pn...>
: conditional_t<bool(P1::value), conjunction<Pn...>, P1> {};
template <typename T, typename Char, typename Enable = void> template <typename T, typename Char, typename Enable = void>
struct range_formatter; struct range_formatter;
@ -486,7 +488,8 @@ struct range_formatter<
for (; it != end; ++it) { for (; it != end; ++it) {
if (i > 0) out = detail::copy_str<Char>(separator_, out); if (i > 0) out = detail::copy_str<Char>(separator_, out);
ctx.advance_to(out); ctx.advance_to(out);
out = underlying_.format(mapper.map(*it), ctx); auto&& item = *it;
out = underlying_.format(mapper.map(item), ctx);
++i; ++i;
} }
out = detail::copy_str<Char>(closing_bracket_, out); out = detail::copy_str<Char>(closing_bracket_, out);
@ -668,8 +671,11 @@ template <typename Container> struct all {
} // namespace detail } // namespace detail
template <typename T, typename Char> template <typename T, typename Char>
struct formatter<T, Char, struct formatter<
enable_if_t<detail::is_container_adaptor_like<T>::value>> T, Char,
enable_if_t<conjunction<detail::is_container_adaptor_like<T>,
bool_constant<range_format_kind<T, Char>::value ==
range_format::disabled>>::value>>
: formatter<detail::all<typename T::container_type>, Char> { : formatter<detail::all<typename T::container_type>, Char> {
using all = detail::all<typename T::container_type>; using all = detail::all<typename T::container_type>;
template <typename FormatContext> template <typename FormatContext>

@ -8,6 +8,8 @@
#ifndef FMT_STD_H_ #ifndef FMT_STD_H_
#define FMT_STD_H_ #define FMT_STD_H_
#include <atomic>
#include <bitset>
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
#include <memory> #include <memory>
@ -15,7 +17,9 @@
#include <type_traits> #include <type_traits>
#include <typeinfo> #include <typeinfo>
#include <utility> #include <utility>
#include <vector>
#include "format.h"
#include "ostream.h" #include "ostream.h"
#if FMT_HAS_INCLUDE(<version>) #if FMT_HAS_INCLUDE(<version>)
@ -34,6 +38,10 @@
# endif # endif
#endif #endif
#if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE(<source_location>)
# include <source_location>
#endif
// GCC 4 does not support FMT_HAS_INCLUDE. // GCC 4 does not support FMT_HAS_INCLUDE.
#if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__) #if FMT_HAS_INCLUDE(<cxxabi.h>) || defined(__GLIBCXX__)
# include <cxxabi.h> # include <cxxabi.h>
@ -44,67 +52,155 @@
# endif # endif
#endif #endif
#ifdef __cpp_lib_filesystem // Check if typeid is available.
#ifndef FMT_USE_TYPEID
// __RTTI is for EDG compilers. In MSVC typeid is available without RTTI.
# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || FMT_MSC_VERSION || \
defined(__INTEL_RTTI__) || defined(__RTTI)
# define FMT_USE_TYPEID 1
# else
# define FMT_USE_TYPEID 0
# endif
#endif
// For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined.
#ifndef FMT_CPP_LIB_FILESYSTEM
# ifdef __cpp_lib_filesystem
# define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem
# else
# define FMT_CPP_LIB_FILESYSTEM 0
# endif
#endif
#ifndef FMT_CPP_LIB_VARIANT
# ifdef __cpp_lib_variant
# define FMT_CPP_LIB_VARIANT __cpp_lib_variant
# else
# define FMT_CPP_LIB_VARIANT 0
# endif
#endif
#if FMT_CPP_LIB_FILESYSTEM
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
template <typename Char> template <typename Char, typename PathChar>
void write_escaped_path(basic_memory_buffer<Char>& quoted, auto get_path_string(const std::filesystem::path& p,
const std::filesystem::path& p) { const std::basic_string<PathChar>& native) {
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>()); if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>)
} return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
# ifdef _WIN32 else
template <> return p.string<Char>();
inline void write_escaped_path<char>(memory_buffer& quoted,
const std::filesystem::path& p) {
auto buf = basic_memory_buffer<wchar_t>();
write_escaped_string<wchar_t>(std::back_inserter(buf), p.native());
// Convert UTF-16 to UTF-8.
if (!unicode_to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}))
FMT_THROW(std::runtime_error("invalid utf16"));
} }
# endif
template <> template <typename Char, typename PathChar>
inline void write_escaped_path<std::filesystem::path::value_type>( void write_escaped_path(basic_memory_buffer<Char>& quoted,
basic_memory_buffer<std::filesystem::path::value_type>& quoted, const std::filesystem::path& p,
const std::filesystem::path& p) { const std::basic_string<PathChar>& native) {
write_escaped_string<std::filesystem::path::value_type>( if constexpr (std::is_same_v<Char, char> &&
std::back_inserter(quoted), p.native()); std::is_same_v<PathChar, wchar_t>) {
auto buf = basic_memory_buffer<wchar_t>();
write_escaped_string<wchar_t>(std::back_inserter(buf), native);
bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
FMT_ASSERT(valid, "invalid utf16");
} else if constexpr (std::is_same_v<Char, PathChar>) {
write_escaped_string<std::filesystem::path::value_type>(
std::back_inserter(quoted), native);
} else {
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
}
} }
} // namespace detail } // namespace detail
FMT_MODULE_EXPORT FMT_EXPORT
template <typename Char> template <typename Char> struct formatter<std::filesystem::path, Char> {
struct formatter<std::filesystem::path, Char> private:
: formatter<basic_string_view<Char>> { format_specs<Char> specs_;
detail::arg_ref<Char> width_ref_;
bool debug_ = false;
char path_type_ = 0;
public:
FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; }
template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) { template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
auto out = formatter<basic_string_view<Char>>::parse(ctx); auto it = ctx.begin(), end = ctx.end();
this->set_debug_format(false); if (it == end) return it;
return out;
it = detail::parse_align(it, end, specs_);
if (it == end) return it;
it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
if (it != end && *it == '?') {
debug_ = true;
++it;
}
if (it != end && (*it == 'g')) path_type_ = *it++;
return it;
} }
template <typename FormatContext> template <typename FormatContext>
auto format(const std::filesystem::path& p, FormatContext& ctx) const -> auto format(const std::filesystem::path& p, FormatContext& ctx) const {
typename FormatContext::iterator { auto specs = specs_;
# ifdef _WIN32
auto path_string = !path_type_ ? p.native() : p.generic_wstring();
# else
auto path_string = !path_type_ ? p.native() : p.generic_string();
# endif
detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
ctx);
if (!debug_) {
auto s = detail::get_path_string<Char>(p, path_string);
return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
}
auto quoted = basic_memory_buffer<Char>(); auto quoted = basic_memory_buffer<Char>();
detail::write_escaped_path(quoted, p); detail::write_escaped_path(quoted, p, path_string);
return formatter<basic_string_view<Char>>::format( return detail::write(ctx.out(),
basic_string_view<Char>(quoted.data(), quoted.size()), ctx); basic_string_view<Char>(quoted.data(), quoted.size()),
specs);
} }
}; };
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif #endif // FMT_CPP_LIB_FILESYSTEM
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
FMT_MODULE_EXPORT FMT_EXPORT
template <std::size_t N, typename Char>
struct formatter<std::bitset<N>, Char> : nested_formatter<string_view> {
private:
// Functor because C++11 doesn't support generic lambdas.
struct writer {
const std::bitset<N>& bs;
template <typename OutputIt>
FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt {
for (auto pos = N; pos > 0; --pos) {
out = detail::write<Char>(out, bs[pos - 1] ? Char('1') : Char('0'));
}
return out;
}
};
public:
template <typename FormatContext>
auto format(const std::bitset<N>& bs, FormatContext& ctx) const
-> decltype(ctx.out()) {
return write_padded(ctx, writer{bs});
}
};
FMT_EXPORT
template <typename Char> template <typename Char>
struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {}; struct formatter<std::thread::id, Char> : basic_ostream_formatter<Char> {};
FMT_END_NAMESPACE FMT_END_NAMESPACE
#ifdef __cpp_lib_optional #ifdef __cpp_lib_optional
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
FMT_MODULE_EXPORT FMT_EXPORT
template <typename T, typename Char> template <typename T, typename Char>
struct formatter<std::optional<T>, Char, struct formatter<std::optional<T>, Char,
std::enable_if_t<is_formattable<T, Char>::value>> { std::enable_if_t<is_formattable<T, Char>::value>> {
@ -132,7 +228,7 @@ struct formatter<std::optional<T>, Char,
} }
template <typename FormatContext> template <typename FormatContext>
auto format(std::optional<T> const& opt, FormatContext& ctx) const auto format(const std::optional<T>& opt, FormatContext& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
if (!opt) return detail::write<Char>(ctx.out(), none); if (!opt) return detail::write<Char>(ctx.out(), none);
@ -146,24 +242,33 @@ struct formatter<std::optional<T>, Char,
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif // __cpp_lib_optional #endif // __cpp_lib_optional
#ifdef __cpp_lib_variant #ifdef __cpp_lib_source_location
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
FMT_MODULE_EXPORT FMT_EXPORT
template <typename Char> struct formatter<std::monostate, Char> { template <> struct formatter<std::source_location> {
template <typename ParseContext> template <typename ParseContext> FMT_CONSTEXPR auto parse(ParseContext& ctx) {
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin(); return ctx.begin();
} }
template <typename FormatContext> template <typename FormatContext>
auto format(const std::monostate&, FormatContext& ctx) const auto format(const std::source_location& loc, FormatContext& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
auto out = ctx.out(); auto out = ctx.out();
out = detail::write<Char>(out, "monostate"); out = detail::write(out, loc.file_name());
out = detail::write(out, ':');
out = detail::write<char>(out, loc.line());
out = detail::write(out, ':');
out = detail::write<char>(out, loc.column());
out = detail::write(out, ": ");
out = detail::write(out, loc.function_name());
return out; return out;
} }
}; };
FMT_END_NAMESPACE
#endif
#if FMT_CPP_LIB_VARIANT
FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
template <typename T> template <typename T>
@ -197,6 +302,7 @@ auto write_variant_alternative(OutputIt out, const T& v) -> OutputIt {
} }
} // namespace detail } // namespace detail
template <typename T> struct is_variant_like { template <typename T> struct is_variant_like {
static constexpr const bool value = detail::is_variant_like_<T>::value; static constexpr const bool value = detail::is_variant_like_<T>::value;
}; };
@ -206,7 +312,21 @@ template <typename T, typename C> struct is_variant_formattable {
detail::is_variant_formattable_<T, C>::value; detail::is_variant_formattable_<T, C>::value;
}; };
FMT_MODULE_EXPORT FMT_EXPORT
template <typename Char> struct formatter<std::monostate, Char> {
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
template <typename FormatContext>
auto format(const std::monostate&, FormatContext& ctx) const
-> decltype(ctx.out()) {
return detail::write<Char>(ctx.out(), "monostate");
}
};
FMT_EXPORT
template <typename Variant, typename Char> template <typename Variant, typename Char>
struct formatter< struct formatter<
Variant, Char, Variant, Char,
@ -223,13 +343,14 @@ struct formatter<
auto out = ctx.out(); auto out = ctx.out();
out = detail::write<Char>(out, "variant("); out = detail::write<Char>(out, "variant(");
try { FMT_TRY {
std::visit( std::visit(
[&](const auto& v) { [&](const auto& v) {
out = detail::write_variant_alternative<Char>(out, v); out = detail::write_variant_alternative<Char>(out, v);
}, },
value); value);
} catch (const std::bad_variant_access&) { }
FMT_CATCH(const std::bad_variant_access&) {
detail::write<Char>(out, "valueless by exception"); detail::write<Char>(out, "valueless by exception");
} }
*out++ = ')'; *out++ = ')';
@ -237,10 +358,10 @@ struct formatter<
} }
}; };
FMT_END_NAMESPACE FMT_END_NAMESPACE
#endif // __cpp_lib_variant #endif // FMT_CPP_LIB_VARIANT
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
FMT_MODULE_EXPORT FMT_EXPORT
template <typename Char> struct formatter<std::error_code, Char> { template <typename Char> struct formatter<std::error_code, Char> {
template <typename ParseContext> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
@ -258,10 +379,10 @@ template <typename Char> struct formatter<std::error_code, Char> {
} }
}; };
FMT_MODULE_EXPORT FMT_EXPORT
template <typename T, typename Char> template <typename T, typename Char>
struct formatter< struct formatter<
T, Char, T, Char, // DEPRECATED! Mixing code unit types.
typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> { typename std::enable_if<std::is_base_of<std::exception, T>::value>::type> {
private: private:
bool with_typename_ = false; bool with_typename_ = false;
@ -274,7 +395,7 @@ struct formatter<
if (it == end || *it == '}') return it; if (it == end || *it == '}') return it;
if (*it == 't') { if (*it == 't') {
++it; ++it;
with_typename_ = true; with_typename_ = FMT_USE_TYPEID != 0;
} }
return it; return it;
} }
@ -287,11 +408,12 @@ struct formatter<
if (!with_typename_) if (!with_typename_)
return detail::write_bytes(out, string_view(ex.what()), spec); return detail::write_bytes(out, string_view(ex.what()), spec);
#if FMT_USE_TYPEID
const std::type_info& ti = typeid(ex); const std::type_info& ti = typeid(ex);
#ifdef FMT_HAS_ABI_CXA_DEMANGLE # ifdef FMT_HAS_ABI_CXA_DEMANGLE
int status = 0; int status = 0;
std::size_t size = 0; std::size_t size = 0;
std::unique_ptr<char, decltype(&std::free)> demangled_name_ptr( std::unique_ptr<char, void (*)(void*)> demangled_name_ptr(
abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free); abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free);
string_view demangled_name_view; string_view demangled_name_view;
@ -327,23 +449,89 @@ struct formatter<
demangled_name_view = string_view(ti.name()); demangled_name_view = string_view(ti.name());
} }
out = detail::write_bytes(out, demangled_name_view, spec); out = detail::write_bytes(out, demangled_name_view, spec);
#elif FMT_MSC_VERSION # elif FMT_MSC_VERSION
string_view demangled_name_view(ti.name()); string_view demangled_name_view(ti.name());
if (demangled_name_view.starts_with("class ")) if (demangled_name_view.starts_with("class "))
demangled_name_view.remove_prefix(6); demangled_name_view.remove_prefix(6);
else if (demangled_name_view.starts_with("struct ")) else if (demangled_name_view.starts_with("struct "))
demangled_name_view.remove_prefix(7); demangled_name_view.remove_prefix(7);
out = detail::write_bytes(out, demangled_name_view, spec); out = detail::write_bytes(out, demangled_name_view, spec);
#else # else
out = detail::write_bytes(out, string_view(ti.name()), spec); out = detail::write_bytes(out, string_view(ti.name()), spec);
# endif
*out++ = ':';
*out++ = ' ';
return detail::write_bytes(out, string_view(ex.what()), spec);
#endif #endif
out = detail::write<Char>(out, Char(':')); }
out = detail::write<Char>(out, Char(' ')); };
out = detail::write_bytes(out, string_view(ex.what()), spec);
return out; namespace detail {
template <typename T, typename Enable = void>
struct has_flip : std::false_type {};
template <typename T>
struct has_flip<T, void_t<decltype(std::declval<T>().flip())>>
: std::true_type {};
template <typename T> struct is_bit_reference_like {
static constexpr const bool value =
std::is_convertible<T, bool>::value &&
std::is_nothrow_assignable<T, bool>::value && has_flip<T>::value;
};
#ifdef _LIBCPP_VERSION
// Workaround for libc++ incompatibility with C++ standard.
// According to the Standard, `bitset::operator[] const` returns bool.
template <typename C>
struct is_bit_reference_like<std::__bit_const_reference<C>> {
static constexpr const bool value = true;
};
#endif
} // namespace detail
// We can't use std::vector<bool, Allocator>::reference and
// std::bitset<N>::reference because the compiler can't deduce Allocator and N
// in partial specialization.
FMT_EXPORT
template <typename BitRef, typename Char>
struct formatter<BitRef, Char,
enable_if_t<detail::is_bit_reference_like<BitRef>::value>>
: formatter<bool, Char> {
template <typename FormatContext>
FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<bool, Char>::format(v, ctx);
} }
}; };
FMT_END_NAMESPACE
FMT_EXPORT
template <typename T, typename Char>
struct formatter<std::atomic<T>, Char,
enable_if_t<is_formattable<T, Char>::value>>
: formatter<T, Char> {
template <typename FormatContext>
auto format(const std::atomic<T>& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<T, Char>::format(v.load(), ctx);
}
};
#ifdef __cpp_lib_atomic_flag_test
FMT_EXPORT
template <typename Char>
struct formatter<std::atomic_flag, Char> : formatter<bool, Char> {
template <typename FormatContext>
auto format(const std::atomic_flag& v, FormatContext& ctx) const
-> decltype(ctx.out()) {
return formatter<bool, Char>::format(v.test(), ctx);
}
};
#endif // __cpp_lib_atomic_flag_test
FMT_END_NAMESPACE
#endif // FMT_STD_H_ #endif // FMT_STD_H_

Loading…
Cancel
Save