diff options
author | Thomas Köppe <tkoeppe@google.com> | 2022-08-04 06:14:14 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-08-04 06:15:05 -0700 |
commit | 4b551344e6ba3243636d841d5a2d944a20a3a956 (patch) | |
tree | 95f8c347117f509f5aa9fab0b05f3c55d1ce73cf /absl | |
parent | 751ade00ee347abef5dac7248db851e3f2012e14 (diff) |
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
Diffstat (limited to 'absl')
-rw-r--r-- | absl/numeric/bits.h | 5 | ||||
-rw-r--r-- | absl/strings/ascii.cc | 10 | ||||
-rw-r--r-- | absl/strings/charconv.cc | 31 | ||||
-rw-r--r-- | absl/strings/cord.cc | 7 | ||||
-rw-r--r-- | absl/strings/cord_buffer.h | 8 | ||||
-rw-r--r-- | absl/strings/escaping.cc | 128 | ||||
-rw-r--r-- | absl/strings/numbers.cc | 65 | ||||
-rw-r--r-- | absl/strings/str_cat.cc | 4 | ||||
-rw-r--r-- | absl/strings/string_view.cc | 12 | ||||
-rw-r--r-- | absl/strings/substitute.cc | 14 |
10 files changed, 126 insertions, 158 deletions
diff --git a/absl/numeric/bits.h b/absl/numeric/bits.h index df81b9a9..628cdf50 100644 --- a/absl/numeric/bits.h +++ b/absl/numeric/bits.h @@ -131,9 +131,10 @@ has_single_bit(T x) noexcept { // fractional part discarded. template <class T> 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 { - 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 diff --git a/absl/strings/ascii.cc b/absl/strings/ascii.cc index 868df2d1..93bb03e9 100644 --- a/absl/strings/ascii.cc +++ b/absl/strings/ascii.cc @@ -157,13 +157,13 @@ ABSL_DLL const char kToUpper[256] = { void AsciiStrToLower(std::string* s) { for (auto& ch : *s) { - ch = absl::ascii_tolower(static_cast<unsigned char>(ch)); + ch = absl::ascii_tolower(ch); } } void AsciiStrToUpper(std::string* 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) { for (; input_it < input_end; ++input_it) { if (is_ws) { // 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; } 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; } - str->erase(static_cast<size_t>(output_it - &(*str)[0])); + str->erase(output_it - &(*str)[0]); } ABSL_NAMESPACE_END diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc index 65d1beb1..fefcfc90 100644 --- a/absl/strings/charconv.cc +++ b/absl/strings/charconv.cc @@ -65,8 +65,6 @@ struct FloatTraits; template <> struct FloatTraits<double> { - using mantissa_t = uint64_t; - // The number of mantissa bits in the given float type. This includes the // implied high bit. static constexpr int kTargetMantissaBits = 53; @@ -105,7 +103,7 @@ struct FloatTraits<double> { // 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 // 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 // Support ldexp no matter which namespace it's in. Some platforms // incorrectly don't put it in namespace std. @@ -118,10 +116,8 @@ struct FloatTraits<double> { if (mantissa > kMantissaMask) { // Normal value. // Adjust by 1023 for the exponent representation bias, and an additional - // 52 due to the implied decimal point in the IEEE mantissa - // representation. - dbl += static_cast<uint64_t>(exponent + 1023 + kTargetMantissaBits - 1) - << 52; + // 52 due to the implied decimal point in the IEEE mantissa represenation. + dbl += uint64_t{exponent + 1023u + kTargetMantissaBits - 1} << 52; mantissa &= kMantissaMask; } else { // subnormal value @@ -138,20 +134,16 @@ struct FloatTraits<double> { // members and methods. template <> struct FloatTraits<float> { - using mantissa_t = uint32_t; - static constexpr int kTargetMantissaBits = 24; static constexpr int kMaxExponent = 104; static constexpr int kMinNormalExponent = -149; - static float MakeNan(const char* tagp) { // Support nanf no matter which namespace it's in. Some platforms // incorrectly don't put it in namespace std. using namespace std; // NOLINT return nanf(tagp); } - - static float Make(mantissa_t mantissa, int exponent, bool sign) { + static float Make(uint32_t mantissa, int exponent, bool sign) { #ifndef ABSL_BIT_PACK_FLOATS // Support ldexpf no matter which namespace it's in. Some platforms // incorrectly don't put it in namespace std. @@ -165,8 +157,7 @@ struct FloatTraits<float> { // Normal value. // Adjust by 127 for the exponent representation bias, and an additional // 23 due to the implied decimal point in the IEEE mantissa represenation. - flt += static_cast<uint32_t>(exponent + 127 + kTargetMantissaBits - 1) - << 23; + flt += uint32_t{exponent + 127u + kTargetMantissaBits - 1} << 23; mantissa &= kMantissaMask; } else { // subnormal value @@ -251,9 +242,9 @@ struct CalculatedFloat { // Returns the bit width of the given uint128. (Equivalently, returns 128 // minus the number of leading zero bits.) -int BitWidth(uint128 value) { +unsigned BitWidth(uint128 value) { if (Uint128High64(value) == 0) { - return bit_width(Uint128Low64(value)); + return static_cast<unsigned>(bit_width(Uint128Low64(value))); } return 128 - countl_zero(Uint128High64(value)); } @@ -346,10 +337,8 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative, *value = negative ? -0.0 : 0.0; return; } - *value = FloatTraits<FloatType>::Make( - static_cast<typename FloatTraits<FloatType>::mantissa_t>( - calculated.mantissa), - calculated.exponent, negative); + *value = FloatTraits<FloatType>::Make(calculated.mantissa, + calculated.exponent, negative); } // Returns the given uint128 shifted to the right by `shift` bits, and rounds @@ -530,7 +519,7 @@ CalculatedFloat CalculateFromParsedHexadecimal( const strings_internal::ParsedFloat& parsed_hex) { uint64_t mantissa = parsed_hex.mantissa; 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); bool result_exact; exponent += shift; diff --git a/absl/strings/cord.cc b/absl/strings/cord.cc index 523379e4..b34c03a2 100644 --- a/absl/strings/cord.cc +++ b/absl/strings/cord.cc @@ -20,7 +20,6 @@ #include <cstdio> #include <cstdlib> #include <iomanip> -#include <ios> #include <iostream> #include <limits> #include <ostream> @@ -185,7 +184,7 @@ inline void Cord::InlineRep::reduce_size(size_t n) { assert(tag >= n); tag -= 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) { @@ -1099,7 +1098,7 @@ Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) { : current_leaf_; const char* data = payload->IsExternal() ? payload->external()->base : 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); subcord.contents_.EmplaceTree(VerifyTree(tree), method); @@ -1309,7 +1308,7 @@ static bool VerifyNode(CordRep* root, CordRep* start_node, std::ostream& operator<<(std::ostream& out, const Cord& cord) { 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; } diff --git a/absl/strings/cord_buffer.h b/absl/strings/cord_buffer.h index 15494b31..09a74ad5 100644 --- a/absl/strings/cord_buffer.h +++ b/absl/strings/cord_buffer.h @@ -411,12 +411,8 @@ class CordBuffer { // Power2 functions static bool IsPow2(size_t size) { return absl::has_single_bit(size); } - static size_t Log2Floor(size_t size) { - return static_cast<size_t>(absl::bit_width(size) - 1); - } - static size_t Log2Ceil(size_t size) { - return static_cast<size_t>(absl::bit_width(size - 1)); - } + static size_t Log2Floor(size_t size) { return absl::bit_width(size) - 1; } + static size_t Log2Ceil(size_t size) { return absl::bit_width(size - 1); } // Implementation of `CreateWithCustomLimit()`. // This implementation allows for future memory allocation hints to diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc index 7d97944e..4dc69702 100644 --- a/absl/strings/escaping.cc +++ b/absl/strings/escaping.cc @@ -42,11 +42,11 @@ constexpr bool kUnescapeNulls = false; inline bool is_octal_digit(char c) { return ('0' <= c) && (c <= '7'); } -inline unsigned int hex_digit_to_int(char c) { +inline int hex_digit_to_int(char c) { static_assert('0' == 0x30 && 'A' == 0x41 && 'a' == 0x61, "Character set must be ASCII."); - assert(absl::ascii_isxdigit(static_cast<unsigned char>(c))); - unsigned int x = static_cast<unsigned char>(c); + assert(absl::ascii_isxdigit(c)); + int x = static_cast<unsigned char>(c); if (x > '9') { x += 9; } @@ -121,29 +121,27 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, case '7': { // octal digit: 1 to 3 digits const char* octal_start = p; - unsigned int ch = static_cast<unsigned int>(*p - '0'); // digit 1 + unsigned int ch = *p - '0'; + if (p < last_byte && is_octal_digit(p[1])) ch = ch * 8 + *++p - '0'; if (p < last_byte && is_octal_digit(p[1])) - ch = ch * 8 + static_cast<unsigned int>(*++p - '0'); // digit 2 - if (p < last_byte && is_octal_digit(p[1])) - ch = ch * 8 + static_cast<unsigned int>(*++p - '0'); // digit 3 + ch = ch * 8 + *++p - '0'; // now points at last digit if (ch > 0xff) { if (error) { *error = "Value of \\" + - std::string(octal_start, - static_cast<size_t>(p + 1 - octal_start)) + + std::string(octal_start, p + 1 - octal_start) + " exceeds 0xff"; } return false; } if ((ch == 0) && leave_nulls_escaped) { // Copy the escape sequence for the null character - const size_t octal_size = static_cast<size_t>(p + 1 - octal_start); + const ptrdiff_t octal_size = p + 1 - octal_start; *d++ = '\\'; memmove(d, octal_start, octal_size); d += octal_size; break; } - *d++ = static_cast<char>(ch); + *d++ = ch; break; } case 'x': @@ -151,34 +149,32 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, if (p >= last_byte) { if (error) *error = "String cannot end with \\x"; return false; - } else if (!absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) { + } else if (!absl::ascii_isxdigit(p[1])) { if (error) *error = "\\x cannot be followed by a non-hex digit"; return false; } unsigned int ch = 0; const char* hex_start = p; - while (p < last_byte && - absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) + while (p < last_byte && absl::ascii_isxdigit(p[1])) // Arbitrarily many hex digits ch = (ch << 4) + hex_digit_to_int(*++p); if (ch > 0xFF) { if (error) { *error = "Value of \\" + - std::string(hex_start, - static_cast<size_t>(p + 1 - hex_start)) + + std::string(hex_start, p + 1 - hex_start) + " exceeds 0xff"; } return false; } if ((ch == 0) && leave_nulls_escaped) { // Copy the escape sequence for the null character - const size_t hex_size = static_cast<size_t>(p + 1 - hex_start); + const ptrdiff_t hex_size = p + 1 - hex_start; *d++ = '\\'; memmove(d, hex_start, hex_size); d += hex_size; break; } - *d++ = static_cast<char>(ch); + *d++ = ch; break; } case 'u': { @@ -188,20 +184,18 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, if (p + 4 >= end) { if (error) { *error = "\\u must be followed by 4 hex digits: \\" + - std::string(hex_start, - static_cast<size_t>(p + 1 - hex_start)); + std::string(hex_start, p + 1 - hex_start); } return false; } for (int i = 0; i < 4; ++i) { // Look one char ahead. - if (absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) { + if (absl::ascii_isxdigit(p[1])) { rune = (rune << 4) + hex_digit_to_int(*++p); // Advance p. } else { if (error) { *error = "\\u must be followed by 4 hex digits: \\" + - std::string(hex_start, - static_cast<size_t>(p + 1 - hex_start)); + std::string(hex_start, p + 1 - hex_start); } return false; } @@ -226,22 +220,20 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, if (p + 8 >= end) { if (error) { *error = "\\U must be followed by 8 hex digits: \\" + - std::string(hex_start, - static_cast<size_t>(p + 1 - hex_start)); + std::string(hex_start, p + 1 - hex_start); } return false; } for (int i = 0; i < 8; ++i) { // Look one char ahead. - if (absl::ascii_isxdigit(static_cast<unsigned char>(p[1]))) { + if (absl::ascii_isxdigit(p[1])) { // Don't change rune until we're sure this // is within the Unicode limit, but do advance p. uint32_t newrune = (rune << 4) + hex_digit_to_int(*++p); if (newrune > 0x10FFFF) { if (error) { *error = "Value of \\" + - std::string(hex_start, - static_cast<size_t>(p + 1 - hex_start)) + + std::string(hex_start, p + 1 - hex_start) + " exceeds Unicode limit (0x10FFFF)"; } return false; @@ -251,8 +243,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, } else { if (error) { *error = "\\U must be followed by 8 hex digits: \\" + - std::string(hex_start, - static_cast<size_t>(p + 1 - hex_start)); + std::string(hex_start, p + 1 - hex_start); } return false; } @@ -300,7 +291,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, error)) { return false; } - dest->erase(static_cast<size_t>(dest_size)); + dest->erase(dest_size); return true; } @@ -320,7 +311,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, std::string dest; bool last_hex_escape = false; // true if last output char was \xNN. - for (char c : src) { + for (unsigned char c : src) { bool is_hex_escape = false; switch (c) { case '\n': dest.append("\\" "n"); break; @@ -329,30 +320,28 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, case '\"': dest.append("\\" "\""); break; case '\'': dest.append("\\" "'"); break; case '\\': dest.append("\\" "\\"); break; - default: { + default: // Note that if we emit \xNN and the src character after that is a hex // digit then that digit must be escaped too to prevent it being // interpreted as part of the character code by C. - const unsigned char uc = static_cast<unsigned char>(c); - if ((!utf8_safe || uc < 0x80) && - (!absl::ascii_isprint(uc) || - (last_hex_escape && absl::ascii_isxdigit(uc)))) { + if ((!utf8_safe || c < 0x80) && + (!absl::ascii_isprint(c) || + (last_hex_escape && absl::ascii_isxdigit(c)))) { if (use_hex) { dest.append("\\" "x"); - dest.push_back(numbers_internal::kHexChar[uc / 16]); - dest.push_back(numbers_internal::kHexChar[uc % 16]); + dest.push_back(numbers_internal::kHexChar[c / 16]); + dest.push_back(numbers_internal::kHexChar[c % 16]); is_hex_escape = true; } else { dest.append("\\"); - dest.push_back(numbers_internal::kHexChar[uc / 64]); - dest.push_back(numbers_internal::kHexChar[(uc % 64) / 8]); - dest.push_back(numbers_internal::kHexChar[uc % 8]); + dest.push_back(numbers_internal::kHexChar[c / 64]); + dest.push_back(numbers_internal::kHexChar[(c % 64) / 8]); + dest.push_back(numbers_internal::kHexChar[c % 8]); } } else { dest.push_back(c); break; } - } } last_hex_escape = is_hex_escape; } @@ -361,7 +350,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, } /* clang-format off */ -constexpr unsigned char c_escaped_len[256] = { +constexpr char c_escaped_len[256] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", ' @@ -386,8 +375,7 @@ constexpr unsigned char c_escaped_len[256] = { // that UTF-8 bytes are not handled specially. inline size_t CEscapedLength(absl::string_view src) { size_t escaped_len = 0; - for (char c : src) - escaped_len += c_escaped_len[static_cast<unsigned char>(c)]; + for (unsigned char c : src) escaped_len += c_escaped_len[c]; return escaped_len; } @@ -403,8 +391,8 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) { cur_dest_len + escaped_len); char* append_ptr = &(*dest)[cur_dest_len]; - for (char c : src) { - size_t char_len = c_escaped_len[static_cast<unsigned char>(c)]; + for (unsigned char c : src) { + int char_len = c_escaped_len[c]; if (char_len == 1) { *append_ptr++ = c; } else if (char_len == 2) { @@ -436,9 +424,9 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) { } } else { *append_ptr++ = '\\'; - *append_ptr++ = '0' + static_cast<unsigned char>(c) / 64; - *append_ptr++ = '0' + (static_cast<unsigned char>(c) % 64) / 8; - *append_ptr++ = '0' + static_cast<unsigned char>(c) % 8; + *append_ptr++ = '0' + c / 64; + *append_ptr++ = '0' + (c % 64) / 8; + *append_ptr++ = '0' + c % 8; } } } @@ -452,7 +440,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, size_t destidx = 0; int decode = 0; int state = 0; - unsigned char ch = 0; + unsigned int ch = 0; unsigned int temp = 0; // If "char" is signed by default, using *src as an array index results in @@ -512,13 +500,13 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, // how to handle those cases. GET_INPUT(first, 4); - temp = static_cast<unsigned char>(decode); + temp = decode; GET_INPUT(second, 3); - temp = (temp << 6) | static_cast<unsigned char>(decode); + temp = (temp << 6) | decode; GET_INPUT(third, 2); - temp = (temp << 6) | static_cast<unsigned char>(decode); + temp = (temp << 6) | decode; GET_INPUT(fourth, 1); - temp = (temp << 6) | static_cast<unsigned char>(decode); + temp = (temp << 6) | decode; } else { // We really did have four good data bytes, so advance four // characters in the string. @@ -530,11 +518,11 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, // temp has 24 bits of input, so write that out as three bytes. if (destidx + 3 > szdest) return false; - dest[destidx + 2] = static_cast<char>(temp); + dest[destidx + 2] = temp; temp >>= 8; - dest[destidx + 1] = static_cast<char>(temp); + dest[destidx + 1] = temp; temp >>= 8; - dest[destidx] = static_cast<char>(temp); + dest[destidx] = temp; destidx += 3; } } else { @@ -595,18 +583,18 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, } // Each input character gives us six bits of output. - temp = (temp << 6) | static_cast<unsigned char>(decode); + temp = (temp << 6) | decode; ++state; if (state == 4) { // If we've accumulated 24 bits of output, write that out as // three bytes. if (dest) { if (destidx + 3 > szdest) return false; - dest[destidx + 2] = static_cast<char>(temp); + dest[destidx + 2] = temp; temp >>= 8; - dest[destidx + 1] = static_cast<char>(temp); + dest[destidx + 1] = temp; temp >>= 8; - dest[destidx] = static_cast<char>(temp); + dest[destidx] = temp; } destidx += 3; state = 0; @@ -631,7 +619,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, if (dest) { if (destidx + 1 > szdest) return false; temp >>= 4; - dest[destidx] = static_cast<char>(temp); + dest[destidx] = temp; } ++destidx; expected_equals = 2; @@ -642,9 +630,9 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, if (dest) { if (destidx + 2 > szdest) return false; temp >>= 2; - dest[destidx + 1] = static_cast<char>(temp); + dest[destidx + 1] = temp; temp >>= 8; - dest[destidx] = static_cast<char>(temp); + dest[destidx] = temp; } destidx += 2; expected_equals = 1; @@ -834,9 +822,9 @@ constexpr char kHexValueLenient[256] = { // or a string. This works because we use the [] operator to access // individual characters at a time. template <typename T> -void HexStringToBytesInternal(const char* from, T to, size_t num) { - for (size_t i = 0; i < num; i++) { - to[i] = static_cast<char>(kHexValueLenient[from[i * 2] & 0xFF] << 4) + +void HexStringToBytesInternal(const char* from, T to, ptrdiff_t num) { + for (int i = 0; i < num; i++) { + to[i] = (kHexValueLenient[from[i * 2] & 0xFF] << 4) + (kHexValueLenient[from[i * 2 + 1] & 0xFF]); } } @@ -844,7 +832,7 @@ void HexStringToBytesInternal(const char* from, T to, size_t num) { // This is a templated function so that T can be either a char* or a // std::string. template <typename T> -void BytesToHexStringInternal(const unsigned char* src, T dest, size_t num) { +void BytesToHexStringInternal(const unsigned char* src, T dest, ptrdiff_t num) { auto dest_ptr = &dest[0]; for (auto src_ptr = src; src_ptr != (src + num); ++src_ptr, dest_ptr += 2) { const char* hex_p = &numbers_internal::kHexTable[*src_ptr * 2]; diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc index 2987158e..e798fc69 100644 --- a/absl/strings/numbers.cc +++ b/absl/strings/numbers.cc @@ -190,32 +190,32 @@ char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) { if (i >= 1000) goto lt10_000; digits = i / 100; i -= digits * 100; - *buffer++ = '0' + static_cast<char>(digits); + *buffer++ = '0' + digits; goto lt100; } if (i < 1000000) { // 1,000,000 if (i >= 100000) goto lt1_000_000; digits = i / 10000; // 10,000 i -= digits * 10000; - *buffer++ = '0' + static_cast<char>(digits); + *buffer++ = '0' + digits; goto lt10_000; } if (i < 100000000) { // 100,000,000 if (i >= 10000000) goto lt100_000_000; digits = i / 1000000; // 1,000,000 i -= digits * 1000000; - *buffer++ = '0' + static_cast<char>(digits); + *buffer++ = '0' + digits; goto lt1_000_000; } // we already know that i < 1,000,000,000 digits = i / 100000000; // 100,000,000 i -= digits * 100000000; - *buffer++ = '0' + static_cast<char>(digits); + *buffer++ = '0' + digits; goto lt100_000_000; } char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) { - uint32_t u = static_cast<uint32_t>(i); + uint32_t u = i; if (i < 0) { *buffer++ = '-'; // We need to do the negation in modular (i.e., "unsigned") @@ -268,7 +268,7 @@ char* numbers_internal::FastIntToBuffer(uint64_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) { *buffer++ = '-'; u = 0 - u; @@ -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); expfive -= 13; } - constexpr uint32_t powers_of_five[13] = { + constexpr int powers_of_five[13] = { 1, 5, 5 * 5, @@ -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. // (The number doesn't have to be a power of two,but powers of two are faster) 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) { // OK, it's fairly likely that precision was lost above, which is // not a surprise given only 52 mantissa bits are available. Therefore // redo the calculation using 128-bit numbers. (64 bits are not enough). // 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 // value we're representing, of course, is M.mmm... * 2^exp2. @@ -461,7 +461,7 @@ static ExpDigits SplitToSix(const double value) { } } else { // 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) { dddddd = 100000; @@ -469,7 +469,7 @@ static ExpDigits SplitToSix(const double value) { } exp_dig.exponent = exp; - uint32_t two_digits = dddddd / 10000; + int two_digits = dddddd / 10000; dddddd -= two_digits * 10000; numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]); @@ -499,7 +499,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { if (std::signbit(d)) *out++ = '-'; *out++ = '0'; *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; } if (d < 0) { *out++ = '-'; @@ -507,7 +507,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { } if (d > std::numeric_limits<double>::max()) { strcpy(out, "inf"); // NOLINT(runtime/printf) - return static_cast<size_t>(out + 3 - buffer); + return out + 3 - buffer; } auto exp_dig = SplitToSix(d); @@ -519,7 +519,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { case 5: memcpy(out, &digits[0], 6), out += 6; *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; case 4: memcpy(out, &digits[0], 5), out += 5; if (digits[5] != '0') { @@ -527,7 +527,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { *out++ = digits[5]; } *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; case 3: memcpy(out, &digits[0], 4), out += 4; if ((digits[5] | digits[4]) != '0') { @@ -536,7 +536,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { if (digits[5] != '0') *out++ = digits[5]; } *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; case 2: memcpy(out, &digits[0], 3), out += 3; *out++ = '.'; @@ -545,7 +545,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { while (out[-1] == '0') --out; if (out[-1] == '.') --out; *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; case 1: memcpy(out, &digits[0], 2), out += 2; *out++ = '.'; @@ -554,7 +554,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { while (out[-1] == '0') --out; if (out[-1] == '.') --out; *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; case 0: memcpy(out, &digits[0], 1), out += 1; *out++ = '.'; @@ -563,7 +563,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { while (out[-1] == '0') --out; if (out[-1] == '.') --out; *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; case -4: out[2] = '0'; ++out; @@ -582,7 +582,7 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { out += 6; while (out[-1] == '0') --out; *out = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; } assert(exp < -4 || exp >= 6); out[0] = digits[0]; @@ -601,12 +601,12 @@ size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) { if (exp > 99) { int dig1 = exp / 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 = 0; - return static_cast<size_t>(out - buffer); + return out - buffer; } namespace { @@ -642,12 +642,10 @@ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/, int base = *base_ptr; // Consume whitespace. - while (start < end && - absl::ascii_isspace(static_cast<unsigned char>(start[0]))) { + while (start < end && absl::ascii_isspace(start[0])) { ++start; } - while (start < end && - absl::ascii_isspace(static_cast<unsigned char>(end[-1]))) { + while (start < end && absl::ascii_isspace(end[-1])) { --end; } if (start >= end) { @@ -696,7 +694,7 @@ inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/, } else { return false; } - *text = absl::string_view(start, static_cast<size_t>(end - start)); + *text = absl::string_view(start, end - start); *base_ptr = base; return true; } @@ -922,18 +920,17 @@ inline bool safe_parse_positive_int(absl::string_view text, int base, const IntType vmax = std::numeric_limits<IntType>::max(); assert(vmax > 0); assert(base >= 0); - const IntType base_inttype = static_cast<IntType>(base); - assert(vmax >= base_inttype); + assert(vmax >= static_cast<IntType>(base)); const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base]; 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* end = start + text.size(); // loop over digits for (; start < end; ++start) { unsigned char c = static_cast<unsigned char>(start[0]); - IntType digit = static_cast<IntType>(kAsciiToInt[c]); - if (digit >= base_inttype) { + int digit = kAsciiToInt[c]; + if (digit >= base) { *value_p = value; return false; } @@ -941,7 +938,7 @@ inline bool safe_parse_positive_int(absl::string_view text, int base, *value_p = vmax; return false; } - value *= base_inttype; + value *= base; if (value > vmax - digit) { *value_p = vmax; return false; diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc index 2f9df673..f4a77493 100644 --- a/absl/strings/str_cat.cc +++ b/absl/strings/str_cat.cc @@ -56,7 +56,7 @@ AlphaNum::AlphaNum(Dec dec) { *--writer = '0' + (value % 10); value /= 10; } - *--writer = '0' + static_cast<char>(value); + *--writer = '0' + value; if (neg) *--writer = '-'; ptrdiff_t fillers = writer - minfill; @@ -73,7 +73,7 @@ AlphaNum::AlphaNum(Dec dec) { if (add_sign_again) *--writer = '-'; } - piece_ = absl::string_view(writer, static_cast<size_t>(end - writer)); + piece_ = absl::string_view(writer, end - writer); } // ---------------------------------------------------------------------- diff --git a/absl/strings/string_view.cc b/absl/strings/string_view.cc index e2261625..adce3be9 100644 --- a/absl/strings/string_view.cc +++ b/absl/strings/string_view.cc @@ -32,7 +32,7 @@ void WritePadding(std::ostream& o, size_t pad) { memset(fill_buf, o.fill(), sizeof(fill_buf)); while (pad) { 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; } } @@ -63,7 +63,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece) { size_t lpad = 0; size_t rpad = 0; 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) { rpad = pad; } else { @@ -71,7 +71,7 @@ std::ostream& operator<<(std::ostream& o, string_view piece) { } } 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); o.width(0); } @@ -86,7 +86,7 @@ string_view::size_type string_view::find(string_view s, } const char* result = 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 { @@ -95,7 +95,7 @@ string_view::size_type string_view::find(char c, size_type pos) const noexcept { } const char* result = 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, @@ -104,7 +104,7 @@ string_view::size_type string_view::rfind(string_view s, if (s.empty()) return std::min(length_, pos); 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_); - 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. diff --git a/absl/strings/substitute.cc b/absl/strings/substitute.cc index 33a39305..8980b198 100644 --- a/absl/strings/substitute.cc +++ b/absl/strings/substitute.cc @@ -40,8 +40,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, absl::CEscape(format).c_str()); #endif return; - } else if (absl::ascii_isdigit( - static_cast<unsigned char>(format[i + 1]))) { + } else if (absl::ascii_isdigit(format[i + 1])) { int index = format[i + 1] - '0'; if (static_cast<size_t>(index) >= num_args) { #ifndef NDEBUG @@ -81,7 +80,7 @@ void SubstituteAndAppendArray(std::string* output, absl::string_view format, char* target = &(*output)[original_size]; for (size_t i = 0; i < format.size(); 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']; target = std::copy(src.begin(), src.end(), target); ++i; // Skip next char. @@ -111,8 +110,7 @@ Arg::Arg(const void* value) { } while (num != 0); *--ptr = 'x'; *--ptr = '0'; - piece_ = absl::string_view( - ptr, static_cast<size_t>(scratch_ + sizeof(scratch_) - ptr)); + piece_ = absl::string_view(ptr, scratch_ + sizeof(scratch_) - ptr); } } @@ -134,7 +132,7 @@ Arg::Arg(Hex hex) { 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 @@ -149,7 +147,7 @@ Arg::Arg(Dec dec) { *--writer = '0' + (value % 10); value /= 10; } - *--writer = '0' + static_cast<char>(value); + *--writer = '0' + value; if (neg) *--writer = '-'; ptrdiff_t fillers = writer - minfill; @@ -166,7 +164,7 @@ Arg::Arg(Dec dec) { 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 |