Skip to content

Commit

Permalink
Rewrite some string_view methods to avoid a -Wunreachable-code warning
Browse files Browse the repository at this point in the history
The previous form was required for C++11 compatibility (C++11 support
was dropped in 2020).

PiperOrigin-RevId: 698018101
Change-Id: I814ec708ea211c4b90ee6a812db9961b5423c22e
  • Loading branch information
derekmauro authored and copybara-github committed Nov 19, 2024
1 parent 917bfee commit ee5e49f
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions absl/strings/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,10 @@ class ABSL_ATTRIBUTE_VIEW string_view {
// and an exception of type `std::out_of_range` will be thrown on invalid
// access.
constexpr const_reference at(size_type i) const {
return ABSL_PREDICT_TRUE(i < size())
? ptr_[i]
: ((void)base_internal::ThrowStdOutOfRange(
"absl::string_view::at"),
ptr_[i]);
if (ABSL_PREDICT_FALSE(i >= size())) {
base_internal::ThrowStdOutOfRange("absl::string_view::at");
}
return ptr_[i];
}

// string_view::front()
Expand Down Expand Up @@ -394,11 +393,10 @@ class ABSL_ATTRIBUTE_VIEW string_view {
// `pos > size`.
// Use absl::ClippedSubstr if you need a truncating substr operation.
constexpr string_view substr(size_type pos = 0, size_type n = npos) const {
return ABSL_PREDICT_FALSE(pos > length_)
? (base_internal::ThrowStdOutOfRange(
"absl::string_view::substr"),
string_view())
: string_view(ptr_ + pos, Min(n, length_ - pos));
if (ABSL_PREDICT_FALSE(pos > length_)) {
base_internal::ThrowStdOutOfRange("absl::string_view::substr");
}
return string_view(ptr_ + pos, Min(n, length_ - pos));
}

// string_view::compare()
Expand Down

0 comments on commit ee5e49f

Please sign in to comment.