Commit 04b34322 authored by Victor Zverovich's avatar Victor Zverovich

Simplify IntTraits.

parent 90d0c50e
...@@ -220,38 +220,33 @@ struct TypeSelector { typedef uint32_t Type; }; ...@@ -220,38 +220,33 @@ struct TypeSelector { typedef uint32_t Type; };
template <> template <>
struct TypeSelector<false> { typedef uint64_t Type; }; struct TypeSelector<false> { typedef uint64_t Type; };
template <typename T> // Checks if a number is negative - used to avoid warnings.
struct IntTraitsBase { template <bool IsSigned>
// Smallest of uint32_t and uint64_t that is large enough to represent struct SignChecker {
// all values of T. template <typename T>
typedef typename static bool IsNegative(T value) { return false; }
TypeSelector<std::numeric_limits<T>::digits <= 32>::Type MainType;
};
// Information about an integer type.
template <typename T>
struct IntTraits : IntTraitsBase<T> {
typedef T UnsignedType;
static bool IsNegative(T) { return false; }
}; };
template <typename T, typename UnsignedT> template <>
struct SignedIntTraits : IntTraitsBase<T> { struct SignChecker<true> {
typedef UnsignedT UnsignedType; template <typename T>
static bool IsNegative(T value) { return value < 0; } static bool IsNegative(T value) { return value < 0; }
}; };
#define FMT_INT_TRAIT(Type) \ // Returns true if value is negative, false otherwise.
template <> \ // Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
struct IntTraits<Type> : SignedIntTraits<Type, unsigned Type> {}; template <typename T>
inline bool IsNegative(T value) {
FMT_INT_TRAIT(char) return SignChecker<std::numeric_limits<T>::is_signed>::IsNegative(value);
FMT_INT_TRAIT(short) }
FMT_INT_TRAIT(int)
FMT_INT_TRAIT(long)
template <> template <typename T>
struct IntTraits<LongLong> : SignedIntTraits<LongLong, ULongLong> {}; struct IntTraits {
// Smallest of uint32_t and uint64_t that is large enough to represent
// all values of T.
typedef typename
TypeSelector<std::numeric_limits<T>::digits <= 32>::Type MainType;
};
template <typename T> template <typename T>
struct IsLongDouble { enum {VALUE = 0}; }; struct IsLongDouble { enum {VALUE = 0}; };
...@@ -867,7 +862,7 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) { ...@@ -867,7 +862,7 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) {
char sign = 0; char sign = 0;
typedef typename internal::IntTraits<T>::MainType UnsignedType; typedef typename internal::IntTraits<T>::MainType UnsignedType;
UnsignedType abs_value = value; UnsignedType abs_value = value;
if (internal::IntTraits<T>::IsNegative(value)) { if (value < 0) {
sign = '-'; sign = '-';
++size; ++size;
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
...@@ -877,8 +872,7 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) { ...@@ -877,8 +872,7 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) {
} }
switch (spec.type()) { switch (spec.type()) {
case 0: case 'd': { case 0: case 'd': {
typename internal::IntTraits<T>::MainType normalized_value = abs_value; unsigned num_digits = internal::CountDigits(abs_value);
unsigned num_digits = internal::CountDigits(normalized_value);
CharPtr p = CharPtr p =
PrepareFilledBuffer(size + num_digits, spec, sign) + 1 - num_digits; PrepareFilledBuffer(size + num_digits, spec, sign) + 1 - num_digits;
internal::FormatDecimal(GetBase(p), abs_value, num_digits); internal::FormatDecimal(GetBase(p), abs_value, num_digits);
...@@ -1409,7 +1403,7 @@ class FormatInt { ...@@ -1409,7 +1403,7 @@ class FormatInt {
template <typename T> template <typename T>
inline void FormatDec(char *&buffer, T value) { inline void FormatDec(char *&buffer, T value) {
typename internal::IntTraits<T>::MainType abs_value = value; typename internal::IntTraits<T>::MainType abs_value = value;
if (internal::IntTraits<T>::IsNegative(value)) { if (internal::IsNegative(value)) {
*buffer++ = '-'; *buffer++ = '-';
abs_value = 0 - abs_value; 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