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
......
...@@ -42,11 +42,11 @@ constexpr bool kUnescapeNulls = false; ...@@ -42,11 +42,11 @@ constexpr bool kUnescapeNulls = false;
inline bool is_octal_digit(char c) { return ('0' <= c) && (c <= '7'); } 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, static_assert('0' == 0x30 && 'A' == 0x41 && 'a' == 0x61,
"Character set must be ASCII."); "Character set must be ASCII.");
assert(absl::ascii_isxdigit(static_cast<unsigned char>(c))); assert(absl::ascii_isxdigit(c));
unsigned int x = static_cast<unsigned char>(c); int x = static_cast<unsigned char>(c);
if (x > '9') { if (x > '9') {
x += 9; x += 9;
} }
...@@ -121,29 +121,27 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -121,29 +121,27 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
case '7': { case '7': {
// octal digit: 1 to 3 digits // octal digit: 1 to 3 digits
const char* octal_start = p; 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])) if (p < last_byte && is_octal_digit(p[1]))
ch = ch * 8 + static_cast<unsigned int>(*++p - '0'); // digit 2 ch = ch * 8 + *++p - '0'; // now points at last digit
if (p < last_byte && is_octal_digit(p[1]))
ch = ch * 8 + static_cast<unsigned int>(*++p - '0'); // digit 3
if (ch > 0xff) { if (ch > 0xff) {
if (error) { if (error) {
*error = "Value of \\" + *error = "Value of \\" +
std::string(octal_start, std::string(octal_start, p + 1 - octal_start) +
static_cast<size_t>(p + 1 - octal_start)) +
" exceeds 0xff"; " exceeds 0xff";
} }
return false; return false;
} }
if ((ch == 0) && leave_nulls_escaped) { if ((ch == 0) && leave_nulls_escaped) {
// Copy the escape sequence for the null character // 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++ = '\\'; *d++ = '\\';
memmove(d, octal_start, octal_size); memmove(d, octal_start, octal_size);
d += octal_size; d += octal_size;
break; break;
} }
*d++ = static_cast<char>(ch); *d++ = ch;
break; break;
} }
case 'x': case 'x':
...@@ -151,34 +149,32 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -151,34 +149,32 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
if (p >= last_byte) { if (p >= last_byte) {
if (error) *error = "String cannot end with \\x"; if (error) *error = "String cannot end with \\x";
return false; 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"; if (error) *error = "\\x cannot be followed by a non-hex digit";
return false; return false;
} }
unsigned int ch = 0; unsigned int ch = 0;
const char* hex_start = p; const char* hex_start = p;
while (p < last_byte && while (p < last_byte && absl::ascii_isxdigit(p[1]))
absl::ascii_isxdigit(static_cast<unsigned char>(p[1])))
// Arbitrarily many hex digits // Arbitrarily many hex digits
ch = (ch << 4) + hex_digit_to_int(*++p); ch = (ch << 4) + hex_digit_to_int(*++p);
if (ch > 0xFF) { if (ch > 0xFF) {
if (error) { if (error) {
*error = "Value of \\" + *error = "Value of \\" +
std::string(hex_start, std::string(hex_start, p + 1 - hex_start) +
static_cast<size_t>(p + 1 - hex_start)) +
" exceeds 0xff"; " exceeds 0xff";
} }
return false; return false;
} }
if ((ch == 0) && leave_nulls_escaped) { if ((ch == 0) && leave_nulls_escaped) {
// Copy the escape sequence for the null character // 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++ = '\\'; *d++ = '\\';
memmove(d, hex_start, hex_size); memmove(d, hex_start, hex_size);
d += hex_size; d += hex_size;
break; break;
} }
*d++ = static_cast<char>(ch); *d++ = ch;
break; break;
} }
case 'u': { case 'u': {
...@@ -188,20 +184,18 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -188,20 +184,18 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
if (p + 4 >= end) { if (p + 4 >= end) {
if (error) { if (error) {
*error = "\\u must be followed by 4 hex digits: \\" + *error = "\\u must be followed by 4 hex digits: \\" +
std::string(hex_start, std::string(hex_start, p + 1 - hex_start);
static_cast<size_t>(p + 1 - hex_start));
} }
return false; return false;
} }
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {
// Look one char ahead. // 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. rune = (rune << 4) + hex_digit_to_int(*++p); // Advance p.
} else { } else {
if (error) { if (error) {
*error = "\\u must be followed by 4 hex digits: \\" + *error = "\\u must be followed by 4 hex digits: \\" +
std::string(hex_start, std::string(hex_start, p + 1 - hex_start);
static_cast<size_t>(p + 1 - hex_start));
} }
return false; return false;
} }
...@@ -226,22 +220,20 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -226,22 +220,20 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
if (p + 8 >= end) { if (p + 8 >= end) {
if (error) { if (error) {
*error = "\\U must be followed by 8 hex digits: \\" + *error = "\\U must be followed by 8 hex digits: \\" +
std::string(hex_start, std::string(hex_start, p + 1 - hex_start);
static_cast<size_t>(p + 1 - hex_start));
} }
return false; return false;
} }
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
// Look one char ahead. // 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 // Don't change rune until we're sure this
// is within the Unicode limit, but do advance p. // is within the Unicode limit, but do advance p.
uint32_t newrune = (rune << 4) + hex_digit_to_int(*++p); uint32_t newrune = (rune << 4) + hex_digit_to_int(*++p);
if (newrune > 0x10FFFF) { if (newrune > 0x10FFFF) {
if (error) { if (error) {
*error = "Value of \\" + *error = "Value of \\" +
std::string(hex_start, std::string(hex_start, p + 1 - hex_start) +
static_cast<size_t>(p + 1 - hex_start)) +
" exceeds Unicode limit (0x10FFFF)"; " exceeds Unicode limit (0x10FFFF)";
} }
return false; return false;
...@@ -251,8 +243,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -251,8 +243,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
} else { } else {
if (error) { if (error) {
*error = "\\U must be followed by 8 hex digits: \\" + *error = "\\U must be followed by 8 hex digits: \\" +
std::string(hex_start, std::string(hex_start, p + 1 - hex_start);
static_cast<size_t>(p + 1 - hex_start));
} }
return false; return false;
} }
...@@ -300,7 +291,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped, ...@@ -300,7 +291,7 @@ bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
error)) { error)) {
return false; return false;
} }
dest->erase(static_cast<size_t>(dest_size)); dest->erase(dest_size);
return true; return true;
} }
...@@ -320,7 +311,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, ...@@ -320,7 +311,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex,
std::string dest; std::string dest;
bool last_hex_escape = false; // true if last output char was \xNN. 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; bool is_hex_escape = false;
switch (c) { switch (c) {
case '\n': dest.append("\\" "n"); break; case '\n': dest.append("\\" "n"); break;
...@@ -329,30 +320,28 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, ...@@ -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; 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 // 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 // digit then that digit must be escaped too to prevent it being
// interpreted as part of the character code by C. // interpreted as part of the character code by C.
const unsigned char uc = static_cast<unsigned char>(c); if ((!utf8_safe || c < 0x80) &&
if ((!utf8_safe || uc < 0x80) && (!absl::ascii_isprint(c) ||
(!absl::ascii_isprint(uc) || (last_hex_escape && absl::ascii_isxdigit(c)))) {
(last_hex_escape && absl::ascii_isxdigit(uc)))) {
if (use_hex) { if (use_hex) {
dest.append("\\" "x"); dest.append("\\" "x");
dest.push_back(numbers_internal::kHexChar[uc / 16]); dest.push_back(numbers_internal::kHexChar[c / 16]);
dest.push_back(numbers_internal::kHexChar[uc % 16]); dest.push_back(numbers_internal::kHexChar[c % 16]);
is_hex_escape = true; is_hex_escape = true;
} else { } else {
dest.append("\\"); dest.append("\\");
dest.push_back(numbers_internal::kHexChar[uc / 64]); dest.push_back(numbers_internal::kHexChar[c / 64]);
dest.push_back(numbers_internal::kHexChar[(uc % 64) / 8]); dest.push_back(numbers_internal::kHexChar[(c % 64) / 8]);
dest.push_back(numbers_internal::kHexChar[uc % 8]); dest.push_back(numbers_internal::kHexChar[c % 8]);
} }
} else { } else {
dest.push_back(c); dest.push_back(c);
break; break;
} }
}
} }
last_hex_escape = is_hex_escape; last_hex_escape = is_hex_escape;
} }
...@@ -361,7 +350,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, ...@@ -361,7 +350,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex,
} }
/* clang-format off */ /* 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, 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, 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, // ", ' 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] = { ...@@ -386,8 +375,7 @@ constexpr unsigned char c_escaped_len[256] = {
// that UTF-8 bytes are not handled specially. // that UTF-8 bytes are not handled specially.
inline size_t CEscapedLength(absl::string_view src) { inline size_t CEscapedLength(absl::string_view src) {
size_t escaped_len = 0; size_t escaped_len = 0;
for (char c : src) for (unsigned char c : src) escaped_len += c_escaped_len[c];
escaped_len += c_escaped_len[static_cast<unsigned char>(c)];
return escaped_len; return escaped_len;
} }
...@@ -403,8 +391,8 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) { ...@@ -403,8 +391,8 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) {
cur_dest_len + escaped_len); cur_dest_len + escaped_len);
char* append_ptr = &(*dest)[cur_dest_len]; char* append_ptr = &(*dest)[cur_dest_len];
for (char c : src) { for (unsigned char c : src) {
size_t char_len = c_escaped_len[static_cast<unsigned char>(c)]; int char_len = c_escaped_len[c];
if (char_len == 1) { if (char_len == 1) {
*append_ptr++ = c; *append_ptr++ = c;
} else if (char_len == 2) { } else if (char_len == 2) {
...@@ -436,9 +424,9 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) { ...@@ -436,9 +424,9 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) {
} }
} else { } else {
*append_ptr++ = '\\'; *append_ptr++ = '\\';
*append_ptr++ = '0' + static_cast<unsigned char>(c) / 64; *append_ptr++ = '0' + c / 64;
*append_ptr++ = '0' + (static_cast<unsigned char>(c) % 64) / 8; *append_ptr++ = '0' + (c % 64) / 8;
*append_ptr++ = '0' + static_cast<unsigned char>(c) % 8; *append_ptr++ = '0' + c % 8;
} }
} }
} }
...@@ -452,7 +440,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, ...@@ -452,7 +440,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest,
size_t destidx = 0; size_t destidx = 0;
int decode = 0; int decode = 0;
int state = 0; int state = 0;
unsigned char ch = 0; unsigned int ch = 0;
unsigned int temp = 0; unsigned int temp = 0;
// If "char" is signed by default, using *src as an array index results in // 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, ...@@ -512,13 +500,13 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest,
// how to handle those cases. // how to handle those cases.
GET_INPUT(first, 4); GET_INPUT(first, 4);
temp = static_cast<unsigned char>(decode); temp = decode;
GET_INPUT(second, 3); GET_INPUT(second, 3);
temp = (temp << 6) | static_cast<unsigned char>(decode); temp = (temp << 6) | decode;
GET_INPUT(third, 2); GET_INPUT(third, 2);
temp = (temp << 6) | static_cast<unsigned char>(decode); temp = (temp << 6) | decode;
GET_INPUT(fourth, 1); GET_INPUT(fourth, 1);
temp = (temp << 6) | static_cast<unsigned char>(decode); temp = (temp << 6) | decode;
} else { } else {
// We really did have four good data bytes, so advance four // We really did have four good data bytes, so advance four
// characters in the string. // characters in the string.
...@@ -530,11 +518,11 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, ...@@ -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. // temp has 24 bits of input, so write that out as three bytes.
if (destidx + 3 > szdest) return false; if (destidx + 3 > szdest) return false;
dest[destidx + 2] = static_cast<char>(temp); dest[destidx + 2] = temp;
temp >>= 8; temp >>= 8;
dest[destidx + 1] = static_cast<char>(temp); dest[destidx + 1] = temp;
temp >>= 8; temp >>= 8;
dest[destidx] = static_cast<char>(temp); dest[destidx] = temp;
destidx += 3; destidx += 3;
} }
} else { } else {
...@@ -595,18 +583,18 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, ...@@ -595,18 +583,18 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest,
} }
// Each input character gives us six bits of output. // Each input character gives us six bits of output.
temp = (temp << 6) | static_cast<unsigned char>(decode); temp = (temp << 6) | decode;
++state; ++state;
if (state == 4) { if (state == 4) {
// If we've accumulated 24 bits of output, write that out as // If we've accumulated 24 bits of output, write that out as
// three bytes. // three bytes.
if (dest) { if (dest) {
if (destidx + 3 > szdest) return false; if (destidx + 3 > szdest) return false;
dest[destidx + 2] = static_cast<char>(temp); dest[destidx + 2] = temp;
temp >>= 8; temp >>= 8;
dest[destidx + 1] = static_cast<char>(temp); dest[destidx + 1] = temp;
temp >>= 8; temp >>= 8;
dest[destidx] = static_cast<char>(temp); dest[destidx] = temp;
} }
destidx += 3; destidx += 3;
state = 0; state = 0;
...@@ -631,7 +619,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, ...@@ -631,7 +619,7 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest,
if (dest) { if (dest) {
if (destidx + 1 > szdest) return false; if (destidx + 1 > szdest) return false;
temp >>= 4; temp >>= 4;
dest[destidx] = static_cast<char>(temp); dest[destidx] = temp;
} }
++destidx; ++destidx;
expected_equals = 2; expected_equals = 2;
...@@ -642,9 +630,9 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest, ...@@ -642,9 +630,9 @@ bool Base64UnescapeInternal(const char* src_param, size_t szsrc, char* dest,
if (dest) { if (dest) {
if (destidx + 2 > szdest) return false; if (destidx + 2 > szdest) return false;
temp >>= 2; temp >>= 2;
dest[destidx + 1] = static_cast<char>(temp); dest[destidx + 1] = temp;
temp >>= 8; temp >>= 8;
dest[destidx] = static_cast<char>(temp); dest[destidx] = temp;
} }
destidx += 2; destidx += 2;
expected_equals = 1; expected_equals = 1;
...@@ -834,9 +822,9 @@ constexpr char kHexValueLenient[256] = { ...@@ -834,9 +822,9 @@ constexpr char kHexValueLenient[256] = {
// or a string. This works because we use the [] operator to access // or a string. This works because we use the [] operator to access
// individual characters at a time. // individual characters at a time.
template <typename T> template <typename T>
void HexStringToBytesInternal(const char* from, T to, size_t num) { void HexStringToBytesInternal(const char* from, T to, ptrdiff_t num) {
for (size_t i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
to[i] = static_cast<char>(kHexValueLenient[from[i * 2] & 0xFF] << 4) + to[i] = (kHexValueLenient[from[i * 2] & 0xFF] << 4) +
(kHexValueLenient[from[i * 2 + 1] & 0xFF]); (kHexValueLenient[from[i * 2 + 1] & 0xFF]);
} }
} }
...@@ -844,7 +832,7 @@ void HexStringToBytesInternal(const char* from, T to, size_t num) { ...@@ -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 // This is a templated function so that T can be either a char* or a
// std::string. // std::string.
template <typename T> 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]; auto dest_ptr = &dest[0];
for (auto src_ptr = src; src_ptr != (src + num); ++src_ptr, dest_ptr += 2) { for (auto src_ptr = src; src_ptr != (src + num); ++src_ptr, dest_ptr += 2) {
const char* hex_p = &numbers_internal::kHexTable[*src_ptr * 2]; const char* hex_p = &numbers_internal::kHexTable[*src_ptr * 2];
......
...@@ -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