Skip to content

Commit

Permalink
Fix -Wsign-conversion warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 25, 2020
1 parent 664dd88 commit 96c68af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
6 changes: 4 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,10 @@ template <typename T> class buffer {
/** Appends data to the end of the buffer. */
template <typename U> void append(const U* begin, const U* end);

T& operator[](std::size_t index) { return ptr_[index]; }
const T& operator[](std::size_t index) const { return ptr_[index]; }
template <typename I> T& operator[](I index) { return ptr_[index]; }
template <typename I> const T& operator[](I index) const {
return ptr_[index];
}
};

// A container-backed buffer.
Expand Down
42 changes: 23 additions & 19 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,23 @@ class bigint {
basic_memory_buffer<bigit, bigits_capacity> bigits_;
int exp_;

bigit operator[](int index) const { return bigits_[to_unsigned(index)]; }
bigit& operator[](int index) { return bigits_[to_unsigned(index)]; }

static FMT_CONSTEXPR_DECL const int bigit_bits = bits<bigit>::value;

friend struct formatter<bigint>;

void subtract_bigits(int index, bigit other, bigit& borrow) {
auto result = static_cast<double_bigit>(bigits_[index]) - other - borrow;
bigits_[index] = static_cast<bigit>(result);
auto result = static_cast<double_bigit>((*this)[index]) - other - borrow;
(*this)[index] = static_cast<bigit>(result);
borrow = static_cast<bigit>(result >> (bigit_bits * 2 - 1));
}

void remove_leading_zeros() {
int num_bigits = static_cast<int>(bigits_.size()) - 1;
while (num_bigits > 0 && bigits_[num_bigits] == 0) --num_bigits;
bigits_.resize(num_bigits + 1);
while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits;
bigits_.resize(to_unsigned(num_bigits + 1));
}

// Computes *this -= other assuming aligned bigints and *this >= other.
Expand Down Expand Up @@ -621,7 +624,7 @@ class bigint {
int end = i - j;
if (end < 0) end = 0;
for (; i >= end; --i, --j) {
bigit lhs_bigit = lhs.bigits_[i], rhs_bigit = rhs.bigits_[j];
bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j];
if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1;
}
if (i != j) return i > j ? 1 : -1;
Expand All @@ -636,7 +639,7 @@ class bigint {
if (max_lhs_bigits + 1 < num_rhs_bigits) return -1;
if (max_lhs_bigits > num_rhs_bigits) return 1;
auto get_bigit = [](const bigint& n, int i) -> bigit {
return i >= n.exp_ && i < n.num_bigits() ? n.bigits_[i - n.exp_] : 0;
return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0;
};
double_bigit borrow = 0;
int min_exp = (std::min)((std::min)(lhs1.exp_, lhs2.exp_), rhs.exp_);
Expand Down Expand Up @@ -676,7 +679,7 @@ class bigint {
basic_memory_buffer<bigit, bigits_capacity> n(std::move(bigits_));
int num_bigits = static_cast<int>(bigits_.size());
int num_result_bigits = 2 * num_bigits;
bigits_.resize(num_result_bigits);
bigits_.resize(to_unsigned(num_result_bigits));
using accumulator_t = conditional_t<FMT_USE_INT128, uint128_t, accumulator>;
auto sum = accumulator_t();
for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) {
Expand All @@ -686,15 +689,15 @@ class bigint {
// Most terms are multiplied twice which can be optimized in the future.
sum += static_cast<double_bigit>(n[i]) * n[j];
}
bigits_[bigit_index] = static_cast<bigit>(sum);
(*this)[bigit_index] = static_cast<bigit>(sum);
sum >>= bits<bigit>::value; // Compute the carry.
}
// Do the same for the top half.
for (int bigit_index = num_bigits; bigit_index < num_result_bigits;
++bigit_index) {
for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;)
sum += static_cast<double_bigit>(n[i++]) * n[j--];
bigits_[bigit_index] = static_cast<bigit>(sum);
(*this)[bigit_index] = static_cast<bigit>(sum);
sum >>= bits<bigit>::value;
}
--num_result_bigits;
Expand All @@ -708,11 +711,11 @@ class bigint {
FMT_ASSERT(this != &divisor, "");
if (compare(*this, divisor) < 0) return 0;
int num_bigits = static_cast<int>(bigits_.size());
FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1] != 0, "");
FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, "");
int exp_difference = exp_ - divisor.exp_;
if (exp_difference > 0) {
// Align bigints by adding trailing zeros to simplify subtraction.
bigits_.resize(num_bigits + exp_difference);
bigits_.resize(to_unsigned(num_bigits + exp_difference));
for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j)
bigits_[j] = bigits_[i];
std::uninitialized_fill_n(bigits_.data(), exp_difference, 0);
Expand Down Expand Up @@ -1023,7 +1026,7 @@ void fallback_format(Double d, buffer<char>& buf, int& exp10) {
if (result > 0 || (result == 0 && (digit % 2) != 0))
++data[num_digits - 1];
}
buf.resize(num_digits);
buf.resize(to_unsigned(num_digits));
exp10 -= num_digits - 1;
return;
}
Expand Down Expand Up @@ -1157,10 +1160,11 @@ int snprintf_float(T value, int precision, float_specs specs,
buf.reserve(buf.capacity() + 1); // The buffer will grow exponentially.
continue;
}
unsigned size = to_unsigned(result);
auto size = to_unsigned(result);
// Size equal to capacity means that the last character was truncated.
if (size >= capacity) {
buf.reserve(size + offset + 1); // Add 1 for the terminating '\0'.
// Add 1 for the terminating '\0'.
buf.reserve(size + offset + 1);
continue;
}
auto is_digit = [](char c) { return c >= '0' && c <= '9'; };
Expand All @@ -1175,7 +1179,7 @@ int snprintf_float(T value, int precision, float_specs specs,
--p;
} while (is_digit(*p));
int fraction_size = static_cast<int>(end - p - 1);
std::memmove(p, p + 1, fraction_size);
std::memmove(p, p + 1, to_unsigned(fraction_size));
buf.resize(size - 1);
return -fraction_size;
}
Expand Down Expand Up @@ -1204,9 +1208,9 @@ int snprintf_float(T value, int precision, float_specs specs,
while (*fraction_end == '0') --fraction_end;
// Move the fractional part left to get rid of the decimal point.
fraction_size = static_cast<int>(fraction_end - begin - 1);
std::memmove(begin + 1, begin + 2, fraction_size);
std::memmove(begin + 1, begin + 2, to_unsigned(fraction_size));
}
buf.resize(fraction_size + offset + 1);
buf.resize(to_unsigned(fraction_size) + offset + 1);
return exp - fraction_size;
}
}
Expand Down Expand Up @@ -1277,7 +1281,7 @@ template <> struct formatter<internal::bigint> {
auto out = ctx.out();
bool first = true;
for (auto i = n.bigits_.size(); i > 0; --i) {
auto value = n.bigits_[i - 1];
auto value = n.bigits_[i - 1u];
if (first) {
out = format_to(out, "{:x}", value);
first = false;
Expand Down Expand Up @@ -1313,7 +1317,7 @@ FMT_FUNC internal::utf8_to_utf16::utf8_to_utf16(string_view s) {
}
if (auto num_chars_left = s.data() + s.size() - p) {
char buf[2 * block_size - 1] = {};
memcpy(buf, p, num_chars_left);
memcpy(buf, p, to_unsigned(num_chars_left));
p = buf;
do {
p = transcode(p);
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ FMT_CONSTEXPR const Char* parse_align(const Char* begin, const Char* end,
auto c = *begin;
if (c == '{')
return handler.on_error("invalid fill character '{'"), begin;
handler.on_fill(basic_string_view<Char>(begin, p - begin));
handler.on_fill(basic_string_view<Char>(begin, to_unsigned(p - begin)));
begin = p + 1;
} else
++begin;
Expand Down

0 comments on commit 96c68af

Please sign in to comment.