diff options
Diffstat (limited to 'absl/strings/string_view.h')
-rw-r--r-- | absl/strings/string_view.h | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h index a1b5a17b52b3..68b90aa3126f 100644 --- a/absl/strings/string_view.h +++ b/absl/strings/string_view.h @@ -269,10 +269,22 @@ class string_view { // string_view::operator[] // - // Returns the ith element of an `string_view` using the array operator. + // Returns the ith element of the `string_view` using the array operator. // Note that this operator does not perform any bounds checking. constexpr const_reference operator[](size_type i) const { return ptr_[i]; } + // string_view::at() + // + // Returns the ith element of the `string_view`. Bounds checking is performed, + // 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] + : (base_internal::ThrowStdOutOfRange("absl::string_view::at"), + ptr_[i]); + } + // string_view::front() // // Returns the first element of a `string_view`. |