Minor cleanup

This commit is contained in:
Victor Zverovich
2023-11-30 07:34:26 -08:00
parent b87ea22e29
commit 04718008ab
3 changed files with 25 additions and 27 deletions

View File

@@ -444,14 +444,14 @@ template <
FMT_ENABLE_IF(is_same_arithmetic_type<FromRep, typename To::rep>::value)> FMT_ENABLE_IF(is_same_arithmetic_type<FromRep, typename To::rep>::value)>
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) { To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
#if FMT_SAFE_DURATION_CAST #if FMT_SAFE_DURATION_CAST
// throwing version of safe_duration_cast // Throwing version of safe_duration_cast is only available for
// only available for integer<->integer or float<->float casts // integer to integer or float to float casts.
int ec; int ec;
To to = safe_duration_cast::safe_duration_cast<To>(from, ec); To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
if (ec) FMT_THROW(format_error("cannot format duration")); if (ec) FMT_THROW(format_error("cannot format duration"));
return to; return to;
#else #else
// standard duration cast, may overflow and invoke undefined behavior // Standard duration cast, may overflow.
return std::chrono::duration_cast<To>(from); return std::chrono::duration_cast<To>(from);
#endif #endif
} }
@@ -460,15 +460,14 @@ template <
typename To, typename FromRep, typename FromPeriod, typename To, typename FromRep, typename FromPeriod,
FMT_ENABLE_IF(!is_same_arithmetic_type<FromRep, typename To::rep>::value)> FMT_ENABLE_IF(!is_same_arithmetic_type<FromRep, typename To::rep>::value)>
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) { To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
// mixed integer<->float cast is not supported with safe_duration_cast // Mixed integer <-> float cast is not supported by safe_duration_cast.
// fallback to standard duration cast in this case
return std::chrono::duration_cast<To>(from); return std::chrono::duration_cast<To>(from);
} }
template <typename Duration> template <typename Duration>
std::time_t to_time_t( std::time_t to_time_t(
std::chrono::time_point<std::chrono::system_clock, Duration> time_point) { std::chrono::time_point<std::chrono::system_clock, Duration> time_point) {
// cannot use std::chrono::system_clock::to_time_t() since this would first // 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 // require a cast to std::chrono::system_clock::time_point, which could
// overflow. // overflow.
return fmt_duration_cast<std::chrono::duration<std::time_t>>( return fmt_duration_cast<std::chrono::duration<std::time_t>>(

View File

@@ -81,8 +81,9 @@ FMT_BEGIN_NAMESPACE
namespace detail { namespace detail {
template <typename Char, typename PathChar> auto get_path_string( template <typename Char, typename PathChar>
const std::filesystem::path& p, const std::basic_string<PathChar>& native) { auto get_path_string(const std::filesystem::path& p,
const std::basic_string<PathChar>& native) {
if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>) 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); return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
else else
@@ -93,7 +94,8 @@ template <typename Char, typename PathChar>
void write_escaped_path(basic_memory_buffer<Char>& quoted, void write_escaped_path(basic_memory_buffer<Char>& quoted,
const std::filesystem::path& p, const std::filesystem::path& p,
const std::basic_string<PathChar>& native) { const std::basic_string<PathChar>& native) {
if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>) { if constexpr (std::is_same_v<Char, char> &&
std::is_same_v<PathChar, wchar_t>) {
auto buf = basic_memory_buffer<wchar_t>(); auto buf = basic_memory_buffer<wchar_t>();
write_escaped_string<wchar_t>(std::back_inserter(buf), native); write_escaped_string<wchar_t>(std::back_inserter(buf), native);
bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}); bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
@@ -131,21 +133,19 @@ template <typename Char> struct formatter<std::filesystem::path, Char> {
debug_ = true; debug_ = true;
++it; ++it;
} }
if (it != end && (*it == 'g' || *it == 'n')) { if (it != end && (*it == 'g' || *it == 'n'))
path_type_ = *it++; path_type_ = *it++;
}
return 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 {
auto specs = specs_; auto specs = specs_;
auto path_type = path_type_; # ifdef _WIN32
# ifdef _WIN32 auto path_string = path_type_ == 'n' ? p.native() : p.generic_wstring();
auto path_string = path_type == 'n' ? p.native() : p.generic_wstring(); # else
# else auto path_string = path_type_ == 'n' ? p.native() : p.generic_string();
auto path_string = path_type == 'n' ? p.native() : p.generic_string(); # endif
# endif
detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_, detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
ctx); ctx);

View File

@@ -32,8 +32,7 @@ TEST(std_test, path) {
EXPECT_EQ(fmt::format("{:n}", path("C:\\foo")), "C:\\foo"); EXPECT_EQ(fmt::format("{:n}", path("C:\\foo")), "C:\\foo");
EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo"); EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo");
EXPECT_EQ(fmt::format("{}", path( EXPECT_EQ(fmt::format("{}", path(L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
L"\x0447\x044B\x043D\x0430")), L"\x0447\x044B\x043D\x0430")),
"Шчучыншчына"); "Шчучыншчына");
EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "<EFBFBD>"); EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "<EFBFBD>");
@@ -290,7 +289,7 @@ TEST(std_test, format_atomic) {
#ifdef __cpp_lib_atomic_flag_test #ifdef __cpp_lib_atomic_flag_test
TEST(std_test, format_atomic_flag) { TEST(std_test, format_atomic_flag) {
std::atomic_flag f = ATOMIC_FLAG_INIT; std::atomic_flag f = ATOMIC_FLAG_INIT;
(void) f.test_and_set(); (void)f.test_and_set();
EXPECT_EQ(fmt::format("{}", f), "true"); EXPECT_EQ(fmt::format("{}", f), "true");
const std::atomic_flag cf = ATOMIC_FLAG_INIT; const std::atomic_flag cf = ATOMIC_FLAG_INIT;