int_traits -> uint32_or_64_t

This commit is contained in:
Victor Zverovich 2019-07-03 16:01:21 -07:00
parent 1289782f06
commit c286ffc88d
4 changed files with 10 additions and 15 deletions

View File

@ -579,8 +579,7 @@ struct chrono_formatter {
void write(Rep value, int width) { void write(Rep value, int width) {
write_sign(); write_sign();
if (isnan(value)) return write_nan(); if (isnan(value)) return write_nan();
typedef typename int_traits<int>::main_type main_type; uint32_or_64_t<int> n = to_unsigned(
main_type n = to_unsigned(
to_nonnegative_int(value, (std::numeric_limits<int>::max)())); to_nonnegative_int(value, (std::numeric_limits<int>::max)()));
int num_digits = internal::count_digits(n); int num_digits = internal::count_digits(n);
if (width > num_digits) out = std::fill_n(out, width - num_digits, '0'); if (width > num_digits) out = std::fill_n(out, width - num_digits, '0');

View File

@ -157,8 +157,7 @@ FMT_FUNC void format_error_code(internal::buffer<char>& out, int error_code,
static const char ERROR_STR[] = "error "; static const char ERROR_STR[] = "error ";
// Subtract 2 to account for terminating null characters in SEP and ERROR_STR. // Subtract 2 to account for terminating null characters in SEP and ERROR_STR.
std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2; std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
typedef internal::int_traits<int>::main_type main_type; auto abs_value = static_cast<uint32_or_64_t<int>>(error_code);
main_type abs_value = static_cast<main_type>(error_code);
if (internal::is_negative(error_code)) { if (internal::is_negative(error_code)) {
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
++error_code_size; ++error_code_size;

View File

@ -606,12 +606,11 @@ FMT_CONSTEXPR bool is_negative(T) {
return false; return false;
} }
template <typename T> struct int_traits { // Smallest of uint32_t and uint64_t that is large enough to represent all
// Smallest of uint32_t and uint64_t that is large enough to represent // values of T.
// all values of T. template <typename T>
using main_type = using uint32_or_64_t =
conditional_t<std::numeric_limits<T>::digits <= 32, uint32_t, uint64_t>; conditional_t<std::numeric_limits<T>::digits <= 32, uint32_t, uint64_t>;
};
// Static data is placed in this class template for the header-only config. // Static data is placed in this class template for the header-only config.
template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data { template <typename T = void> struct FMT_EXTERN_TEMPLATE_API basic_data {
@ -1273,8 +1272,7 @@ template <typename Range> class basic_writer {
// Writes a decimal integer. // Writes a decimal integer.
template <typename Int> void write_decimal(Int value) { template <typename Int> void write_decimal(Int value) {
typedef typename internal::int_traits<Int>::main_type main_type; auto abs_value = static_cast<uint32_or_64_t<Int>>(value);
main_type abs_value = static_cast<main_type>(value);
bool is_negative = internal::is_negative(value); bool is_negative = internal::is_negative(value);
if (is_negative) abs_value = 0 - abs_value; if (is_negative) abs_value = 0 - abs_value;
int num_digits = internal::count_digits(abs_value); int num_digits = internal::count_digits(abs_value);
@ -1286,7 +1284,7 @@ template <typename Range> class basic_writer {
// The handle_int_type_spec handler that writes an integer. // The handle_int_type_spec handler that writes an integer.
template <typename Int, typename Spec> struct int_writer { template <typename Int, typename Spec> struct int_writer {
typedef typename internal::int_traits<Int>::main_type unsigned_type; using unsigned_type = uint32_or_64_t<Int>;
basic_writer<Range>& writer; basic_writer<Range>& writer;
const Spec& spec; const Spec& spec;

View File

@ -158,8 +158,7 @@ template <typename Char> class printf_width_handler {
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
unsigned operator()(T value) { unsigned operator()(T value) {
typedef typename internal::int_traits<T>::main_type UnsignedType; auto width = static_cast<uint32_or_64_t<T>>(value);
UnsignedType width = static_cast<UnsignedType>(value);
if (internal::is_negative(value)) { if (internal::is_negative(value)) {
spec_.align_ = ALIGN_LEFT; spec_.align_ = ALIGN_LEFT;
width = 0 - width; width = 0 - width;