mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
Introduce upb_ByteSize
Naïve computation of a serialized upb message PiperOrigin-RevId: 670990379
This commit is contained in:
parent
b01eee755e
commit
290f28871f
5 changed files with 138 additions and 0 deletions
|
|
@ -218,6 +218,7 @@ cc_dist_library(
|
|||
"//upb/text",
|
||||
"//upb/util:def_to_proto",
|
||||
"//upb/util:required_fields",
|
||||
"//upb/wire:byte_size",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -61,6 +61,35 @@ cc_library(
|
|||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "byte_size",
|
||||
srcs = ["byte_size.c"],
|
||||
hdrs = ["byte_size.h"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//upb:mem",
|
||||
"//upb:message",
|
||||
"//upb:mini_table",
|
||||
"//upb:port",
|
||||
"//upb:wire",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "byte_size_test",
|
||||
srcs = ["byte_size_test.cc"],
|
||||
deps = [
|
||||
":byte_size",
|
||||
"//upb:base",
|
||||
"//upb:mem",
|
||||
"//upb:mini_table",
|
||||
"//upb/test:test_messages_proto2_upb_minitable",
|
||||
"//upb/test:test_messages_proto2_upb_proto",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "eps_copy_input_stream",
|
||||
srcs = ["eps_copy_input_stream.c"],
|
||||
|
|
|
|||
37
upb/wire/byte_size.c
Normal file
37
upb/wire/byte_size.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "upb/wire/byte_size.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "upb/mem/arena.h"
|
||||
#include "upb/message/message.h"
|
||||
#include "upb/mini_table/message.h"
|
||||
#include "upb/wire/encode.h"
|
||||
|
||||
// Must be last.
|
||||
#include "upb/port/def.inc"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
size_t upb_ByteSize(const upb_Message* msg, const upb_MiniTable* mt) {
|
||||
upb_Arena* arena = upb_Arena_New();
|
||||
char* buf;
|
||||
size_t res = 0;
|
||||
|
||||
upb_Encode(msg, mt, 0, arena, &buf, &res);
|
||||
|
||||
upb_Arena_Free(arena);
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
31
upb/wire/byte_size.h
Normal file
31
upb/wire/byte_size.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#ifndef UPB_WIRE_BYTE_SIZE_H_
|
||||
#define UPB_WIRE_BYTE_SIZE_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "upb/message/message.h"
|
||||
#include "upb/mini_table/message.h"
|
||||
|
||||
// Must be last.
|
||||
#include "upb/port/def.inc"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
UPB_API size_t upb_ByteSize(const upb_Message* msg, const upb_MiniTable* mt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#include "upb/port/undef.inc"
|
||||
|
||||
#endif // UPB_WIRE_BYTE_SIZE_H_
|
||||
40
upb/wire/byte_size_test.cc
Normal file
40
upb/wire/byte_size_test.cc
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#include "upb/wire/byte_size.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "google/protobuf/test_messages_proto2.upb.h"
|
||||
#include "google/protobuf/test_messages_proto2.upb_minitable.h"
|
||||
#include "upb/base/upcast.h"
|
||||
#include "upb/mem/arena.h"
|
||||
#include "upb/mini_table/message.h"
|
||||
|
||||
namespace {
|
||||
static const upb_MiniTable* kTestMiniTable =
|
||||
&protobuf_0test_0messages__proto2__TestAllTypesProto2_msg_init;
|
||||
|
||||
TEST(ByteSizeTest, UnpopulatedMsg) {
|
||||
upb_Arena* arena = upb_Arena_New();
|
||||
protobuf_test_messages_proto2_TestAllTypesProto2* msg =
|
||||
protobuf_test_messages_proto2_TestAllTypesProto2_new(arena);
|
||||
auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable);
|
||||
EXPECT_EQ(res, 0);
|
||||
upb_Arena_Free(arena);
|
||||
}
|
||||
|
||||
TEST(ByteSizeTest, PopulatedMsg) {
|
||||
upb_Arena* arena = upb_Arena_New();
|
||||
protobuf_test_messages_proto2_TestAllTypesProto2* msg =
|
||||
protobuf_test_messages_proto2_TestAllTypesProto2_new(arena);
|
||||
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_int32(msg, 322);
|
||||
auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable);
|
||||
EXPECT_EQ(res, 3);
|
||||
upb_Arena_Free(arena);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Loading…
Add table
Add a link
Reference in a new issue