From c23fa59139c425a3dfa2e5eeaeb3269c251c90d1 Mon Sep 17 00:00:00 2001 From: Paul Dreik Date: Sun, 9 Jun 2019 17:30:22 +0200 Subject: [PATCH] clang format --- include/fmt/chrono.h | 32 ++-- include/fmt/safe_duration_cast.h | 252 ++++++++++++++----------------- include/safe_duration_cast | 1 - 3 files changed, 130 insertions(+), 155 deletions(-) delete mode 120000 include/safe_duration_cast diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 99a1bdda4edb..6dc67d9d0ada 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -398,13 +398,13 @@ inline bool isfinite(T value) { } template inline int to_int(T value) { FMT_ASSERT(!isnan(value), "nan to int conversion is UB"); - if(std::numeric_limits::is_signed) { - //this covers both float and integers - FMT_ASSERT(value >= (std::numeric_limits::min)(), - "value is too small to fit in an int"); + if (std::numeric_limits::is_signed) { + // this covers both float and integers + FMT_ASSERT(value >= (std::numeric_limits::min)(), + "value is too small to fit in an int"); } FMT_ASSERT(value <= (std::numeric_limits::max)(), - "value is too large to fit in an int"); + "value is too large to fit in an int"); return static_cast(value); } @@ -513,13 +513,13 @@ struct chrono_formatter { val = -val; negative = true; } - + // this may overflow and/or the result may not fit in the // target type. #ifdef FMT_SAFE_DURATION_CAST int ec; // might need checked conversion (rep!=Rep) - auto tmpval=std::chrono::duration(val); + auto tmpval = std::chrono::duration(val); s = safe_duration_cast::safe_duration_cast(tmpval, ec); if (ec) { FMT_THROW(format_error("value would cause UB or the wrong result")); @@ -660,16 +660,16 @@ struct chrono_formatter { if (ns == numeric_system::standard) { write(second(), 2); #ifdef FMT_SAFE_DURATION_CAST - int ec; - //convert rep->Rep - using Crep=std::chrono::duration; - using CRep=std::chrono::duration; - auto tmpval = safe_duration_cast::safe_duration_cast(Crep{val}, ec); - if (ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } + int ec; + // convert rep->Rep + using Crep = std::chrono::duration; + using CRep = std::chrono::duration; + auto tmpval = safe_duration_cast::safe_duration_cast(Crep{val}, ec); + if (ec) { + FMT_THROW(format_error("value would cause UB or the wrong result")); + } #else - auto tmpval=std::chrono::duration(val); + auto tmpval = std::chrono::duration(val); #endif auto ms = get_milliseconds(tmpval); if (ms != std::chrono::milliseconds(0)) { diff --git a/include/fmt/safe_duration_cast.h b/include/fmt/safe_duration_cast.h index e40c4760d201..4c1ddd2ac1cb 100644 --- a/include/fmt/safe_duration_cast.h +++ b/include/fmt/safe_duration_cast.h @@ -20,9 +20,9 @@ // https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros#C.2B.2B17 #if __cpp_constexpr >= 201304 -#define SDC_RELAXED_CONSTEXPR constexpr +# define SDC_RELAXED_CONSTEXPR constexpr #else -#define SDC_RELAXED_CONSTEXPR +# define SDC_RELAXED_CONSTEXPR #endif FMT_BEGIN_NAMESPACE @@ -33,91 +33,75 @@ namespace safe_duration_cast { * converts From to To, without loss. If the dynamic value of from * can't be converted to To without loss, ec is set. */ -template::value) > -SDC_RELAXED_CONSTEXPR To -lossless_integral_conversion(const From from, int& ec) -{ +template ::value)> +SDC_RELAXED_CONSTEXPR To lossless_integral_conversion(const From from, + int& ec) { ec = 0; using F = std::numeric_limits; using T = std::numeric_limits; static_assert(F::is_integer, "From must be integral"); static_assert(T::is_integer, "To must be integral"); - if - (F::is_signed == T::is_signed) - { - // A and B are both signed, or both unsigned. - if - (F::digits <= T::digits) - { - // From fits in To without any problem - } - else { - // From does not always fit in To, resort to a dynamic check. - if (from < T::min() || from > T::max()) { - // outside range. - ec = 1; - return {}; - } + if (F::is_signed == T::is_signed) { + // A and B are both signed, or both unsigned. + if (F::digits <= T::digits) { + // From fits in To without any problem + } else { + // From does not always fit in To, resort to a dynamic check. + if (from < T::min() || from > T::max()) { + // outside range. + ec = 1; + return {}; } } + } + + if (F::is_signed && !T::is_signed) { + // From may be negative, not allowed! + if (from < 0) { + ec = 1; + return {}; + } - if - (F::is_signed && !T::is_signed) - { - // From may be negative, not allowed! - if (from < 0) { + // From is positive. Can it always fit in To? + if (F::digits <= T::digits) { + // yes, From always fits in To. + } else { + // from may not fit in To, we have to do a dynamic check + if (from > T::max()) { ec = 1; return {}; } - - // From is positive. Can it always fit in To? - if - (F::digits <= T::digits) - { - // yes, From always fits in To. - } - else { - // from may not fit in To, we have to do a dynamic check - if (from > T::max()) { - ec = 1; - return {}; - } - } } + } - if - (!F::is_signed && T::is_signed) - { - // can from be held in To? - if - (F::digits < T::digits) - { - // yes, From always fits in To. - } - else { - // from may not fit in To, we have to do a dynamic check - if (from > T::max()) { - // outside range. - ec = 1; - return {}; - } + if (!F::is_signed && T::is_signed) { + // can from be held in To? + if (F::digits < T::digits) { + // yes, From always fits in To. + } else { + // from may not fit in To, we have to do a dynamic check + if (from > T::max()) { + // outside range. + ec = 1; + return {}; } } + } // reaching here means all is ok for lossless conversion. return static_cast(from); -} // function +} // function -template::value) > -SDC_RELAXED_CONSTEXPR To - lossless_integral_conversion(const From from, int& ec) { - ec=0; - return from; -} // function +template ::value)> +SDC_RELAXED_CONSTEXPR To lossless_integral_conversion(const From from, + int& ec) { + ec = 0; + return from; +} // function /** * converts From to To if possible, otherwise ec is set. @@ -126,16 +110,13 @@ SDC_RELAXED_CONSTEXPR To * ---------------------------------|--------------- * NaN | NaN * Inf | Inf - * normal, fits in output | converted (possibly with loss of precision) - * normal, does not fit in output | ec is set - * subnormal | best effort - * -Inf | -Inf + * normal, fits in output | converted (possibly with loss of + * precision) normal, does not fit in output | ec is set subnormal | best + * effort -Inf | -Inf */ -template::value) > -SDC_RELAXED_CONSTEXPR To -safe_float_conversion(const From from, int& ec) -{ +template ::value)> +SDC_RELAXED_CONSTEXPR To safe_float_conversion(const From from, int& ec) { ec = 0; using T = std::numeric_limits; static_assert(std::is_floating_point::value, "From must be floating"); @@ -153,25 +134,24 @@ safe_float_conversion(const From from, int& ec) // nan and inf will be preserved return static_cast(from); -} // function - -template::value)> -SDC_RELAXED_CONSTEXPR To - safe_float_conversion(const From from, int& ec) { - ec=0; - static_assert(std::is_floating_point::value, "From must be floating"); - return from; +} // function + +template ::value)> +SDC_RELAXED_CONSTEXPR To safe_float_conversion(const From from, int& ec) { + ec = 0; + static_assert(std::is_floating_point::value, "From must be floating"); + return from; } /** * safe duration cast between integral durations */ -template::value), - FMT_ENABLE_IF(std::is_integral::value)> -To safe_duration_cast(std::chrono::duration from, int& ec) -{ + FMT_ENABLE_IF(std::is_integral::value)> +To safe_duration_cast(std::chrono::duration from, + int& ec) { using From = std::chrono::duration; ec = 0; // the basic idea is that we need to convert from count() in the from type @@ -186,68 +166,65 @@ To safe_duration_cast(std::chrono::duration from, int& ec) // overflow/underflow. let's start by finding a suitable type that can hold // both To, From and Factor::num using IntermediateRep = - typename std::common_type::type; + typename std::common_type::type; // safe conversion to IntermediateRep IntermediateRep count = - lossless_integral_conversion(from.count(), ec); + lossless_integral_conversion(from.count(), ec); if (ec) { return {}; } // multiply with Factor::num without overflow or underflow - if - (Factor::num != 1) - { - constexpr auto max1 = + if (Factor::num != 1) { + constexpr auto max1 = std::numeric_limits::max() / Factor::num; - if (count > max1) { - ec = 1; - return {}; - } - constexpr auto min1 = + if (count > max1) { + ec = 1; + return {}; + } + constexpr auto min1 = std::numeric_limits::min() / Factor::num; - if (count < min1) { - ec = 1; - return {}; - } - count *= Factor::num; + if (count < min1) { + ec = 1; + return {}; } + count *= Factor::num; + } // this can't go wrong, right? den>0 is checked earlier. - if - (Factor::den != 1) { count /= Factor::den; } + if (Factor::den != 1) { + count /= Factor::den; + } // convert to the to type, safely using ToRep = typename To::rep; const ToRep tocount = lossless_integral_conversion(count, ec); if (ec) { return {}; } - return To{ tocount }; + return To{tocount}; } - /** * safe duration_cast between floating point durations */ -template::value), - FMT_ENABLE_IF(std::is_floating_point::value)> -To safe_duration_cast(std::chrono::duration from, int& ec) -{ + FMT_ENABLE_IF(std::is_floating_point::value)> +To safe_duration_cast(std::chrono::duration from, + int& ec) { using From = std::chrono::duration; ec = 0; if (std::isnan(from.count())) { // nan in, gives nan out. easy. - return To{ std::numeric_limits::quiet_NaN() }; + return To{std::numeric_limits::quiet_NaN()}; } // maybe we should also check if from is denormal, and decide what to do about // it. // +-inf should be preserved. if (std::isinf(from.count())) { - return To{ from.count() }; + return To{from.count()}; } // the basic idea is that we need to convert from count() in the from type @@ -262,39 +239,38 @@ To safe_duration_cast(std::chrono::duration from, int& ec) // overflow/underflow. let's start by finding a suitable type that can hold // both To, From and Factor::num using IntermediateRep = - typename std::common_type::type; + typename std::common_type::type; // force conversion of From::rep -> IntermediateRep to be safe, // even if it will never happen be narrowing in this context. - IntermediateRep count = safe_float_conversion(from.count(), ec); + IntermediateRep count = + safe_float_conversion(from.count(), ec); if (ec) { return {}; } // multiply with Factor::num without overflow or underflow - if - (Factor::num != 1) - { - constexpr auto max1 = + if (Factor::num != 1) { + constexpr auto max1 = std::numeric_limits::max() / Factor::num; - if (count > max1) { - ec = 1; - return {}; - } - constexpr auto min1 = + if (count > max1) { + ec = 1; + return {}; + } + constexpr auto min1 = std::numeric_limits::lowest() / Factor::num; - if (count < min1) { - ec = 1; - return {}; - } - count *= Factor::num; + if (count < min1) { + ec = 1; + return {}; } + count *= Factor::num; + } // this can't go wrong, right? den>0 is checked earlier. - if - (Factor::den != 1) { count /= Factor::den; } + if (Factor::den != 1) { + count /= Factor::den; + } // convert to the to type, safely using ToRep = typename To::rep; @@ -303,9 +279,9 @@ To safe_duration_cast(std::chrono::duration from, int& ec) if (ec) { return {}; } - return To{ tocount }; + return To{tocount}; } -} // namespace safe_duration_cast +} // namespace safe_duration_cast FMT_END_NAMESPACE diff --git a/include/safe_duration_cast b/include/safe_duration_cast deleted file mode 120000 index e10f5974cc66..000000000000 --- a/include/safe_duration_cast +++ /dev/null @@ -1 +0,0 @@ -../safe_duration_cast/include/safe_duration_cast/ \ No newline at end of file