diff --git a/hpb/backend/upb/interop.h b/hpb/backend/upb/interop.h index c899cb6fef..1c219845ed 100644 --- a/hpb/backend/upb/interop.h +++ b/hpb/backend/upb/interop.h @@ -10,6 +10,8 @@ // The sole public header in hpb/backend/upb +#include + #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(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__ diff --git a/hpb/hpb.h b/hpb/hpb.h index da47212483..b84b0534ab 100644 --- a/hpb/hpb.h +++ b/hpb/hpb.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(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 { diff --git a/hpb_generator/gen_accessors.cc b/hpb_generator/gen_accessors.cc index 3d68aa920f..9422df8104 100644 --- a/hpb_generator/gen_accessors.cc +++ b/hpb_generator/gen_accessors.cc @@ -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), diff --git a/hpb_generator/gen_repeated_fields.cc b/hpb_generator/gen_repeated_fields.cc index 0af6b54af6..bf67fe7838 100644 --- a/hpb_generator/gen_repeated_fields.cc +++ b/hpb_generator/gen_repeated_fields.cc @@ -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,