Update `google::protobuf::MapValueRef` and `google::protobuf::MapValueConstRef` to use `absl::string_view`

PiperOrigin-RevId: 681485247
pull/18532/head
Protobuf Team Bot 4 months ago committed by Copybara-Service
parent 6bfff3909c
commit 7407733a1e
  1. 3
      src/google/protobuf/map_field.cc
  2. 8
      src/google/protobuf/map_field.h
  3. 2
      src/google/protobuf/reflection_visit_field_info.h
  4. 3
      src/google/protobuf/text_format.cc
  5. 3
      src/google/protobuf/util/field_comparator.h

@ -269,7 +269,8 @@ void MapFieldBase::SyncRepeatedFieldWithMapNoLock() {
const MapValueRef& map_val = it.GetValueRef(); const MapValueRef& map_val = it.GetValueRef();
switch (val_des->cpp_type()) { switch (val_des->cpp_type()) {
case FieldDescriptor::CPPTYPE_STRING: case FieldDescriptor::CPPTYPE_STRING:
reflection->SetString(new_entry, val_des, map_val.GetStringValue()); reflection->SetString(new_entry, val_des,
std::string(map_val.GetStringValue()));
break; break;
case FieldDescriptor::CPPTYPE_INT64: case FieldDescriptor::CPPTYPE_INT64:
reflection->SetInt64(new_entry, val_des, map_val.GetInt64Value()); reflection->SetInt64(new_entry, val_des, map_val.GetInt64Value());

@ -762,10 +762,10 @@ class PROTOBUF_EXPORT MapValueConstRef {
TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueConstRef::GetEnumValue"); TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueConstRef::GetEnumValue");
return *reinterpret_cast<int*>(data_); return *reinterpret_cast<int*>(data_);
} }
const std::string& GetStringValue() const { absl::string_view GetStringValue() const {
TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING,
"MapValueConstRef::GetStringValue"); "MapValueConstRef::GetStringValue");
return *reinterpret_cast<std::string*>(data_); return absl::string_view(*reinterpret_cast<std::string*>(data_));
} }
float GetFloatValue() const { float GetFloatValue() const {
TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT,
@ -855,9 +855,9 @@ class PROTOBUF_EXPORT MapValueRef final : public MapValueConstRef {
TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::SetEnumValue"); TYPE_CHECK(FieldDescriptor::CPPTYPE_ENUM, "MapValueRef::SetEnumValue");
*reinterpret_cast<int*>(data_) = value; *reinterpret_cast<int*>(data_) = value;
} }
void SetStringValue(const std::string& value) { void SetStringValue(absl::string_view value) {
TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::SetStringValue"); TYPE_CHECK(FieldDescriptor::CPPTYPE_STRING, "MapValueRef::SetStringValue");
*reinterpret_cast<std::string*>(data_) = value; reinterpret_cast<std::string*>(data_)->assign(value.data(), value.size());
} }
void SetFloatValue(float value) { void SetFloatValue(float value) {
TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::SetFloatValue"); TYPE_CHECK(FieldDescriptor::CPPTYPE_FLOAT, "MapValueRef::SetFloatValue");

@ -1245,7 +1245,7 @@ PROTOBUF_MAP_VALUE_INFO(Bool, bool, BOOL);
PROTOBUF_MAP_VALUE_INFO(Enum, int, ENUM); PROTOBUF_MAP_VALUE_INFO(Enum, int, ENUM);
PROTOBUF_MAP_VALUE_INFO(Float, float, FLOAT); PROTOBUF_MAP_VALUE_INFO(Float, float, FLOAT);
PROTOBUF_MAP_VALUE_INFO(Double, double, DOUBLE); PROTOBUF_MAP_VALUE_INFO(Double, double, DOUBLE);
PROTOBUF_MAP_VALUE_INFO(String, const std::string&, STRING); PROTOBUF_MAP_VALUE_INFO(String, absl::string_view, STRING);
#undef PROTOBUF_MAP_VALUE_INFO #undef PROTOBUF_MAP_VALUE_INFO

@ -2608,7 +2608,8 @@ void MapFieldPrinterHelper::CopyValue(const MapValueRef& value,
return; return;
} }
case FieldDescriptor::CPPTYPE_STRING: case FieldDescriptor::CPPTYPE_STRING:
reflection->SetString(message, field_desc, value.GetStringValue()); reflection->SetString(message, field_desc,
std::string(value.GetStringValue()));
return; return;
case FieldDescriptor::CPPTYPE_INT64: case FieldDescriptor::CPPTYPE_INT64:
reflection->SetInt64(message, field_desc, value.GetInt64Value()); reflection->SetInt64(message, field_desc, value.GetInt64Value());

@ -16,6 +16,7 @@
#include "google/protobuf/stubs/common.h" #include "google/protobuf/stubs/common.h"
#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/port.h" #include "google/protobuf/port.h"
// Must be included last. // Must be included last.
@ -192,7 +193,7 @@ class PROTOBUF_EXPORT SimpleFieldComparator : public FieldComparator {
} }
bool CompareString(const FieldDescriptor& /* unused */, bool CompareString(const FieldDescriptor& /* unused */,
const std::string& value_1, const std::string& value_2) { absl::string_view value_1, absl::string_view value_2) {
return value_1 == value_2; return value_1 == value_2;
} }

Loading…
Cancel
Save