Migrate std::string_view usage to absl::string_view.

This is a no-op in C++17 and newer, but adds the ability to use string_view pre-C++17.

Closes https://github.com/protocolbuffers/protobuf/issues/11627

PiperOrigin-RevId: 504410634
pull/11654/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 6d09e2c80a
commit 888810af33
  1. 1
      src/google/protobuf/BUILD.bazel
  2. 31
      src/google/protobuf/map.h
  3. 4
      src/google/protobuf/map_test.inc

@ -337,6 +337,7 @@ cc_library(
"//src/google/protobuf/stubs:lite",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/numeric:bits",
"@com_google_absl//absl/strings:internal",

@ -46,17 +46,15 @@
#include <type_traits>
#include <utility>
#if defined(__cpp_lib_string_view)
#include <string_view>
#endif // defined(__cpp_lib_string_view)
#if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#include "google/protobuf/stubs/common.h"
#include "absl/container/btree_map.h"
#include "absl/hash/hash.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/generated_enum_util.h"
#include "google/protobuf/map_type_handler.h"
@ -229,33 +227,31 @@ struct TransparentSupport {
using key_arg = key_type;
};
#if defined(__cpp_lib_string_view)
// If std::string_view is available, we add transparent support for std::string
// keys. We use std::hash<std::string_view> as it supports the input types we
// care about. The lookup functions accept arbitrary `K`. This will include any
// key type that is convertible to std::string_view.
// We add transparent support for std::string keys. We use
// std::hash<absl::string_view> as it supports the input types we care about.
// The lookup functions accept arbitrary `K`. This will include any key type
// that is convertible to absl::string_view.
template <>
struct TransparentSupport<std::string> {
static std::string_view ImplicitConvert(std::string_view str) { return str; }
// If the element is not convertible to std::string_view, try to convert to
static absl::string_view ImplicitConvert(absl::string_view str) {
return str;
}
// If the element is not convertible to absl::string_view, try to convert to
// std::string first.
// The template makes this overload lose resolution when both have the same
// rank otherwise.
template <typename = void>
static std::string_view ImplicitConvert(const std::string& str) {
static absl::string_view ImplicitConvert(const std::string& str) {
return str;
}
struct hash : private std::hash<std::string_view> {
struct hash : public absl::Hash<absl::string_view> {
using is_transparent = void;
template <typename T>
size_t operator()(const T& str) const {
return base()(ImplicitConvert(str));
return absl::Hash<absl::string_view>::operator()(ImplicitConvert(str));
}
private:
const std::hash<std::string_view>& base() const { return *this; }
};
struct less {
using is_transparent = void;
@ -274,7 +270,6 @@ struct TransparentSupport<std::string> {
template <typename K>
using key_arg = K;
};
#endif // defined(__cpp_lib_string_view)
struct NodeBase {
// Align the node to allow KeyNode to predict the location of the key.

@ -1404,9 +1404,7 @@ void TestTransparent(const Key& key, const Key& miss_key) {
TEST_F(MapImplTest, TransparentLookupForString) {
TestTransparent("ABC", "LKJ");
TestTransparent(std::string("ABC"), std::string("LKJ"));
#if defined(__cpp_lib_string_view)
TestTransparent(std::string_view("ABC"), std::string_view("LKJ"));
#endif // defined(__cpp_lib_string_view)
TestTransparent(absl::string_view("ABC"), absl::string_view("LKJ"));
// std::reference_wrapper
std::string abc = "ABC", lkj = "LKJ";

Loading…
Cancel
Save