Commit 90d0c50e authored by Victor Zverovich's avatar Victor Zverovich

Fix formatting of integer types smaller than int in FormatDec.

parent 2f423d8b
......@@ -1454,6 +1454,11 @@ std::string FormatDec(T value) {
}
TEST(FormatIntTest, FormatDec) {
EXPECT_EQ("-42", FormatDec(static_cast<char>(-42)));
EXPECT_EQ("-42", FormatDec(static_cast<short>(-42)));
std::ostringstream os;
os << std::numeric_limits<unsigned short>::max();
EXPECT_EQ(os.str(), FormatDec(std::numeric_limits<unsigned short>::max()));
EXPECT_EQ("42", FormatDec(42));
EXPECT_EQ("-42", FormatDec(-42));
EXPECT_EQ("42", FormatDec(42l));
......
......@@ -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 <typename T>
struct IntTraits : IntTraitsBase<T> {
typedef T UnsignedType;
......@@ -243,11 +241,14 @@ struct SignedIntTraits : IntTraitsBase<T> {
static bool IsNegative(T value) { return value < 0; }
};
template <>
struct IntTraits<int> : SignedIntTraits<int, unsigned> {};
#define FMT_INT_TRAIT(Type) \
template <> \
struct IntTraits<Type> : SignedIntTraits<Type, unsigned Type> {};
template <>
struct IntTraits<long> : SignedIntTraits<long, unsigned long> {};
FMT_INT_TRAIT(char)
FMT_INT_TRAIT(short)
FMT_INT_TRAIT(int)
FMT_INT_TRAIT(long)
template <>
struct IntTraits<LongLong> : SignedIntTraits<LongLong, ULongLong> {};
......@@ -1407,8 +1408,7 @@ class FormatInt {
// write a terminating null character.
template <typename T>
inline void FormatDec(char *&buffer, T value) {
typedef typename internal::IntTraits<T>::MainType UnsignedType;
UnsignedType abs_value = value;
typename internal::IntTraits<T>::MainType abs_value = value;
if (internal::IntTraits<T>::IsNegative(value)) {
*buffer++ = '-';
abs_value = 0 - abs_value;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment