Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Jun 13, 2019
2 parents 90cab5a + cbbee1b commit 7842582
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
6 changes: 3 additions & 3 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ struct chrono_formatter {

if (ns == numeric_system::standard) return write(hour12(), 2);
auto time = tm();
time.tm_hour = hour12();
time.tm_hour = to_int(hour12());
format_localized(time, "%OI");
}

Expand All @@ -646,7 +646,7 @@ struct chrono_formatter {

if (ns == numeric_system::standard) return write(minute(), 2);
auto time = tm();
time.tm_min = minute();
time.tm_min = to_int(minute());
format_localized(time, "%OM");
}

Expand Down Expand Up @@ -679,7 +679,7 @@ struct chrono_formatter {
return;
}
auto time = tm();
time.tm_sec = second();
time.tm_sec = to_int(second());
format_localized(time, "%OS");
}

Expand Down
1 change: 1 addition & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ template <typename Context> struct arg_mapper {

FMT_CONSTEXPR const void* map(void* val) { return val; }
FMT_CONSTEXPR const void* map(const void* val) { return val; }
FMT_CONSTEXPR const void* map(std::nullptr_t val) { return val; }
template <typename T> FMT_CONSTEXPR int map(const T*) {
// Formatting of arbitrary pointers is disallowed. If you want to output
// a pointer cast it to "void *" or "const void *". In particular, this
Expand Down
29 changes: 15 additions & 14 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,20 +792,21 @@ void sprintf_format(Double value, internal::buffer<char>& buf,
// Find the decimal point.
auto p = buf.data(), end = p + n;
if (*p == '+' || *p == '-') ++p;
if (spec.type == 'a' || spec.type == 'A') p += 2; // Skip "0x".
while (p < end && *p >= '0' && *p <= '9') ++p;
if (p < end && *p != 'e' && *p != 'E') {
if (*p != '.') *p = '.';
if (!spec.type) {
// Keep only one trailing zero after the decimal point.
++p;
if (*p == '0') ++p;
while (p != end && *p >= '1' && *p <= '9') ++p;
char* where = p;
while (p != end && *p == '0') ++p;
if (p == end || *p < '0' || *p > '9') {
if (p != end) std::memmove(where, p, to_unsigned(end - p));
n -= static_cast<unsigned>(p - where);
if (spec.type != 'a' && spec.type != 'A') {
while (p < end && *p >= '0' && *p <= '9') ++p;
if (p < end && *p != 'e' && *p != 'E') {
if (*p != '.') *p = '.';
if (!spec.type) {
// Keep only one trailing zero after the decimal point.
++p;
if (*p == '0') ++p;
while (p != end && *p >= '1' && *p <= '9') ++p;
char* where = p;
while (p != end && *p == '0') ++p;
if (p == end || *p < '0' || *p > '9') {
if (p != end) std::memmove(where, p, to_unsigned(end - p));
n -= static_cast<unsigned>(p - where);
}
}
}
}
Expand Down
30 changes: 17 additions & 13 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1434,20 +1434,24 @@ class arg_formatter_base {
return out();
}

template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value ||
std::is_same<T, char_type>::value)>
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
iterator operator()(T value) {
// MSVC2013 fails to compile separate overloads for bool and char_type so
// use std::is_same instead.
if (std::is_same<T, bool>::value) {
if (specs_ && specs_->type) return (*this)(value ? 1 : 0);
write(value != 0);
} else if (std::is_same<T, char_type>::value) {
internal::handle_char_specs(
specs_, char_spec_handler(*this, static_cast<char_type>(value)));
} else {
specs_ ? writer_.write_int(value, *specs_) : writer_.write(value);
}
if (specs_)
writer_.write_int(value, *specs_);
else
writer_.write(value);
return out();
}

iterator operator()(char_type value) {
internal::handle_char_specs(
specs_, char_spec_handler(*this, static_cast<char_type>(value)));
return out();
}

iterator operator()(bool value) {
if (specs_ && specs_->type) return (*this)(value ? 1 : 0);
write(value != 0);
return out();
}

Expand Down
8 changes: 5 additions & 3 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,8 @@ TEST(FormatterTest, FormatLongDouble) {
safe_sprintf(buffer, "%Le", 392.65l);
EXPECT_EQ(buffer, format("{0:e}", 392.65l));
EXPECT_EQ("+0000392.6", format("{0:+010.4g}", 392.64l));
safe_sprintf(buffer, "%La", 3.31l);
EXPECT_EQ(buffer, format("{:a}", 3.31l));
}

TEST(FormatterTest, FormatChar) {
Expand Down Expand Up @@ -1576,9 +1578,7 @@ TEST(FormatterTest, FormatPointer) {
EXPECT_EQ(format("{}", fmt::ptr(up.get())), format("{}", fmt::ptr(up)));
std::shared_ptr<int> sp(new int(1));
EXPECT_EQ(format("{}", fmt::ptr(sp.get())), format("{}", fmt::ptr(sp)));
#if FMT_USE_NULLPTR
EXPECT_EQ("0x0", format("{}", nullptr));
#endif
}

TEST(FormatterTest, FormatString) {
Expand Down Expand Up @@ -2496,7 +2496,9 @@ TEST(FormatTest, CharTraitsIsNotAmbiguous) {
struct mychar {
int value;
mychar() = default;
mychar(char val) : value(val) {}

template <typename T> mychar(T val) : value(static_cast<int>(val)) {}

operator int() const { return value; }
};

Expand Down

0 comments on commit 7842582

Please sign in to comment.