Commit 4b551344 by Thomas Köppe Committed by Copybara-Service

Revert change "Fix "unsafe narrowing" warnings in absl, 4/n.".

The change breaks existing code by changing the return type of absl::bit_width.

PiperOrigin-RevId: 465295951
Change-Id: Id4ce7c2ac3699ce22aa2b4851a949f9e0104a3d7
parent 751ade00
...@@ -131,9 +131,10 @@ has_single_bit(T x) noexcept { ...@@ -131,9 +131,10 @@ has_single_bit(T x) noexcept {
// fractional part discarded. // fractional part discarded.
template <class T> template <class T>
ABSL_INTERNAL_CONSTEXPR_CLZ inline ABSL_INTERNAL_CONSTEXPR_CLZ inline
typename std::enable_if<std::is_unsigned<T>::value, int>::type typename std::enable_if<std::is_unsigned<T>::value, T>::type
bit_width(T x) noexcept { bit_width(T x) noexcept {
return std::numeric_limits<T>::digits - countl_zero(x); return std::numeric_limits<T>::digits -
static_cast<unsigned int>(countl_zero(x));
} }
// Returns: If x == 0, 0; otherwise the maximal value y such that // Returns: If x == 0, 0; otherwise the maximal value y such that
......
...@@ -157,13 +157,13 @@ ABSL_DLL const char kToUpper[256] = { ...@@ -157,13 +157,13 @@ ABSL_DLL const char kToUpper[256] = {
void AsciiStrToLower(std::string* s) { void AsciiStrToLower(std::string* s) {
for (auto& ch : *s) { for (auto& ch : *s) {
ch = absl::ascii_tolower(static_cast<unsigned char>(ch)); ch = absl::ascii_tolower(ch);
} }
} }
void AsciiStrToUpper(std::string* s) { void AsciiStrToUpper(std::string* s) {
for (auto& ch : *s) { for (auto& ch : *s) {
ch = absl::ascii_toupper(static_cast<unsigned char>(ch)); ch = absl::ascii_toupper(ch);
} }
} }
...@@ -183,17 +183,17 @@ void RemoveExtraAsciiWhitespace(std::string* str) { ...@@ -183,17 +183,17 @@ void RemoveExtraAsciiWhitespace(std::string* str) {
for (; input_it < input_end; ++input_it) { for (; input_it < input_end; ++input_it) {
if (is_ws) { if (is_ws) {
// Consecutive whitespace? Keep only the last. // Consecutive whitespace? Keep only the last.
is_ws = absl::ascii_isspace(static_cast<unsigned char>(*input_it)); is_ws = absl::ascii_isspace(*input_it);
if (is_ws) --output_it; if (is_ws) --output_it;
} else { } else {
is_ws = absl::ascii_isspace(static_cast<unsigned char>(*input_it)); is_ws = absl::ascii_isspace(*input_it);
} }
*output_it = *input_it; *output_it = *input_it;
++output_it; ++output_it;
} }
str->erase(static_cast<size_t>(output_it - &(*str)[0])); str->erase(output_it - &(*str)[0]);
} }
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
......
...@@ -65,8 +65,6 @@ struct FloatTraits; ...@@ -65,8 +65,6 @@ struct FloatTraits;
template <> template <>
struct FloatTraits<double> { struct FloatTraits<double> {
using mantissa_t = uint64_t;
// The number of mantissa bits in the given float type. This includes the // The number of mantissa bits in the given float type. This includes the
// implied high bit. // implied high bit.
static constexpr int kTargetMantissaBits = 53; static constexpr int kTargetMantissaBits = 53;
...@@ -105,7 +103,7 @@ struct FloatTraits<double> { ...@@ -105,7 +103,7 @@ struct FloatTraits<double> {
// a normal value is made, or it must be less narrow than that, in which case // a normal value is made, or it must be less narrow than that, in which case
// `exponent` must be exactly kMinNormalExponent, and a subnormal value is // `exponent` must be exactly kMinNormalExponent, and a subnormal value is
// made. // made.
static double Make(mantissa_t mantissa, int exponent, bool sign) { static double Make(uint64_t mantissa, int exponent, bool sign) {
#ifndef ABSL_BIT_PACK_FLOATS #ifndef ABSL_BIT_PACK_FLOATS
// Support ldexp no matter which namespace it's in. Some platforms // Support ldexp no matter which namespace it's in. Some platforms
// incorrectly don't put it in namespace std. // incorrectly don't put it in namespace std.
...@@ -118,10 +116,8 @@ struct FloatTraits<double> { ...@@ -118,10 +116,8 @@ struct FloatTraits<double> {
if (mantissa > kMantissaMask) { if (mantissa > kMantissaMask) {
// Normal value. // Normal value.
// Adjust by 1023 for the exponent representation bias, and an additional // Adjust by 1023 for the exponent representation bias, and an additional
// 52 due to the implied decimal point in the IEEE mantissa // 52 due to the implied decimal point in the IEEE mantissa represenation.
// representation. dbl += uint64_t{exponent + 1023u + kTargetMantissaBits - 1} << 52;
dbl += static_cast<uint64_t>(exponent + 1023 + kTargetMantissaBits - 1)
<< 52;
mantissa &= kMantissaMask; mantissa &= kMantissaMask;
} else { } else {
// subnormal value // subnormal value
...@@ -138,20 +134,16 @@ struct FloatTraits<double> { ...@@ -138,20 +134,16 @@ struct FloatTraits<double> {
// members and methods. // members and methods.
template <> template <>
struct FloatTraits<float> { struct FloatTraits<float> {
using mantissa_t = uint32_t;
static constexpr int kTargetMantissaBits = 24; static constexpr int kTargetMantissaBits = 24;
static constexpr int kMaxExponent = 104; static constexpr int kMaxExponent = 104;
static constexpr int kMinNormalExponent = -149; static constexpr int kMinNormalExponent = -149;
static float MakeNan(const char* tagp) { static float MakeNan(const char* tagp) {
// Support nanf no matter which namespace it's in. Some platforms // Support nanf no matter which namespace it's in. Some platforms
// incorrectly don't put it in namespace std. // incorrectly don't put it in namespace std.
using namespace std; // NOLINT using namespace std; // NOLINT
return nanf(tagp); return nanf(tagp);
} }
static float Make(uint32_t mantissa, int exponent, bool sign) {
static float Make(mantissa_t mantissa, int exponent, bool sign) {
#ifndef ABSL_BIT_PACK_FLOATS #ifndef ABSL_BIT_PACK_FLOATS
// Support ldexpf no matter which namespace it's in. Some platforms // Support ldexpf no matter which namespace it's in. Some platforms
// incorrectly don't put it in namespace std. // incorrectly don't put it in namespace std.
...@@ -165,8 +157,7 @@ struct FloatTraits<float> { ...@@ -165,8 +157,7 @@ struct FloatTraits<float> {
// Normal value. // Normal value.
// Adjust by 127 for the exponent representation bias, and an additional // Adjust by 127 for the exponent representation bias, and an additional
// 23 due to the implied decimal point in the IEEE mantissa represenation. // 23 due to the implied decimal point in the IEEE mantissa represenation.
flt += static_cast<uint32_t>(exponent + 127 + kTargetMantissaBits - 1) flt += uint32_t{exponent + 127u + kTargetMantissaBits - 1} << 23;
<< 23;
mantissa &= kMantissaMask; mantissa &= kMantissaMask;
} else { } else {
// subnormal value // subnormal value
...@@ -251,9 +242,9 @@ struct CalculatedFloat { ...@@ -251,9 +242,9 @@ struct CalculatedFloat {
// Returns the bit width of the given uint128. (Equivalently, returns 128 // Returns the bit width of the given uint128. (Equivalently, returns 128
// minus the number of leading zero bits.) // minus the number of leading zero bits.)
int BitWidth(uint128 value) { unsigned BitWidth(uint128 value) {
if (Uint128High64(value) == 0) { if (Uint128High64(value) == 0) {
return bit_width(Uint128Low64(value)); return static_cast<unsigned>(bit_width(Uint128Low64(value)));
} }
return 128 - countl_zero(Uint128High64(value)); return 128 - countl_zero(Uint128High64(value));
} }
...@@ -346,10 +337,8 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative, ...@@ -346,10 +337,8 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative,
*value = negative ? -0.0 : 0.0; *value = negative ? -0.0 : 0.0;
return; return;
} }
*value = FloatTraits<FloatType>::Make( *value = FloatTraits<FloatType>::Make(calculated.mantissa,
static_cast<typename FloatTraits<FloatType>::mantissa_t>( calculated.exponent, negative);
calculated.mantissa),
calculated.exponent, negative);
} }
// Returns the given uint128 shifted to the right by `shift` bits, and rounds // Returns the given uint128 shifted to the right by `shift` bits, and rounds
...@@ -530,7 +519,7 @@ CalculatedFloat CalculateFromParsedHexadecimal( ...@@ -530,7 +519,7 @@ CalculatedFloat CalculateFromParsedHexadecimal(
const strings_internal::ParsedFloat& parsed_hex) { const strings_internal::ParsedFloat& parsed_hex) {
uint64_t mantissa = parsed_hex.mantissa; uint64_t mantissa = parsed_hex.mantissa;
int exponent = parsed_hex.exponent; int exponent = parsed_hex.exponent;
int mantissa_width = bit_width(mantissa); auto mantissa_width = static_cast<unsigned>(bit_width(mantissa));
const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent); const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
bool result_exact; bool result_exact;
exponent += shift; exponent += shift;
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iomanip> #include <iomanip>
#include <ios>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <ostream> #include <ostream>
...@@ -185,7 +184,7 @@ inline void Cord::InlineRep::reduce_size(size_t n) { ...@@ -185,7 +184,7 @@ inline void Cord::InlineRep::reduce_size(size_t n) {
assert(tag >= n); assert(tag >= n);
tag -= n; tag -= n;
memset(data_.as_chars() + tag, 0, n); memset(data_.as_chars() + tag, 0, n);
set_inline_size(tag); set_inline_size(static_cast<char>(tag));
} }
inline void Cord::InlineRep::remove_prefix(size_t n) { inline void Cord::InlineRep::remove_prefix(size_t n) {
...@@ -1099,7 +1098,7 @@ Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) { ...@@ -1099,7 +1098,7 @@ Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) {
: current_leaf_; : current_leaf_;
const char* data = payload->IsExternal() ? payload->external()->base const char* data = payload->IsExternal() ? payload->external()->base
: payload->flat()->Data(); : payload->flat()->Data();
const size_t offset = static_cast<size_t>(current_chunk_.data() - data); const size_t offset = current_chunk_.data() - data;
auto* tree = CordRepSubstring::Substring(payload, offset, n); auto* tree = CordRepSubstring::Substring(payload, offset, n);
subcord.contents_.EmplaceTree(VerifyTree(tree), method); subcord.contents_.EmplaceTree(VerifyTree(tree), method);
...@@ -1309,7 +1308,7 @@ static bool VerifyNode(CordRep* root, CordRep* start_node, ...@@ -1309,7 +1308,7 @@ static bool VerifyNode(CordRep* root, CordRep* start_node,
std::ostream& operator<<(std::ostream& out, const Cord& cord) { std::ostream& operator<<(std::ostream& out, const Cord& cord) {
for (absl::string_view chunk : cord.Chunks()) { for (absl::string_view chunk : cord.Chunks()) {
out.write(chunk.data(), static_cast<std::streamsize>(chunk.size())); out.write(chunk.data(), chunk.size());
} }
return out; return out;
} }
......
...@@ -411,12 +411,8 @@ class CordBuffer { ...@@ -411,12 +411,8 @@ class CordBuffer {
// Power2 functions // Power2 functions
static bool IsPow2(size_t size) { return absl::has_single_bit(size); } static bool IsPow2(size_t size) { return absl::has_single_bit(size); }
static size_t Log2Floor(size_t size) { static size_t Log2Floor(size_t size) { return absl::bit_width(size) - 1; }
return static_cast<size_t>(absl::bit_width(size) - 1); static size_t Log2Ceil(size_t size) { return absl::bit_width(size - 1); }
}
static size_t Log2Ceil(size_t size) {
return static_cast<size_t>(absl::bit_width(size - 1));
}
// Implementation of `CreateWithCustomLimit()`. // Implementation of `CreateWithCustomLimit()`.
// This implementation allows for future memory allocation hints to // This implementation allows for future memory allocation hints to
......
...@@ -190,32 +190,32 @@ char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) { ...@@ -190,32 +190,32 @@ char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) {
if (i >= 1000) goto lt10_000; if (i >= 1000) goto lt10_000;
digits = i / 100; digits = i / 100;
i -= digits * 100; i -= digits * 100;
*buffer++ = '0' + static_cast<char>(digits); *buffer++ = '0' + digits;
goto lt100; goto lt100;
} }
if (i < 1000000) { // 1,000,000 if (i < 1000000) { // 1,000,000
if (i >= 100000) goto lt1_000_000; if (i >= 100000) goto lt1_000_000;
digits = i / 10000; // 10,000 digits = i / 10000; // 10,000
i -= digits * 10000; i -= digits * 10000;
*buffer++ = '0' + static_cast<char>(digits); *buffer++ = '0' + digits;
goto lt10_000; goto lt10_000;
} }
if (i < 100000000) { // 100,000,000 if (i < 100000000) { // 100,000,000
if (i >= 10000000) goto lt100_000_000; if (i >= 10000000) goto lt100_000_000;
digits = i / 1000000; // 1,000,000 digits = i / 1000000; // 1,000,000
i -= digits * 1000000; i -= digits * 1000000;
*buffer++ = '0' + static_cast<char>(digits); *buffer++ = '0' + digits;
goto lt1_000_000; goto lt1_000_000;
} }
// we already know that i < 1,000,000,000 // we already know that i < 1,000,000,000
digits = i / 100000000; // 100,000,000 digits = i / 100000000; // 100,000,000
i -= digits * 100000000; i -= digits * 100000000;
*buffer++ = '0' + static_cast<char>(digits); *buffer++ = '0' + digits;
goto lt100_000_000; goto lt100_000_000;
} }
char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) { char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) {
uint32_t u = static_cast<uint32_t>(i); uint32_t u = i;
if (i < 0) { if (i < 0) {
*buffer++ = '-'; *buffer++ = '-';
// We need to do the negation in modular (i.e., "unsigned") // We need to do the negation in modular (i.e., "unsigned")
...@@ -268,7 +268,7 @@ char* numbers_internal::FastIntToBuffer(uint64_t i, char* buffer) { ...@@ -268,7 +268,7 @@ char* numbers_internal::FastIntToBuffer(uint64_t i, char* buffer) {
} }
char* numbers_internal::FastIntToBuffer(int64_t i, char* buffer) { char* numbers_internal::FastIntToBuffer(int64_t i, char* buffer) {
uint64_t u = static_cast<uint64_t>(i); uint64_t u = i;
if (i < 0) { if (i < 0) {
*buffer++ = '-'; *buffer++ = '-';
u = 0 - u; u = 0 - u;
...@@ -329,7 +329,7 @@ static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) { ...@@ -329,7 +329,7 @@ static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) {
result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5); result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5);
expfive -= 13; expfive -= 13;
} }
constexpr uint32_t powers_of_five[13] = { constexpr int powers_of_five[13] = {
1, 1,
5, 5,
5 * 5, 5 * 5,
...@@ -404,14 +404,14 @@ static ExpDigits SplitToSix(const double value) { ...@@ -404,14 +404,14 @@ static ExpDigits SplitToSix(const double value) {
// we multiply it by 65536 and see if the fractional part is close to 32768. // we multiply it by 65536 and see if the fractional part is close to 32768.
// (The number doesn't have to be a power of two,but powers of two are faster) // (The number doesn't have to be a power of two,but powers of two are faster)
uint64_t d64k = d * 65536; uint64_t d64k = d * 65536;
uint32_t dddddd; // A 6-digit decimal integer. int dddddd; // A 6-digit decimal integer.
if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) { if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) {
// OK, it's fairly likely that precision was lost above, which is // OK, it's fairly likely that precision was lost above, which is
// not a surprise given only 52 mantissa bits are available. Therefore // not a surprise given only 52 mantissa bits are available. Therefore
// redo the calculation using 128-bit numbers. (64 bits are not enough). // redo the calculation using 128-bit numbers. (64 bits are not enough).
// Start out with digits rounded down; maybe add one below. // Start out with digits rounded down; maybe add one below.
dddddd = static_cast<uint32_t>(d64k / 65536); dddddd = static_cast<int>(d64k / 65536);
// mantissa is a 64-bit integer representing M.mmm... * 2^63. The actual // mantissa is a 64-bit integer representing M.mmm... * 2^63. The actual
// value we're representing, of course, is M.mmm... * 2^exp2. // value we're representing, of course, is M.mmm... * 2^exp2.
...@@ -461,7 +461,7 @@ static ExpDigits SplitToSix(const double value) { ...@@ -461,7 +461,7 @@ static ExpDigits SplitToSix(const double value) {
} }
} else { } else {
// Here, we are not close to the edge. // Here, we are not close to the edge.
dddddd = static_cast<uint32_t>((d64k + 32768) / 65536); dddddd = static_cast<int>((d64k + 32768) / 65536);
} }
if (dddddd == 1000000) { if (dddddd == 1000000) {
dddddd = 100000; dddddd = 100000;
...@@ -469,7 +469,7 @@ static ExpDigits SplitToSix(const double value) { ...@@ -469,7 +469,7 @@ static ExpDigits SplitToSix(const double value) {
} }
exp_dig.exponent = exp; exp_dig.exponent = exp;
uint32_t two_digits = dddddd / 10000; int two_digits = dddddd / 10000;
dddddd -= two_digits * 10000; dddddd -= two_digits * 10000;
numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]); numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]);
...@@ -499,7 +499,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -499,7 +499,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
if (std::signbit(d)) *out++ = '-'; if (std::signbit(d)) *out++ = '-';
*out++ = '0'; *out++ = '0';
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
} }
if (d < 0) { if (d < 0) {
*out++ = '-'; *out++ = '-';
...@@ -507,7 +507,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -507,7 +507,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
} }
if (d > std::numeric_limits<double>::max()) { if (d > std::numeric_limits<double>::max()) {
strcpy(out, "inf"); // NOLINT(runtime/printf) strcpy(out, "inf"); // NOLINT(runtime/printf)
return static_cast<size_t>(out + 3 - buffer); return out + 3 - buffer;
} }
auto exp_dig = SplitToSix(d); auto exp_dig = SplitToSix(d);
...@@ -519,7 +519,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -519,7 +519,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
case 5: case 5:
memcpy(out, &digits[0], 6), out += 6; memcpy(out, &digits[0], 6), out += 6;
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
case 4: case 4:
memcpy(out, &digits[0], 5), out += 5; memcpy(out, &digits[0], 5), out += 5;
if (digits[5] != '0') { if (digits[5] != '0') {
...@@ -527,7 +527,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -527,7 +527,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
*out++ = digits[5]; *out++ = digits[5];
} }
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
case 3: case 3:
memcpy(out, &digits[0], 4), out += 4; memcpy(out, &digits[0], 4), out += 4;
if ((digits[5] | digits[4]) != '0') { if ((digits[5] | digits[4]) != '0') {
...@@ -536,7 +536,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -536,7 +536,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
if (digits[5] != '0') *out++ = digits[5]; if (digits[5] != '0') *out++ = digits[5];
} }
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
case 2: case 2:
memcpy(out, &digits[0], 3), out += 3; memcpy(out, &digits[0], 3), out += 3;
*out++ = '.'; *out++ = '.';
...@@ -545,7 +545,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -545,7 +545,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
while (out[-1] == '0') --out; while (out[-1] == '0') --out;
if (out[-1] == '.') --out; if (out[-1] == '.') --out;
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
case 1: case 1:
memcpy(out, &digits[0], 2), out += 2; memcpy(out, &digits[0], 2), out += 2;
*out++ = '.'; *out++ = '.';
...@@ -554,7 +554,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -554,7 +554,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
while (out[-1] == '0') --out; while (out[-1] == '0') --out;
if (out[-1] == '.') --out; if (out[-1] == '.') --out;
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
case 0: case 0:
memcpy(out, &digits[0], 1), out += 1; memcpy(out, &digits[0], 1), out += 1;
*out++ = '.'; *out++ = '.';
...@@ -563,7 +563,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -563,7 +563,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
while (out[-1] == '0') --out; while (out[-1] == '0') --out;
if (out[-1] == '.') --out; if (out[-1] == '.') --out;
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
case -4: case -4:
out[2] = '0'; out[2] = '0';
++out; ++out;
...@@ -582,7 +582,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -582,7 +582,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
out += 6; out += 6;
while (out[-1] == '0') --out; while (out[-1] == '0') --out;
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
} }
assert(exp < -4 || exp >= 6); assert(exp < -4 || exp >= 6);
out[0] = digits[0]; out[0] = digits[0];
...@@ -601,12 +601,12 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { ...@@ -601,12 +601,12 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
if (exp > 99) { if (exp > 99) {
int dig1 = exp / 100; int dig1 = exp / 100;
exp -= dig1 * 100; exp -= dig1 * 100;
*out++ = '0' + static_cast<char>(dig1); *out++ = '0' + dig1;
} }
PutTwoDigits(static_cast<uint32_t>(exp), out); PutTwoDigits(exp, out);
out += 2; out += 2;
*out = 0; *out = 0;
return static_cast<size_t>(out - buffer); return out - buffer;
} }
namespace { namespace {
...@@ -642,12 +642,10 @@ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/, ...@@ -642,12 +642,10 @@ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/,
int base = *base_ptr; int base = *base_ptr;
// Consume whitespace. // Consume whitespace.
while (start < end && while (start < end && absl::ascii_isspace(start[0])) {
absl::ascii_isspace(static_cast<unsigned char>(start[0]))) {
++start; ++start;
} }
while (start < end && while (start < end && absl::ascii_isspace(end[-1])) {
absl::ascii_isspace(static_cast<unsigned char>(end[-1]))) {
--end; --end;
} }
if (start >= end) { if (start >= end) {
...@@ -696,7 +694,7 @@ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/, ...@@ -696,7 +694,7 @@ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/,
} else { } else {
return false; return false;
} }
*text = absl::string_view(start, static_cast<size_t>(end - start)); *text = absl::string_view(start, end - start);
*base_ptr = base; *base_ptr = base;
return true; return true;
} }
...@@ -922,18 +920,17 @@ inline bool safe_parse_positive_int(absl::string_view text, int base, ...@@ -922,18 +920,17 @@ inline bool safe_parse_positive_int(absl::string_view text, int base,
const IntType vmax = std::numeric_limits<IntType>::max(); const IntType vmax = std::numeric_limits<IntType>::max();
assert(vmax > 0); assert(vmax > 0);
assert(base >= 0); assert(base >= 0);
const IntType base_inttype = static_cast<IntType>(base); assert(vmax >= static_cast<IntType>(base));
assert(vmax >= base_inttype);
const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base]; const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base];
assert(base < 2 || assert(base < 2 ||
std::numeric_limits<IntType>::max() / base_inttype == vmax_over_base); std::numeric_limits<IntType>::max() / base == vmax_over_base);
const char* start = text.data(); const char* start = text.data();
const char* end = start + text.size(); const char* end = start + text.size();
// loop over digits // loop over digits
for (; start < end; ++start) { for (; start < end; ++start) {
unsigned char c = static_cast<unsigned char>(start[0]); unsigned char c = static_cast<unsigned char>(start[0]);
IntType digit = static_cast<IntType>(kAsciiToInt[c]); int digit = kAsciiToInt[c];
if (digit >= base_inttype) { if (digit >= base) {
*value_p = value; *value_p = value;
return false; return false;
} }
...@@ -941,7 +938,7 @@ inline bool safe_parse_positive_int(absl::string_view text, int base, ...@@ -941,7 +938,7 @@ inline bool safe_parse_positive_int(absl::string_view text, int base,
*value_p = vmax; *value_p = vmax;
return false; return false;
} }
value *= base_inttype; value *= base;
if (value > vmax - digit) { if (value > vmax - digit) {
*value_p = vmax; *value_p = vmax;
return false; return false;
......
...@@ -56,7 +56,7 @@ AlphaNum::AlphaNum(Dec dec) { ...@@ -56,7 +56,7 @@ AlphaNum::AlphaNum(Dec dec) {
*--writer = '0' + (value % 10); *--writer = '0' + (value % 10);
value /= 10; value /= 10;
} }
*--writer = '0' + static_cast<char>(value); *--writer = '0' + value;
if (neg) *--writer = '-'; if (neg) *--writer = '-';
ptrdiff_t fillers = writer - minfill; ptrdiff_t fillers = writer - minfill;
...@@ -73,7 +73,7 @@ AlphaNum::AlphaNum(Dec dec) { ...@@ -73,7 +73,7 @@ AlphaNum::AlphaNum(Dec dec) {
if (add_sign_again) *--writer = '-'; if (add_sign_again) *--writer = '-';
} }
piece_ = absl::string_view(writer, static_cast<size_t>(end - writer)); piece_ = absl::string_view(writer, end - writer);
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
......
...@@ -32,7 +32,7 @@ void WritePadding(std::ostream& o, size_t pad) { ...@@ -32,7 +32,7 @@ void WritePadding(std::ostream& o, size_t pad) {
memset(fill_buf, o.fill(), sizeof(fill_buf)); memset(fill_buf, o.fill(), sizeof(fill_buf));
while (pad) { while (pad) {
size_t n = std::min(pad, sizeof(fill_buf)); size_t n = std::min(pad, sizeof(fill_buf));
o.write(fill_buf, static_cast<std::streamsize>(n)); o.write(fill_buf, n);
pad -= n; pad -= n;
} }
} }
...@@ -63,7 +63,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece) { ...@@ -63,7 +63,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece) {
size_t lpad = 0; size_t lpad = 0;
size_t rpad = 0; size_t rpad = 0;
if (static_cast<size_t>(o.width()) > piece.size()) { if (static_cast<size_t>(o.width()) > piece.size()) {
size_t pad = static_cast<size_t>(o.width()) - piece.size(); size_t pad = o.width() - piece.size();
if ((o.flags() & o.adjustfield) == o.left) { if ((o.flags() & o.adjustfield) == o.left) {
rpad = pad; rpad = pad;
} else { } else {
...@@ -71,7 +71,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece) { ...@@ -71,7 +71,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece) {
} }
} }
if (lpad) WritePadding(o, lpad); if (lpad) WritePadding(o, lpad);
o.write(piece.data(), static_cast<std::streamsize>(piece.size())); o.write(piece.data(), piece.size());
if (rpad) WritePadding(o, rpad); if (rpad) WritePadding(o, rpad);
o.width(0); o.width(0);
} }
...@@ -86,7 +86,7 @@ string_view::size_type string_view::find(string_view s, ...@@ -86,7 +86,7 @@ string_view::size_type string_view::find(string_view s,
} }
const char* result = const char* result =
strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_); strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
return result ? static_cast<size_type>(result - ptr_) : npos; return result ? result - ptr_ : npos;
} }
string_view::size_type string_view::find(char c, size_type pos) const noexcept { string_view::size_type string_view::find(char c, size_type pos) const noexcept {
...@@ -95,7 +95,7 @@ string_view::size_type string_view::find(char c, size_type pos) const noexcept { ...@@ -95,7 +95,7 @@ string_view::size_type string_view::find(char c, size_type pos) const noexcept {
} }
const char* result = const char* result =
static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos)); static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos));
return result != nullptr ? static_cast<size_type>(result - ptr_) : npos; return result != nullptr ? result - ptr_ : npos;
} }
string_view::size_type string_view::rfind(string_view s, string_view::size_type string_view::rfind(string_view s,
...@@ -104,7 +104,7 @@ string_view::size_type string_view::rfind(string_view s, ...@@ -104,7 +104,7 @@ string_view::size_type string_view::rfind(string_view s,
if (s.empty()) return std::min(length_, pos); if (s.empty()) return std::min(length_, pos);
const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
return result != last ? static_cast<size_type>(result - ptr_) : npos; return result != last ? result - ptr_ : npos;
} }
// Search range is [0..pos] inclusive. If pos == npos, search everything. // Search range is [0..pos] inclusive. If pos == npos, search everything.
......
...@@ -40,8 +40,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, ...@@ -40,8 +40,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format,
absl::CEscape(format).c_str()); absl::CEscape(format).c_str());
#endif #endif
return; return;
} else if (absl::ascii_isdigit( } else if (absl::ascii_isdigit(format[i + 1])) {
static_cast<unsigned char>(format[i + 1]))) {
int index = format[i + 1] - '0'; int index = format[i + 1] - '0';
if (static_cast<size_t>(index) >= num_args) { if (static_cast<size_t>(index) >= num_args) {
#ifndef NDEBUG #ifndef NDEBUG
...@@ -81,7 +80,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, ...@@ -81,7 +80,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format,
char* target = &(*output)[original_size]; char* target = &(*output)[original_size];
for (size_t i = 0; i < format.size(); i++) { for (size_t i = 0; i < format.size(); i++) {
if (format[i] == '$') { if (format[i] == '$') {
if (absl::ascii_isdigit(static_cast<unsigned char>(format[i + 1]))) { if (absl::ascii_isdigit(format[i + 1])) {
const absl::string_view src = args_array[format[i + 1] - '0']; const absl::string_view src = args_array[format[i + 1] - '0'];
target = std::copy(src.begin(), src.end(), target); target = std::copy(src.begin(), src.end(), target);
++i; // Skip next char. ++i; // Skip next char.
...@@ -111,8 +110,7 @@ Arg::Arg(const void* value) { ...@@ -111,8 +110,7 @@ Arg::Arg(const void* value) {
} while (num != 0); } while (num != 0);
*--ptr = 'x'; *--ptr = 'x';
*--ptr = '0'; *--ptr = '0';
piece_ = absl::string_view( piece_ = absl::string_view(ptr, scratch_ + sizeof(scratch_) - ptr);
ptr, static_cast<size_t>(scratch_ + sizeof(scratch_) - ptr));
} }
} }
...@@ -134,7 +132,7 @@ Arg::Arg(Hex hex) { ...@@ -134,7 +132,7 @@ Arg::Arg(Hex hex) {
beg = writer; beg = writer;
} }
piece_ = absl::string_view(beg, static_cast<size_t>(end - beg)); piece_ = absl::string_view(beg, end - beg);
} }
// TODO(jorg): Don't duplicate so much code between here and str_cat.cc // TODO(jorg): Don't duplicate so much code between here and str_cat.cc
...@@ -149,7 +147,7 @@ Arg::Arg(Dec dec) { ...@@ -149,7 +147,7 @@ Arg::Arg(Dec dec) {
*--writer = '0' + (value % 10); *--writer = '0' + (value % 10);
value /= 10; value /= 10;
} }
*--writer = '0' + static_cast<char>(value); *--writer = '0' + value;
if (neg) *--writer = '-'; if (neg) *--writer = '-';
ptrdiff_t fillers = writer - minfill; ptrdiff_t fillers = writer - minfill;
...@@ -166,7 +164,7 @@ Arg::Arg(Dec dec) { ...@@ -166,7 +164,7 @@ Arg::Arg(Dec dec) {
if (add_sign_again) *--writer = '-'; if (add_sign_again) *--writer = '-';
} }
piece_ = absl::string_view(writer, static_cast<size_t>(end - writer)); piece_ = absl::string_view(writer, end - writer);
} }
} // namespace substitute_internal } // namespace substitute_internal
......
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