From 31a70080a63a5213594e4b4e6a33e7e315cf756e Mon Sep 17 00:00:00 2001 From: Paul Dreik Date: Wed, 29 May 2019 19:32:52 +0200 Subject: [PATCH] clang format --- include/fmt/chrono.h | 140 ++++++++++++++++++++++----------------- include/fmt/format-inl.h | 6 +- include/fmt/format.h | 12 ++-- test/chrono-test.cc | 16 ++--- 4 files changed, 97 insertions(+), 77 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 912593f8bca4..a64a17269193 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -17,7 +17,7 @@ #include #ifdef FMT_SAFE_DURATION_CAST -#include +# include #endif FMT_BEGIN_NAMESPACE @@ -397,7 +397,7 @@ inline bool isfinite(T value) { return std::isfinite(value); } template inline int to_int(T value) { - FMT_ASSERT(!isnan(value),"nan to int conversion is UB"); + FMT_ASSERT(!isnan(value), "nan to int conversion is UB"); FMT_ASSERT((value >= (std::numeric_limits::min)() && value <= (std::numeric_limits::max)()), "invalid value"); @@ -417,31 +417,36 @@ template ::value, int>::type = 0> inline std::chrono::duration get_milliseconds( std::chrono::duration d) { - // this may overflow and/or the result may not fit in the - // target type. + // this may overflow and/or the result may not fit in the + // target type. #ifdef FMT_SAFE_DURATION_CAST - // if(std::ratio_less>::value) { - using CommonSecondsType=typename std::common_type::type; - int ec; - const auto d_as_common = safe_duration_cast::safe_duration_cast(d,ec); - if(ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } - const auto d_as_whole_seconds = safe_duration_cast::safe_duration_cast(d_as_common,ec); - if(ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } - //this conversion should be nonproblematic - const auto diff=d_as_common-d_as_whole_seconds; - const auto ms=safe_duration_cast::safe_duration_cast>(diff,ec); - if(ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } - return ms; + // if(std::ratio_less>::value) { + using CommonSecondsType = + typename std::common_type::type; + int ec; + const auto d_as_common = + safe_duration_cast::safe_duration_cast(d, ec); + if (ec) { + FMT_THROW(format_error("value would cause UB or the wrong result")); + } + const auto d_as_whole_seconds = + safe_duration_cast::safe_duration_cast(d_as_common, + ec); + if (ec) { + FMT_THROW(format_error("value would cause UB or the wrong result")); + } + // this conversion should be nonproblematic + const auto diff = d_as_common - d_as_whole_seconds; + const auto ms = safe_duration_cast::safe_duration_cast< + std::chrono::duration>(diff, ec); + if (ec) { + FMT_THROW(format_error("value would cause UB or the wrong result")); + } + return ms; #else - auto s = std::chrono::duration_cast(d); - return std::chrono::duration_cast(d - s); + auto s = std::chrono::duration_cast(d); + return std::chrono::duration_cast(d - s); #endif } @@ -488,7 +493,7 @@ struct chrono_formatter { std::chrono::duration d) : context(ctx), out(o), val(d.count()) { if (d.count() < 0) { - //hmm, what happens if this is INT_MIN? + // hmm, what happens if this is INT_MIN? d = -d; *out++ = '-'; } @@ -496,31 +501,31 @@ struct chrono_formatter { // target type. #ifdef FMT_SAFE_DURATION_CAST int ec; - s = safe_duration_cast::safe_duration_cast(d,ec); - if(ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); + s = safe_duration_cast::safe_duration_cast(d, ec); + if (ec) { + FMT_THROW(format_error("value would cause UB or the wrong result")); } #else s = std::chrono::duration_cast(d); #endif } - //returns true if nan or inf, writes to out. + // returns true if nan or inf, writes to out. bool handle_nan_inf() { - if(isfinite(val)) { - return false; - } - if(isnan(val)) { - write_nan(); - return true; - } - //must be +-inf - if(val>0) { - write_pinf(); - } else { - write_ninf(); - } + if (isfinite(val)) { + return false; + } + if (isnan(val)) { + write_nan(); return true; + } + // must be +-inf + if (val > 0) { + write_pinf(); + } else { + write_ninf(); + } + return true; } Rep hour() const { return mod((s.count() / 3600), 24); } @@ -585,7 +590,9 @@ struct chrono_formatter { void on_tz_name() {} void on_24_hour(numeric_system ns) { - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } if (ns == numeric_system::standard) return write(hour(), 2); auto time = tm(); @@ -594,7 +601,9 @@ struct chrono_formatter { } void on_12_hour(numeric_system ns) { - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } if (ns == numeric_system::standard) return write(hour12(), 2); auto time = tm(); @@ -603,7 +612,9 @@ struct chrono_formatter { } void on_minute(numeric_system ns) { - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } if (ns == numeric_system::standard) return write(minute(), 2); auto time = tm(); @@ -612,10 +623,11 @@ struct chrono_formatter { } void on_second(numeric_system ns) { - if(handle_nan_inf()) { - *out++ = '.'; - handle_nan_inf(); - return;} + if (handle_nan_inf()) { + *out++ = '.'; + handle_nan_inf(); + return; + } if (ns == numeric_system::standard) { write(second(), 2); @@ -632,17 +644,19 @@ struct chrono_formatter { } void on_12_hour_time() { - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } - format_localized(time(), "%r"); + format_localized(time(), "%r"); } void on_24_hour_time() { - if(handle_nan_inf()) { - *out++ = ':'; - handle_nan_inf(); - return; - } + if (handle_nan_inf()) { + *out++ = ':'; + handle_nan_inf(); + return; + } write(hour(), 2); *out++ = ':'; @@ -652,18 +666,24 @@ struct chrono_formatter { void on_iso_time() { on_24_hour_time(); *out++ = ':'; - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } write(second(), 2); } void on_am_pm() { - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } - format_localized(time(), "%p"); + format_localized(time(), "%p"); } void on_duration_value() { - if(handle_nan_inf()) { return;} + if (handle_nan_inf()) { + return; + } out = format_chrono_duration_value(out, val, precision); } diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index fc884a0aa96d..84f9bf7261cc 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -782,9 +782,9 @@ void sprintf_format(Double value, internal::buffer& buf, *format_ptr = '\0'; #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -if(spec.precision>1000) { - throw std::runtime_error("fuzz mode - avoiding large precision"); -} + if (spec.precision > 1000) { + throw std::runtime_error("fuzz mode - avoiding large precision"); + } #endif // Format using snprintf. diff --git a/include/fmt/format.h b/include/fmt/format.h index 289fbe664367..1b6981cafa94 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -529,9 +529,9 @@ class basic_memory_buffer : private Allocator, public internal::buffer { template void basic_memory_buffer::grow(std::size_t size) { #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -if(size>1000) { - throw std::runtime_error("fuzz mode - won't grow that much"); -} + if (size > 1000) { + throw std::runtime_error("fuzz mode - won't grow that much"); + } #endif std::size_t old_capacity = this->capacity(); std::size_t new_capacity = old_capacity + old_capacity / 2; @@ -1188,9 +1188,9 @@ It grisu_prettify(const char* digits, int size, int exp, It it, if (params.trailing_zeros) { *it++ = static_cast('.'); #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - //avoid getting stuck here - if(num_zeros>1000) { - throw std::runtime_error("fuzz mode - avoiding excessive memory"); + // avoid getting stuck here + if (num_zeros > 1000) { + throw std::runtime_error("fuzz mode - avoiding excessive memory"); } #endif it = std::fill_n(it, num_zeros, static_cast('0')); diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 78367ddf5be6..25c43ee0236a 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -47,10 +47,10 @@ std::string format_tm(const std::tm& time, const char* spec, return os.str(); } TEST(ChronoTest, Negative) { -EXPECT_EQ("-12345", fmt::format("{:%Q}", std::chrono::seconds(-12345))); -EXPECT_EQ("-03:25:45", - fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345))); -EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(12345))); + EXPECT_EQ("-12345", fmt::format("{:%Q}", std::chrono::seconds(-12345))); + EXPECT_EQ("-03:25:45", + fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345))); + EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(12345))); } /* @@ -60,9 +60,10 @@ TEST(ChronoTest,crash2) { //,fmt::format_error); } */ -TEST(ChronoTest,crash1) { - EXPECT_THROW(fmt::format("{:%R}", std::chrono::duration{2}),fmt::format_error); - } +TEST(ChronoTest, crash1) { + EXPECT_THROW(fmt::format("{:%R}", std::chrono::duration{2}), + fmt::format_error); +} TEST(TimeTest, Format) { std::tm tm = std::tm(); tm.tm_year = 116; @@ -206,7 +207,6 @@ TEST(ChronoTest, FormatSpecs) { EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(12345))); } - TEST(ChronoTest, InvalidSpecs) { auto sec = std::chrono::seconds(0); EXPECT_THROW_MSG(fmt::format("{:%a}", sec), fmt::format_error, "no date");