diff --git a/src/google/protobuf/map_field.cc b/src/google/protobuf/map_field.cc index 19cc3b7a6551..dccef67d8a3f 100644 --- a/src/google/protobuf/map_field.cc +++ b/src/google/protobuf/map_field.cc @@ -8,6 +8,7 @@ #include "google/protobuf/map_field.h" #include +#include #include #include @@ -243,7 +244,8 @@ void MapFieldBase::SyncRepeatedFieldWithMapNoLock() { const MapKey& map_key = it.GetKey(); switch (key_des->cpp_type()) { case FieldDescriptor::CPPTYPE_STRING: - reflection->SetString(new_entry, key_des, map_key.GetStringValue()); + reflection->SetString(new_entry, key_des, + std::string(map_key.GetStringValue())); break; case FieldDescriptor::CPPTYPE_INT64: reflection->SetInt64(new_entry, key_des, map_key.GetInt64Value()); diff --git a/src/google/protobuf/map_field.h b/src/google/protobuf/map_field.h index 21021560cc95..c40070072d26 100644 --- a/src/google/protobuf/map_field.h +++ b/src/google/protobuf/map_field.h @@ -18,6 +18,7 @@ #include "absl/log/absl_check.h" #include "absl/log/absl_log.h" +#include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "google/protobuf/arena.h" #include "google/protobuf/descriptor.h" @@ -136,7 +137,7 @@ class PROTOBUF_EXPORT MapKey { TYPE_CHECK(FieldDescriptor::CPPTYPE_BOOL, "MapKey::GetBoolValue"); return val_.bool_value; } - const std::string& GetStringValue() const { + absl::string_view GetStringValue() const { TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapKey::GetStringValue"); return val_.string_value.get(); } diff --git a/src/google/protobuf/map_field_inl.h b/src/google/protobuf/map_field_inl.h index e6a7acfc9ba1..5dfed435468c 100644 --- a/src/google/protobuf/map_field_inl.h +++ b/src/google/protobuf/map_field_inl.h @@ -16,6 +16,7 @@ #include #include "absl/base/casts.h" +#include "absl/strings/string_view.h" #include "google/protobuf/map.h" #include "google/protobuf/map_field.h" #include "google/protobuf/map_type_handler.h" @@ -50,8 +51,8 @@ inline uint64_t UnwrapMapKeyImpl(const MapKey& map_key, const uint64_t*) { inline bool UnwrapMapKeyImpl(const MapKey& map_key, const bool*) { return map_key.GetBoolValue(); } -inline const std::string& UnwrapMapKeyImpl(const MapKey& map_key, - const std::string*) { +inline absl::string_view UnwrapMapKeyImpl(const MapKey& map_key, + const std::string*) { return map_key.GetStringValue(); } inline const MapKey& UnwrapMapKeyImpl(const MapKey& map_key, const MapKey*) { diff --git a/src/google/protobuf/reflection_visit_field_info.h b/src/google/protobuf/reflection_visit_field_info.h index 148a246dc123..c6758271e148 100644 --- a/src/google/protobuf/reflection_visit_field_info.h +++ b/src/google/protobuf/reflection_visit_field_info.h @@ -1217,7 +1217,7 @@ PROTOBUF_MAP_KEY_INFO(Int64, int64_t, INT64); PROTOBUF_MAP_KEY_INFO(UInt32, uint32_t, UINT32); PROTOBUF_MAP_KEY_INFO(UInt64, uint64_t, UINT64); PROTOBUF_MAP_KEY_INFO(Bool, bool, BOOL); -PROTOBUF_MAP_KEY_INFO(String, const std::string&, STRING); +PROTOBUF_MAP_KEY_INFO(String, absl::string_view, STRING); #undef PROTOBUF_MAP_KEY_INFO diff --git a/src/google/protobuf/text_format.cc b/src/google/protobuf/text_format.cc index 1ed6e575bcc9..b66195cfe48b 100644 --- a/src/google/protobuf/text_format.cc +++ b/src/google/protobuf/text_format.cc @@ -2566,7 +2566,8 @@ void MapFieldPrinterHelper::CopyKey(const MapKey& key, Message* message, ABSL_LOG(ERROR) << "Not supported."; break; case FieldDescriptor::CPPTYPE_STRING: - reflection->SetString(message, field_desc, key.GetStringValue()); + reflection->SetString(message, field_desc, + std::string(key.GetStringValue())); return; case FieldDescriptor::CPPTYPE_INT64: reflection->SetInt64(message, field_desc, key.GetInt64Value());