From ff486a72a764ff9f0ea3be460bd713ed7a237db6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 14 Mar 2020 11:37:38 -0700 Subject: [PATCH] Allow leading zeros in precision (#1579) --- include/fmt/format.h | 10 +++++----- test/format-test.cc | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index ee7f0f93..a529383a 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1953,10 +1953,6 @@ template 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(); @@ -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 diff --git a/test/format-test.cc b/test/format-test.cc index af72acaf..a2223749 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -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(0xcafe)), format_error,