Fix sign-conversion warnings in headers

This commit is contained in:
Florin Iucha 2018-12-16 01:30:22 -05:00
parent 1d2d479afa
commit e4edc84101
3 changed files with 39 additions and 36 deletions

View File

@ -250,7 +250,7 @@ struct chrono_formatter {
void write(int value, int width) { void write(int value, int width) {
typedef typename int_traits<int>::main_type main_type; typedef typename int_traits<int>::main_type main_type;
main_type n = to_unsigned(value); main_type n = to_unsigned(value);
int num_digits = static_cast<int>(internal::count_digits(n)); int num_digits = internal::count_digits(n);
if (width > num_digits) if (width > num_digits)
out = std::fill_n(out, width - num_digits, '0'); out = std::fill_n(out, width - num_digits, '0');
out = format_decimal<char_type>(out, n, num_digits); out = format_decimal<char_type>(out, n, num_digits);

View File

@ -172,7 +172,7 @@ void format_error_code(internal::buffer &out, int error_code,
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
++error_code_size; ++error_code_size;
} }
error_code_size += internal::count_digits(abs_value); error_code_size += static_cast<size_t>(internal::count_digits(abs_value));
writer w(out); writer w(out);
if (message.size() <= inline_buffer_size - error_code_size) { if (message.size() <= inline_buffer_size - error_code_size) {
w.write(message); w.write(message);
@ -739,7 +739,7 @@ FMT_FUNC typename std::enable_if<sizeof(Double) == sizeof(uint64_t), bool>::type
// hi (p1 in Grisu) contains the most significant digits of scaled_upper. // hi (p1 in Grisu) contains the most significant digits of scaled_upper.
// hi = floor(upper / one). // hi = floor(upper / one).
uint32_t hi = static_cast<uint32_t>(upper.f >> -one.e); uint32_t hi = static_cast<uint32_t>(upper.f >> -one.e);
int exp = static_cast<int>(count_digits(hi)); // kappa in Grisu. int exp = count_digits(hi); // kappa in Grisu.
gen_digits_params params = process_specs(specs, cached_exp + exp, buf); gen_digits_params params = process_specs(specs, cached_exp + exp, buf);
fp_value.normalize(); fp_value.normalize();
fp scaled_value = fp_value * cached_pow; fp scaled_value = fp_value * cached_pow;

View File

@ -763,16 +763,16 @@ typedef basic_data<> data;
#ifdef FMT_BUILTIN_CLZLL #ifdef FMT_BUILTIN_CLZLL
// Returns the number of decimal digits in n. Leading zeros are not counted // Returns the number of decimal digits in n. Leading zeros are not counted
// except for n == 0 in which case count_digits returns 1. // except for n == 0 in which case count_digits returns 1.
inline unsigned count_digits(uint64_t n) { inline int count_digits(uint64_t n) {
// Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 // Based on http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
// and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits. // and the benchmark https://github.com/localvoid/cxx-benchmark-count-digits.
int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12; int t = (64 - FMT_BUILTIN_CLZLL(n | 1)) * 1233 >> 12;
return to_unsigned(t) - (n < data::ZERO_OR_POWERS_OF_10_64[t]) + 1; return t - (n < data::ZERO_OR_POWERS_OF_10_64[t]) + 1;
} }
#else #else
// Fallback version of count_digits used when __builtin_clz is not available. // Fallback version of count_digits used when __builtin_clz is not available.
inline unsigned count_digits(uint64_t n) { inline int count_digits(uint64_t n) {
unsigned count = 1; int count = 1;
for (;;) { for (;;) {
// Integer division is slow so do it for a group of four digits instead // Integer division is slow so do it for a group of four digits instead
// of for every digit. The idea comes from the talk by Alexandrescu // of for every digit. The idea comes from the talk by Alexandrescu
@ -893,9 +893,9 @@ class decimal_formatter_null : public decimal_formatter {
#ifdef FMT_BUILTIN_CLZ #ifdef FMT_BUILTIN_CLZ
// Optional version of count_digits for better performance on 32-bit platforms. // Optional version of count_digits for better performance on 32-bit platforms.
inline unsigned count_digits(uint32_t n) { inline int count_digits(uint32_t n) {
int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12; int t = (32 - FMT_BUILTIN_CLZ(n | 1)) * 1233 >> 12;
return to_unsigned(t) - (n < data::ZERO_OR_POWERS_OF_10_32[t]) + 1; return t - (n < data::ZERO_OR_POWERS_OF_10_32[t]) + 1;
} }
#endif #endif
@ -1006,7 +1006,7 @@ inline Char *format_uint(Char *buffer, UInt value, int num_digits,
do { do {
const char *digits = upper ? "0123456789ABCDEF" : "0123456789abcdef"; const char *digits = upper ? "0123456789ABCDEF" : "0123456789abcdef";
unsigned digit = (value & ((1 << BASE_BITS) - 1)); unsigned digit = (value & ((1 << BASE_BITS) - 1));
*--buffer = static_cast<Char>(BASE_BITS < 4 ? '0' + digit : digits[digit]); *--buffer = static_cast<Char>(BASE_BITS < 4 ? static_cast<char>('0' + digit) : digits[digit]);
} while ((value >>= BASE_BITS) != 0); } while ((value >>= BASE_BITS) != 0);
return end; return end;
} }
@ -1961,7 +1961,9 @@ FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out) {
template <> template <>
inline bool find<false, char>( inline bool find<false, char>(
const char *first, const char *last, char value, const char *&out) { const char *first, const char *last, char value, const char *&out) {
out = static_cast<const char*>(std::memchr(first, value, last - first)); const std::ptrdiff_t length = last - first;
FMT_ASSERT(length >= 0, "invalid input range");
out = static_cast<const char*>(std::memchr(first, value, static_cast<size_t>(length)));
return out != FMT_NULL; return out != FMT_NULL;
} }
@ -2041,10 +2043,11 @@ FMT_CONSTEXPR const typename ParseContext::char_type *
template <typename Char, typename ErrorHandler, typename... Args> template <typename Char, typename ErrorHandler, typename... Args>
class format_string_checker { class format_string_checker {
enum { INVALID_ARG_ID = 0xffffffffu };
public: public:
explicit FMT_CONSTEXPR format_string_checker( explicit FMT_CONSTEXPR format_string_checker(
basic_string_view<Char> format_str, ErrorHandler eh) basic_string_view<Char> format_str, ErrorHandler eh)
: arg_id_(-1), context_(format_str, eh), : arg_id_(INVALID_ARG_ID), context_(format_str, eh),
parse_funcs_{&parse_format_specs<Args, parse_context_type>...} {} parse_funcs_{&parse_format_specs<Args, parse_context_type>...} {}
FMT_CONSTEXPR void on_text(const Char *, const Char *) {} FMT_CONSTEXPR void on_text(const Char *, const Char *) {}
@ -2064,7 +2067,7 @@ class format_string_checker {
FMT_CONSTEXPR const Char *on_format_specs(const Char *begin, const Char *) { FMT_CONSTEXPR const Char *on_format_specs(const Char *begin, const Char *) {
context_.advance_to(begin); context_.advance_to(begin);
return to_unsigned(arg_id_) < NUM_ARGS ? return arg_id_ < NUM_ARGS ?
parse_funcs_[arg_id_](context_) : begin; parse_funcs_[arg_id_](context_) : begin;
} }
@ -2077,14 +2080,14 @@ class format_string_checker {
enum { NUM_ARGS = sizeof...(Args) }; enum { NUM_ARGS = sizeof...(Args) };
FMT_CONSTEXPR void check_arg_id() { FMT_CONSTEXPR void check_arg_id() {
if (internal::to_unsigned(arg_id_) >= NUM_ARGS) if (arg_id_ >= NUM_ARGS)
context_.on_error("argument index out of range"); context_.on_error("argument index out of range");
} }
// Format specifier parsing function. // Format specifier parsing function.
typedef const Char *(*parse_func)(parse_context_type &); typedef const Char *(*parse_func)(parse_context_type &);
int arg_id_; unsigned arg_id_;
parse_context_type context_; parse_context_type context_;
parse_func parse_funcs_[NUM_ARGS > 0 ? NUM_ARGS : 1]; parse_func parse_funcs_[NUM_ARGS > 0 ? NUM_ARGS : 1];
}; };
@ -2287,9 +2290,9 @@ class basic_writer {
// <left-padding><prefix><numeric-padding><digits><right-padding> // <left-padding><prefix><numeric-padding><digits><right-padding>
// where <digits> are written by f(it). // where <digits> are written by f(it).
template <typename Spec, typename F> template <typename Spec, typename F>
void write_int(unsigned num_digits, string_view prefix, void write_int(int num_digits, string_view prefix,
const Spec &spec, F f) { const Spec &spec, F f) {
std::size_t size = prefix.size() + num_digits; std::size_t size = prefix.size() + static_cast<size_t>(num_digits);
char_type fill = static_cast<char_type>(spec.fill()); char_type fill = static_cast<char_type>(spec.fill());
std::size_t padding = 0; std::size_t padding = 0;
if (spec.align() == ALIGN_NUMERIC) { if (spec.align() == ALIGN_NUMERIC) {
@ -2297,9 +2300,9 @@ class basic_writer {
padding = spec.width() - size; padding = spec.width() - size;
size = spec.width(); size = spec.width();
} }
} else if (spec.precision > static_cast<int>(num_digits)) { } else if (spec.precision > num_digits) {
size = prefix.size() + internal::to_unsigned(spec.precision); size = prefix.size() + internal::to_unsigned(spec.precision);
padding = internal::to_unsigned(spec.precision) - num_digits; padding = internal::to_unsigned(spec.precision - num_digits);
fill = static_cast<char_type>('0'); fill = static_cast<char_type>('0');
} }
align_spec as = spec; align_spec as = spec;
@ -2316,8 +2319,8 @@ class basic_writer {
bool is_negative = internal::is_negative(value); bool is_negative = internal::is_negative(value);
if (is_negative) if (is_negative)
abs_value = 0 - abs_value; abs_value = 0 - abs_value;
unsigned num_digits = internal::count_digits(abs_value); int num_digits = internal::count_digits(abs_value);
auto &&it = reserve((is_negative ? 1 : 0) + num_digits); auto &&it = reserve((is_negative ? 1 : 0) + static_cast<size_t>(num_digits));
if (is_negative) if (is_negative)
*it++ = static_cast<char_type>('-'); *it++ = static_cast<char_type>('-');
it = internal::format_decimal<char_type>(it, abs_value, num_digits); it = internal::format_decimal<char_type>(it, abs_value, num_digits);
@ -2338,9 +2341,9 @@ class basic_writer {
// Counts the number of digits in abs_value. BITS = log2(radix). // Counts the number of digits in abs_value. BITS = log2(radix).
template <unsigned BITS> template <unsigned BITS>
unsigned count_digits() const { int count_digits() const {
unsigned_type n = abs_value; unsigned_type n = abs_value;
unsigned num_digits = 0; int num_digits = 0;
do { do {
++num_digits; ++num_digits;
} while ((n >>= BITS) != 0); } while ((n >>= BITS) != 0);
@ -2362,7 +2365,7 @@ class basic_writer {
struct dec_writer { struct dec_writer {
unsigned_type abs_value; unsigned_type abs_value;
unsigned num_digits; int num_digits;
template <typename It> template <typename It>
void operator()(It &&it) const { void operator()(It &&it) const {
@ -2371,14 +2374,14 @@ class basic_writer {
}; };
void on_dec() { void on_dec() {
unsigned num_digits = internal::count_digits(abs_value); int num_digits = internal::count_digits(abs_value);
writer.write_int(num_digits, get_prefix(), spec, writer.write_int(num_digits, get_prefix(), spec,
dec_writer{abs_value, num_digits}); dec_writer{abs_value, num_digits});
} }
struct hex_writer { struct hex_writer {
int_writer &self; int_writer &self;
unsigned num_digits; int num_digits;
template <typename It> template <typename It>
void operator()(It &&it) const { void operator()(It &&it) const {
@ -2392,7 +2395,7 @@ class basic_writer {
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
prefix[prefix_size++] = static_cast<char>(spec.type); prefix[prefix_size++] = static_cast<char>(spec.type);
} }
unsigned num_digits = count_digits<4>(); int num_digits = count_digits<4>();
writer.write_int(num_digits, get_prefix(), spec, writer.write_int(num_digits, get_prefix(), spec,
hex_writer{*this, num_digits}); hex_writer{*this, num_digits});
} }
@ -2400,7 +2403,7 @@ class basic_writer {
template <int BITS> template <int BITS>
struct bin_writer { struct bin_writer {
unsigned_type abs_value; unsigned_type abs_value;
unsigned num_digits; int num_digits;
template <typename It> template <typename It>
void operator()(It &&it) const { void operator()(It &&it) const {
@ -2413,15 +2416,15 @@ class basic_writer {
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
prefix[prefix_size++] = static_cast<char>(spec.type); prefix[prefix_size++] = static_cast<char>(spec.type);
} }
unsigned num_digits = count_digits<1>(); int num_digits = count_digits<1>();
writer.write_int(num_digits, get_prefix(), spec, writer.write_int(num_digits, get_prefix(), spec,
bin_writer<1>{abs_value, num_digits}); bin_writer<1>{abs_value, num_digits});
} }
void on_oct() { void on_oct() {
unsigned num_digits = count_digits<3>(); int num_digits = count_digits<3>();
if (spec.has(HASH_FLAG) && if (spec.has(HASH_FLAG) &&
spec.precision <= static_cast<int>(num_digits)) { spec.precision <= num_digits) {
// Octal prefix '0' is counted as a digit, so only add it if precision // Octal prefix '0' is counted as a digit, so only add it if precision
// is not greater than the number of digits. // is not greater than the number of digits.
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
@ -2434,7 +2437,7 @@ class basic_writer {
struct num_writer { struct num_writer {
unsigned_type abs_value; unsigned_type abs_value;
unsigned size; int size;
char_type sep; char_type sep;
template <typename It> template <typename It>
@ -2446,9 +2449,9 @@ class basic_writer {
}; };
void on_num() { void on_num() {
unsigned num_digits = internal::count_digits(abs_value); int num_digits = internal::count_digits(abs_value);
char_type sep = internal::thousands_sep<char_type>(writer.locale_); char_type sep = internal::thousands_sep<char_type>(writer.locale_);
unsigned size = num_digits + SEP_SIZE * ((num_digits - 1) / 3); int size = num_digits + SEP_SIZE * ((num_digits - 1) / 3);
writer.write_int(size, get_prefix(), spec, writer.write_int(size, get_prefix(), spec,
num_writer{abs_value, size, sep}); num_writer{abs_value, size, sep});
} }
@ -2904,9 +2907,9 @@ inline void format_decimal(char *&buffer, T value) {
*buffer++ = internal::data::DIGITS[index + 1]; *buffer++ = internal::data::DIGITS[index + 1];
return; return;
} }
unsigned num_digits = internal::count_digits(abs_value); int num_digits = internal::count_digits(abs_value);
internal::format_decimal<char>( internal::format_decimal<char>(
internal::make_checked(buffer, num_digits), abs_value, num_digits); internal::make_checked(buffer, static_cast<size_t>(num_digits)), abs_value, num_digits);
buffer += num_digits; buffer += num_digits;
} }