Allow leading zeros in precision (#1579)

This commit is contained in:
Victor Zverovich 2020-03-14 11:37:38 -07:00
parent 678341275b
commit ff486a72a7
2 changed files with 6 additions and 5 deletions

View File

@ -1953,10 +1953,6 @@ template <typename Char, typename ErrorHandler>
FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end, FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end,
ErrorHandler&& eh) { ErrorHandler&& eh) {
FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', ""); FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', "");
if (*begin == '0') {
++begin;
return 0;
}
unsigned value = 0; unsigned value = 0;
// Convert to unsigned to prevent a warning. // Convert to unsigned to prevent a warning.
constexpr unsigned max_int = max_value<int>(); constexpr unsigned max_int = max_value<int>();
@ -2311,7 +2307,11 @@ FMT_CONSTEXPR const Char* parse_arg_id(const Char* begin, const Char* end,
return begin; return begin;
} }
if (c >= '0' && c <= '9') { 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 != ':')) if (begin == end || (*begin != '}' && *begin != ':'))
handler.on_error("invalid format string"); handler.on_error("invalid format string");
else else

View File

@ -1124,6 +1124,7 @@ TEST(FormatterTest, Precision) {
"000000000000000000000000000000000000000000000000000P+127", "000000000000000000000000000000000000000000000000000P+127",
format("{:.838A}", -2.14001164E+38)); format("{:.838A}", -2.14001164E+38));
EXPECT_EQ("123.", format("{:#.0f}", 123.0)); 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)), EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast<void*>(0xcafe)),
format_error, format_error,