absl::string_view: Add support for starts_with() and ends_with()

when targeting at least C++20

These methods were added to C++20, so they are not available in
earlier language standards. Users requiring compatibility prior to C++20
should use absl::StartsWith() and absl::EndsWith() from
//absl/strings/match.h.

Most users are not affected by this change. By default when targeting at least
C++20 absl::string_view will be an alias for std::string_view. Only users
that have modified //absl/base/options.h will see this change.

PiperOrigin-RevId: 575238435
Change-Id: I7b03fde02c987b30b88c794640c2a616851997d1
This commit is contained in:
Derek Mauro
2023-10-20 09:21:26 -07:00
committed by Copybara-Service
parent 0378614336
commit 0f110600fc
2 changed files with 123 additions and 1 deletions

View File

@@ -506,7 +506,7 @@ class string_view {
// Overload of `string_view::find_first_of()` for finding a substring of a
// different C-style string `s` within the `string_view`.
size_type find_first_of(const char* s, size_type pos,
size_type count) const {
size_type count) const {
return find_first_of(string_view(s, count), pos);
}
@@ -590,6 +590,58 @@ class string_view {
return find_last_not_of(string_view(s), pos);
}
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
// string_view::starts_with()
//
// Returns true if the `string_view` starts with the prefix `s`.
//
// This method only exists when targeting at least C++20.
// If support for C++ prior to C++20 is required, use `absl::StartsWith()`
// from `//absl/strings/match.h` for compatibility.
constexpr bool starts_with(string_view s) const noexcept {
return s.empty() ||
(size() >= s.size() &&
ABSL_INTERNAL_STRING_VIEW_MEMCMP(data(), s.data(), s.size()) == 0);
}
// Overload of `string_view::starts_with()` that returns true if `c` is the
// first character of the `string_view`.
constexpr bool starts_with(char c) const noexcept {
return !empty() && front() == c;
}
// Overload of `string_view::starts_with()` that returns true if the
// `string_view` starts with the C-style prefix `s`.
constexpr bool starts_with(const char* s) const {
return starts_with(string_view(s));
}
// string_view::ends_with()
//
// Returns true if the `string_view` ends with the suffix `s`.
//
// This method only exists when targeting at least C++20.
// If support for C++ prior to C++20 is required, use `absl::EndsWith()`
// from `//absl/strings/match.h` for compatibility.
constexpr bool ends_with(string_view s) const noexcept {
return s.empty() || (size() >= s.size() && ABSL_INTERNAL_STRING_VIEW_MEMCMP(
data() + (size() - s.size()),
s.data(), s.size()) == 0);
}
// Overload of `string_view::ends_with()` that returns true if `c` is the
// last character of the `string_view`.
constexpr bool ends_with(char c) const noexcept {
return !empty() && back() == c;
}
// Overload of `string_view::ends_with()` that returns true if the
// `string_view` ends with the C-style suffix `s`.
constexpr bool ends_with(const char* s) const {
return ends_with(string_view(s));
}
#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
private:
// The constructor from std::string delegates to this constructor.
// See the comment on that constructor for the rationale.