mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-19 20:18:49 +00:00
Fix formatting of integer types smaller than int in FormatDec.
This commit is contained in:
parent
2f423d8b46
commit
90d0c50eb3
@ -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));
|
||||
|
16
format.h
16
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 <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;
|
||||
|
Loading…
Reference in New Issue
Block a user