Skip to content

Commit

Permalink
clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed May 29, 2019
1 parent 981e30c commit 31a7008
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 77 deletions.
140 changes: 80 additions & 60 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <sstream>

#ifdef FMT_SAFE_DURATION_CAST
#include <safe_duration_cast/chronoconv.hpp>
# include <safe_duration_cast/chronoconv.hpp>
#endif

FMT_BEGIN_NAMESPACE
Expand Down Expand Up @@ -397,7 +397,7 @@ inline bool isfinite(T value) {
return std::isfinite(value);
}
template <typename T> 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<int>::min)() &&
value <= (std::numeric_limits<int>::max)()),
"invalid value");
Expand All @@ -417,31 +417,36 @@ template <typename Rep, typename Period,
typename std::enable_if<std::is_integral<Rep>::value, int>::type = 0>
inline std::chrono::duration<Rep, std::milli> get_milliseconds(
std::chrono::duration<Rep, Period> 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<Period,std::ratio<1>>::value) {
using CommonSecondsType=typename std::common_type<decltype(d),std::chrono::seconds>::type;
int ec;
const auto d_as_common = safe_duration_cast::safe_duration_cast<CommonSecondsType>(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<std::chrono::seconds>(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<Rep, std::milli>>(diff,ec);
if(ec) {
FMT_THROW(format_error("value would cause UB or the wrong result"));
}
return ms;
// if(std::ratio_less<Period,std::ratio<1>>::value) {
using CommonSecondsType =
typename std::common_type<decltype(d), std::chrono::seconds>::type;
int ec;
const auto d_as_common =
safe_duration_cast::safe_duration_cast<CommonSecondsType>(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<std::chrono::seconds>(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<Rep, std::milli>>(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<std::chrono::seconds>(d);
return std::chrono::duration_cast<std::chrono::milliseconds>(d - s);
auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
return std::chrono::duration_cast<std::chrono::milliseconds>(d - s);
#endif
}

Expand Down Expand Up @@ -488,39 +493,39 @@ struct chrono_formatter {
std::chrono::duration<Rep, Period> 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++ = '-';
}
// this may overflow and/or the result may not fit in the
// target type.
#ifdef FMT_SAFE_DURATION_CAST
int ec;
s = safe_duration_cast::safe_duration_cast<seconds>(d,ec);
if(ec) {
FMT_THROW(format_error("value would cause UB or the wrong result"));
s = safe_duration_cast::safe_duration_cast<seconds>(d, ec);
if (ec) {
FMT_THROW(format_error("value would cause UB or the wrong result"));
}
#else
s = std::chrono::duration_cast<seconds>(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); }
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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++ = ':';
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,9 @@ void sprintf_format(Double value, internal::buffer<char>& 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.
Expand Down
12 changes: 6 additions & 6 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ class basic_memory_buffer : private Allocator, public internal::buffer<T> {
template <typename T, std::size_t SIZE, typename Allocator>
void basic_memory_buffer<T, SIZE, Allocator>::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;
Expand Down Expand Up @@ -1188,9 +1188,9 @@ It grisu_prettify(const char* digits, int size, int exp, It it,
if (params.trailing_zeros) {
*it++ = static_cast<Char>('.');
#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<Char>('0'));
Expand Down
16 changes: 8 additions & 8 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

/*
Expand All @@ -60,9 +60,10 @@ TEST(ChronoTest,crash2) {
//,fmt::format_error);
}
*/
TEST(ChronoTest,crash1) {
EXPECT_THROW(fmt::format("{:%R}", std::chrono::duration<char, std::mega>{2}),fmt::format_error);
}
TEST(ChronoTest, crash1) {
EXPECT_THROW(fmt::format("{:%R}", std::chrono::duration<char, std::mega>{2}),
fmt::format_error);
}
TEST(TimeTest, Format) {
std::tm tm = std::tm();
tm.tm_year = 116;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 31a7008

Please sign in to comment.