From ca31c2b32211d4f8b93c86787a11fd39e99d8c0d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 20 Jun 2014 08:04:44 -0700 Subject: [PATCH] Fix handling of octal prefix. --- format.h | 4 ++-- test/printf-test.cc | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/format.h b/format.h index 26e1ae3e..56bf470a 100644 --- a/format.h +++ b/format.h @@ -1313,8 +1313,8 @@ typename fmt::BasicWriter::CharPtr if (spec.precision() > static_cast(num_digits)) { // Octal prefix '0' is counted as a digit, so ignore it if precision // is specified. - if (prefix_size == 1) - prefix_size = 0; + if (prefix_size > 0 && prefix[prefix_size - 1] == '0') + --prefix_size; unsigned number_size = prefix_size + spec.precision(); if (number_size < width) { buffer_.reserve(width); diff --git a/test/printf-test.cc b/test/printf-test.cc index 4ae45964..e5f7f7a1 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -229,6 +229,7 @@ TEST(PrintfTest, DynamicWidth) { TEST(PrintfTest, Precision) { EXPECT_PRINTF("00042", "%.5d", 42); + EXPECT_PRINTF("-00042", "%.5d", -42); EXPECT_PRINTF("00042", "%.5x", 0x42); EXPECT_PRINTF("0x00042", "%#.5x", 0x42); EXPECT_PRINTF("00042", "%.5o", 042);