forked from fmtlib/fmt
Fix formatting of integer types smaller than int in FormatDec.
This commit is contained in:
@@ -1454,6 +1454,11 @@ std::string FormatDec(T value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatIntTest, FormatDec) {
|
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(-42));
|
EXPECT_EQ("-42", FormatDec(-42));
|
||||||
EXPECT_EQ("42", FormatDec(42l));
|
EXPECT_EQ("42", FormatDec(42l));
|
||||||
|
16
format.h
16
format.h
@@ -229,8 +229,6 @@ struct IntTraitsBase {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Information about an integer type.
|
// Information about an integer type.
|
||||||
// IntTraits is not specialized for integer types smaller than int,
|
|
||||||
// since these are promoted to int.
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct IntTraits : IntTraitsBase<T> {
|
struct IntTraits : IntTraitsBase<T> {
|
||||||
typedef T UnsignedType;
|
typedef T UnsignedType;
|
||||||
@@ -243,11 +241,14 @@ struct SignedIntTraits : IntTraitsBase<T> {
|
|||||||
static bool IsNegative(T value) { return value < 0; }
|
static bool IsNegative(T value) { return value < 0; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
#define FMT_INT_TRAIT(Type) \
|
||||||
struct IntTraits<int> : SignedIntTraits<int, unsigned> {};
|
template <> \
|
||||||
|
struct IntTraits<Type> : SignedIntTraits<Type, unsigned Type> {};
|
||||||
|
|
||||||
template <>
|
FMT_INT_TRAIT(char)
|
||||||
struct IntTraits<long> : SignedIntTraits<long, unsigned long> {};
|
FMT_INT_TRAIT(short)
|
||||||
|
FMT_INT_TRAIT(int)
|
||||||
|
FMT_INT_TRAIT(long)
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct IntTraits<LongLong> : SignedIntTraits<LongLong, ULongLong> {};
|
struct IntTraits<LongLong> : SignedIntTraits<LongLong, ULongLong> {};
|
||||||
@@ -1407,8 +1408,7 @@ class FormatInt {
|
|||||||
// write a terminating null character.
|
// write a terminating null character.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void FormatDec(char *&buffer, T value) {
|
inline void FormatDec(char *&buffer, T value) {
|
||||||
typedef typename internal::IntTraits<T>::MainType UnsignedType;
|
typename internal::IntTraits<T>::MainType abs_value = value;
|
||||||
UnsignedType abs_value = value;
|
|
||||||
if (internal::IntTraits<T>::IsNegative(value)) {
|
if (internal::IntTraits<T>::IsNegative(value)) {
|
||||||
*buffer++ = '-';
|
*buffer++ = '-';
|
||||||
abs_value = 0 - abs_value;
|
abs_value = 0 - abs_value;
|
||||||
|
Reference in New Issue
Block a user