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)>
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
#if FMT_SAFE_DURATION_CAST
// throwing version of safe_duration_cast
// only available for integer<->integer or float<->float casts
// 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 and invoke undefined behavior
// Standard duration cast, may overflow.
return std::chrono::duration_cast<To>(from);
#endif
}
@ -460,15 +460,14 @@ template <
typename To, typename FromRep, typename FromPeriod,
FMT_ENABLE_IF(!is_same_arithmetic_type<FromRep, typename To::rep>::value)>
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
// mixed integer<->float cast is not supported with safe_duration_cast
// fallback to standard duration cast in this case
// Mixed integer <-> float cast is not supported by safe_duration_cast.
return std::chrono::duration_cast<To>(from);
}
template <typename Duration>
std::time_t to_time_t(
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
// overflow.
return fmt_duration_cast<std::chrono::duration<std::time_t>>(

View File

@ -65,7 +65,7 @@
# define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem
# else
# define FMT_CPP_LIB_FILESYSTEM 0
# endif
# endif
#endif
#ifndef FMT_CPP_LIB_VARIANT
@ -81,8 +81,9 @@ FMT_BEGIN_NAMESPACE
namespace detail {
template <typename Char, typename PathChar> auto get_path_string(
const std::filesystem::path& p, const std::basic_string<PathChar>& native) {
template <typename Char, typename PathChar>
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>)
return to_utf8<wchar_t>(native, to_utf8_error_policy::replace);
else
@ -93,7 +94,8 @@ template <typename Char, typename PathChar>
void write_escaped_path(basic_memory_buffer<Char>& quoted,
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>) {
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()});
@ -131,21 +133,19 @@ template <typename Char> struct formatter<std::filesystem::path, Char> {
debug_ = true;
++it;
}
if (it != end && (*it == 'g' || *it == 'n')) {
if (it != end && (*it == 'g' || *it == 'n'))
path_type_ = *it++;
}
return it;
}
template <typename FormatContext>
auto format(const std::filesystem::path& p, FormatContext& ctx) const {
auto specs = specs_;
auto path_type = path_type_;
# ifdef _WIN32
auto path_string = path_type == 'n' ? p.native() : p.generic_wstring();
# else
auto path_string = path_type == 'n' ? p.native() : p.generic_string();
# endif
# ifdef _WIN32
auto path_string = path_type_ == 'n' ? p.native() : p.generic_wstring();
# else
auto path_string = path_type_ == 'n' ? p.native() : p.generic_string();
# endif
detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
ctx);
@ -161,7 +161,7 @@ template <typename Char> struct formatter<std::filesystem::path, Char> {
}
};
FMT_END_NAMESPACE
#endif // FMT_CPP_LIB_FILESYSTEM
#endif // FMT_CPP_LIB_FILESYSTEM
FMT_BEGIN_NAMESPACE
FMT_EXPORT

View File

@ -25,20 +25,19 @@ TEST(std_test, path) {
EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar");
EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
EXPECT_EQ(fmt::format("{:n}", path("/usr/bin")), "/usr/bin");
EXPECT_EQ(fmt::format("{:g}", path("/usr/bin")), "/usr/bin");
# ifdef _WIN32
# ifdef _WIN32
EXPECT_EQ(fmt::format("{:n}", path("C:\\foo")), "C:\\foo");
EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo");
EXPECT_EQ(fmt::format("{}", path(
L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
L"\x0447\x044B\x043D\x0430")),
EXPECT_EQ(fmt::format("{}", path(L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
L"\x0447\x044B\x043D\x0430")),
"Шчучыншчына");
EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "<EFBFBD>");
EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\"");
# endif
# endif
}
// Test ambiguity problem described in #2954.
@ -290,10 +289,10 @@ TEST(std_test, format_atomic) {
#ifdef __cpp_lib_atomic_flag_test
TEST(std_test, format_atomic_flag) {
std::atomic_flag f = ATOMIC_FLAG_INIT;
(void) f.test_and_set();
(void)f.test_and_set();
EXPECT_EQ(fmt::format("{}", f), "true");
const std::atomic_flag cf = ATOMIC_FLAG_INIT;
EXPECT_EQ(fmt::format("{}", cf), "false");
}
#endif // __cpp_lib_atomic_flag_test
#endif // __cpp_lib_atomic_flag_test