string_view: Enable std::view and std::borrowed_range

These are enabled for `std::string_view`.

https://en.cppreference.com/w/cpp/string/basic_string_view.html#Helper_templates

PiperOrigin-RevId: 822117252
Change-Id: I8b503a0612d33eabf1be3550c0ea735866c28d63
This commit is contained in:
Jesse Rosenstock
2025-10-21 07:55:22 -07:00
committed by Copybara-Service
parent 2f1be7bdae
commit 71c513c5f8
2 changed files with 45 additions and 0 deletions

View File

@@ -66,6 +66,32 @@ ABSL_NAMESPACE_END
#define ABSL_INTERNAL_STRING_VIEW_MEMCMP memcmp
#endif // ABSL_HAVE_BUILTIN(__builtin_memcmp)
// If `std::ranges` is available, mark `string_view` as satisfying the
// `view` and `borrowed_range` concepts, just like `std::string_view`.
#ifdef __has_include
#if __has_include(<version>)
#include <version>
#endif
#endif
#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L
#include <ranges> // NOLINT(build/c++20)
namespace absl {
ABSL_NAMESPACE_BEGIN
class string_view;
ABSL_NAMESPACE_END
} // namespace absl
template <>
// NOLINTNEXTLINE(build/c++20)
inline constexpr bool std::ranges::enable_view<absl::string_view> = true;
template <>
// NOLINTNEXTLINE(build/c++20)
inline constexpr bool std::ranges::enable_borrowed_range<absl::string_view> =
true;
#endif
namespace absl {
ABSL_NAMESPACE_BEGIN

View File

@@ -35,6 +35,16 @@
#include "absl/base/config.h"
#include "absl/meta/type_traits.h"
#ifdef __has_include
#if __has_include(<version>)
#include <version> // NOLINT(misc-include-cleaner)
#endif
#endif
#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L
#include <ranges> // NOLINT(build/c++20)
#endif
#if defined(ABSL_USES_STD_STRING_VIEW) || defined(__ANDROID__)
// We don't control the death messaging when using std::string_view.
// Android assert messages only go to system log, so death tests cannot inspect
@@ -56,6 +66,15 @@ static_assert(absl::type_traits_internal::IsLifetimeBoundAssignment<
absl::string_view, std::string>::value,
"lifetimebound assignment not detected");
#if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L
// NOLINTNEXTLINE(build/c++20)
static_assert(std::ranges::enable_view<absl::string_view>,
"std::ranges::view not enabled");
// NOLINTNEXTLINE(build/c++20)
static_assert(std::ranges::enable_borrowed_range<absl::string_view>,
"std::ranges::borrowed_range not enabled");
#endif
// A minimal allocator that uses malloc().
template <typename T>
struct Mallocator {