Skip to content

Commit

Permalink
Allow leading zeros in precision (fmtlib#1579)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 14, 2020
1 parent 6783412 commit ff486a7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 5 additions & 5 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1953,10 +1953,6 @@ template <typename Char, typename ErrorHandler>
FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end,
ErrorHandler&& eh) {
FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', "");
if (*begin == '0') {
++begin;
return 0;
}
unsigned value = 0;
// Convert to unsigned to prevent a warning.
constexpr unsigned max_int = max_value<int>();
Expand Down Expand Up @@ -2311,7 +2307,11 @@ FMT_CONSTEXPR const Char* parse_arg_id(const Char* begin, const Char* end,
return begin;
}
if (c >= '0' && c <= '9') {
int index = parse_nonnegative_int(begin, end, handler);
int index = 0;
if (c != '0')
index = parse_nonnegative_int(begin, end, handler);
else
++begin;
if (begin == end || (*begin != '}' && *begin != ':'))
handler.on_error("invalid format string");
else
Expand Down
1 change: 1 addition & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ TEST(FormatterTest, Precision) {
"000000000000000000000000000000000000000000000000000P+127",
format("{:.838A}", -2.14001164E+38));
EXPECT_EQ("123.", format("{:#.0f}", 123.0));
EXPECT_EQ("1.23", format("{:.02f}", 1.234));

EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast<void*>(0xcafe)),
format_error,
Expand Down

0 comments on commit ff486a7

Please sign in to comment.