summaryrefslogtreecommitdiff
path: root/absl/strings/internal/str_format/arg.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-08-17 09:17:56 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-08-17 09:18:39 -0700
commitfcfc7a6d15eab5aff86d7b90c9e38fa386a103fa (patch)
treeec697cf6574f311f9f373df7d19f307d9c185781 /absl/strings/internal/str_format/arg.cc
parent934f613818ffcb26c942dff4a80be9a4031c662c (diff)
Fix "unsafe narrowing" warnings in absl, 5/n.
Addresses failures with the following, in some files: -Wshorten-64-to-32 -Wimplicit-int-conversion -Wsign-compare -Wsign-conversion -Wtautological-unsigned-zero-compare (This specific CL focuses on .cc files in strings/internal/.) Bug: chromium:1292951 PiperOrigin-RevId: 468215101 Change-Id: I07fa487bcf2cf62d403489c3be7a5997cdef8987
Diffstat (limited to 'absl/strings/internal/str_format/arg.cc')
-rw-r--r--absl/strings/internal/str_format/arg.cc35
1 files changed, 19 insertions, 16 deletions
diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc
index 02aeeebe..007e8e83 100644
--- a/absl/strings/internal/str_format/arg.cc
+++ b/absl/strings/internal/str_format/arg.cc
@@ -77,7 +77,7 @@ class IntDigits {
v >>= 3;
} while (v);
start_ = p;
- size_ = storage_ + sizeof(storage_) - p;
+ size_ = static_cast<size_t>(storage_ + sizeof(storage_) - p);
}
// Print the signed or unsigned integer as decimal.
@@ -86,7 +86,8 @@ class IntDigits {
void PrintAsDec(T v) {
static_assert(std::is_integral<T>::value, "");
start_ = storage_;
- size_ = numbers_internal::FastIntToBuffer(v, storage_) - storage_;
+ size_ = static_cast<size_t>(numbers_internal::FastIntToBuffer(v, storage_) -
+ storage_);
}
void PrintAsDec(int128 v) {
@@ -115,7 +116,7 @@ class IntDigits {
if (add_neg) {
*--p = '-';
}
- size_ = storage_ + sizeof(storage_) - p;
+ size_ = static_cast<size_t>(storage_ + sizeof(storage_) - p);
start_ = p;
}
@@ -138,7 +139,7 @@ class IntDigits {
++p;
}
start_ = p;
- size_ = storage_ + sizeof(storage_) - p;
+ size_ = static_cast<size_t>(storage_ + sizeof(storage_) - p);
}
// Print the unsigned integer as hex using uppercase.
@@ -154,7 +155,7 @@ class IntDigits {
v >>= 4;
} while (v);
start_ = p;
- size_ = storage_ + sizeof(storage_) - p;
+ size_ = static_cast<size_t>(storage_ + sizeof(storage_) - p);
}
// The printed value including the '-' sign if available.
@@ -208,10 +209,12 @@ string_view SignColumn(bool neg, const FormatConversionSpecImpl conv) {
return {};
}
-bool ConvertCharImpl(unsigned char v, const FormatConversionSpecImpl conv,
- FormatSinkImpl *sink) {
+bool ConvertCharImpl(char v,
+ const FormatConversionSpecImpl conv,
+ FormatSinkImpl* sink) {
size_t fill = 0;
- if (conv.width() >= 0) fill = conv.width();
+ if (conv.width() >= 0)
+ fill = static_cast<size_t>(conv.width());
ReducePadding(1, &fill);
if (!conv.has_left_flag()) sink->Append(fill, ' ');
sink->Append(1, v);
@@ -225,7 +228,8 @@ bool ConvertIntImplInnerSlow(const IntDigits &as_digits,
// Print as a sequence of Substrings:
// [left_spaces][sign][base_indicator][zeroes][formatted][right_spaces]
size_t fill = 0;
- if (conv.width() >= 0) fill = conv.width();
+ if (conv.width() >= 0)
+ fill = static_cast<size_t>(conv.width());
string_view formatted = as_digits.without_neg_or_zero();
ReducePadding(formatted, &fill);
@@ -236,10 +240,9 @@ bool ConvertIntImplInnerSlow(const IntDigits &as_digits,
string_view base_indicator = BaseIndicator(as_digits, conv);
ReducePadding(base_indicator, &fill);
- int precision = conv.precision();
- bool precision_specified = precision >= 0;
- if (!precision_specified)
- precision = 1;
+ bool precision_specified = conv.precision() >= 0;
+ size_t precision =
+ precision_specified ? static_cast<size_t>(conv.precision()) : size_t{1};
if (conv.has_alt_flag() &&
conv.conversion_char() == FormatConversionCharInternal::o) {
@@ -247,7 +250,7 @@ bool ConvertIntImplInnerSlow(const IntDigits &as_digits,
// "For o conversion, it increases the precision (if necessary) to
// force the first digit of the result to be zero."
if (formatted.empty() || *formatted.begin() != '0') {
- int needed = static_cast<int>(formatted.size()) + 1;
+ size_t needed = formatted.size() + 1;
precision = std::max(precision, needed);
}
}
@@ -287,7 +290,7 @@ bool ConvertIntArg(T v, const FormatConversionSpecImpl conv,
// FormatConversionChar is declared, but not defined.
switch (static_cast<uint8_t>(conv.conversion_char())) {
case static_cast<uint8_t>(FormatConversionCharInternal::c):
- return ConvertCharImpl(static_cast<unsigned char>(v), conv, sink);
+ return ConvertCharImpl(static_cast<char>(v), conv, sink);
case static_cast<uint8_t>(FormatConversionCharInternal::o):
as_digits.PrintAsOct(static_cast<U>(v));
@@ -375,7 +378,7 @@ FormatConvertImpl(const char *v, const FormatConversionSpecImpl conv,
len = std::strlen(v);
} else {
// If precision is set, we look for the NUL-terminator on the valid range.
- len = std::find(v, v + conv.precision(), '\0') - v;
+ len = static_cast<size_t>(std::find(v, v + conv.precision(), '\0') - v);
}
return {ConvertStringArg(string_view(v, len), conv, sink)};
}