From 90d0c50eb372978d9afc65425fae42159c35288b Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 20 Feb 2014 07:04:54 -0800 Subject: [PATCH] Fix formatting of integer types smaller than int in FormatDec. --- format-test.cc | 5 +++++ format.h | 16 ++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/format-test.cc b/format-test.cc index b9ea8ed0..b44e4946 100644 --- a/format-test.cc +++ b/format-test.cc @@ -1454,6 +1454,11 @@ std::string FormatDec(T value) { } TEST(FormatIntTest, FormatDec) { + EXPECT_EQ("-42", FormatDec(static_cast(-42))); + EXPECT_EQ("-42", FormatDec(static_cast(-42))); + std::ostringstream os; + os << std::numeric_limits::max(); + EXPECT_EQ(os.str(), FormatDec(std::numeric_limits::max())); EXPECT_EQ("42", FormatDec(42)); EXPECT_EQ("-42", FormatDec(-42)); EXPECT_EQ("42", FormatDec(42l)); diff --git a/format.h b/format.h index 9c5495ca..0dc987f4 100644 --- a/format.h +++ b/format.h @@ -229,8 +229,6 @@ struct IntTraitsBase { }; // Information about an integer type. -// IntTraits is not specialized for integer types smaller than int, -// since these are promoted to int. template struct IntTraits : IntTraitsBase { typedef T UnsignedType; @@ -243,11 +241,14 @@ struct SignedIntTraits : IntTraitsBase { static bool IsNegative(T value) { return value < 0; } }; -template <> -struct IntTraits : SignedIntTraits {}; +#define FMT_INT_TRAIT(Type) \ + template <> \ + struct IntTraits : SignedIntTraits {}; -template <> -struct IntTraits : SignedIntTraits {}; +FMT_INT_TRAIT(char) +FMT_INT_TRAIT(short) +FMT_INT_TRAIT(int) +FMT_INT_TRAIT(long) template <> struct IntTraits : SignedIntTraits {}; @@ -1407,8 +1408,7 @@ class FormatInt { // write a terminating null character. template inline void FormatDec(char *&buffer, T value) { - typedef typename internal::IntTraits::MainType UnsignedType; - UnsignedType abs_value = value; + typename internal::IntTraits::MainType abs_value = value; if (internal::IntTraits::IsNegative(value)) { *buffer++ = '-'; abs_value = 0 - abs_value;