migrate UpbStrFromStringView out of hpb.h into interop

+renamed to CopyToUpbStringView

PiperOrigin-RevId: 676485604
pull/18380/head
Hong Shin 6 months ago committed by Copybara-Service
parent 87f9ecc14d
commit 8681742a14
  1. 10
      hpb/backend/upb/interop.h
  2. 9
      hpb/hpb.h
  3. 5
      hpb_generator/gen_accessors.cc
  4. 6
      hpb_generator/gen_repeated_fields.cc

@ -10,6 +10,8 @@
// The sole public header in hpb/backend/upb
#include <cstring>
#include "absl/strings/string_view.h"
#include "google/protobuf/hpb/internal/internal.h"
#include "google/protobuf/hpb/ptr.h"
@ -111,6 +113,14 @@ inline absl::string_view FromUpbStringView(upb_StringView str) {
return absl::string_view(str.data, str.size);
}
inline upb_StringView CopyToUpbStringView(absl::string_view str,
upb_Arena* arena) {
const size_t str_size = str.size();
char* buffer = static_cast<char*>(upb_Arena_Malloc(arena, str_size));
memcpy(buffer, str.data(), str_size);
return upb_StringView_FromDataAndSize(buffer, str_size);
}
} // namespace hpb::interop::upb
#endif // GOOGLE_PROTOBUF_HPB_BACKEND_UPB_INTEROP_H__

@ -37,15 +37,6 @@
namespace hpb {
class ExtensionRegistry;
// TODO: update bzl and move to upb runtime / protos.cc.
inline upb_StringView UpbStrFromStringView(absl::string_view str,
upb_Arena* arena) {
const size_t str_size = str.size();
char* buffer = static_cast<char*>(upb_Arena_Malloc(arena, str_size));
memcpy(buffer, str.data(), str_size);
return upb_StringView_FromDataAndSize(buffer, str_size);
}
// This type exists to work around an absl type that has not yet been
// released.
struct SourceLocation {

@ -234,7 +234,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Output& output) {
output(
R"cc(
void $0::set_$2($1 value) {
$4_set_$3(msg_, ::hpb::UpbStrFromStringView(value, $5));
$4_set_$3(msg_, hpb::interop::upb::CopyToUpbStringView(value, $5));
}
)cc",
class_name, CppConstType(field), resolved_field_name,
@ -353,7 +353,8 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
R"cc(
bool $0::set_$1($2 key, $3 value) {
$5return $4_$7_set(
msg_, $6, ::hpb::UpbStrFromStringView(value, arena_), arena_);
msg_, $6, hpb::interop::upb::CopyToUpbStringView(value, arena_),
arena_);
}
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),

@ -219,7 +219,9 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
output(
R"cc(
bool $0::add_$2($1 val) {
return $3_add_$4(msg_, ::hpb::UpbStrFromStringView(val, arena_), arena_);
return $3_add_$4(msg_,
hpb::interop::upb::CopyToUpbStringView(val, arena_),
arena_);
}
)cc",
class_name, CppConstType(field), resolved_field_name,
@ -230,7 +232,7 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
size_t len;
auto* ptr = $3_mutable_$4(msg_, &len);
assert(index < len);
*(ptr + index) = ::hpb::UpbStrFromStringView(val, arena_);
*(ptr + index) = hpb::interop::upb::CopyToUpbStringView(val, arena_);
}
)cc",
class_name, CppConstType(field), resolved_field_name,

Loading…
Cancel
Save