Add ParseFromCord to TextFormat

PiperOrigin-RevId: 515128212
pull/12182/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 03c7692bfb
commit 055a6c669f
  1. 18
      src/google/protobuf/text_format.cc
  2. 5
      src/google/protobuf/text_format.h
  3. 7
      src/google/protobuf/text_format_unittest.cc

@ -48,6 +48,7 @@
#include "absl/container/btree_set.h" #include "absl/container/btree_set.h"
#include "absl/strings/ascii.h" #include "absl/strings/ascii.h"
#include "absl/strings/cord.h"
#include "absl/strings/escaping.h" #include "absl/strings/escaping.h"
#include "absl/strings/numbers.h" #include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
@ -62,6 +63,7 @@
#include "google/protobuf/io/tokenizer.h" #include "google/protobuf/io/tokenizer.h"
#include "google/protobuf/io/zero_copy_stream.h" #include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/map_field.h" #include "google/protobuf/map_field.h"
#include "google/protobuf/message.h" #include "google/protobuf/message.h"
#include "google/protobuf/reflection_mode.h" #include "google/protobuf/reflection_mode.h"
@ -1670,8 +1672,8 @@ TextFormat::Parser::~Parser() {}
namespace { namespace {
bool CheckParseInputSize(absl::string_view input, template <typename T>
io::ErrorCollector* error_collector) { bool CheckParseInputSize(T& input, io::ErrorCollector* error_collector) {
if (input.size() > INT_MAX) { if (input.size() > INT_MAX) {
error_collector->RecordError( error_collector->RecordError(
-1, 0, -1, 0,
@ -1709,6 +1711,13 @@ bool TextFormat::Parser::ParseFromString(absl::string_view input,
return Parse(&input_stream, output); return Parse(&input_stream, output);
} }
bool TextFormat::Parser::ParseFromCord(const absl::Cord& input,
Message* output) {
DO(CheckParseInputSize(input, error_collector_));
io::CordInputStream input_stream(&input);
return Parse(&input_stream, output);
}
bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input, bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input,
Message* output) { Message* output) {
ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_, ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_,
@ -1770,6 +1779,11 @@ bool TextFormat::Parser::ParseFieldValueFromString(const std::string& input,
return Parser().ParseFromString(input, output); return Parser().ParseFromString(input, output);
} }
/* static */ bool TextFormat::ParseFromCord(const absl::Cord& input,
Message* output) {
return Parser().ParseFromCord(input, output);
}
/* static */ bool TextFormat::MergeFromString(absl::string_view input, /* static */ bool TextFormat::MergeFromString(absl::string_view input,
Message* output) { Message* output) {
return Parser().MergeFromString(input, output); return Parser().MergeFromString(input, output);

@ -45,6 +45,7 @@
#include "google/protobuf/port.h" #include "google/protobuf/port.h"
#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_map.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h" #include "google/protobuf/message.h"
@ -535,6 +536,8 @@ class PROTOBUF_EXPORT TextFormat {
static bool Parse(io::ZeroCopyInputStream* input, Message* output); static bool Parse(io::ZeroCopyInputStream* input, Message* output);
// Like Parse(), but reads directly from a string. // Like Parse(), but reads directly from a string.
static bool ParseFromString(absl::string_view input, Message* output); static bool ParseFromString(absl::string_view input, Message* output);
// Like Parse(), but reads directly from a Cord.
static bool ParseFromCord(const absl::Cord& input, Message* output);
// Like Parse(), but the data is merged into the given message, as if // Like Parse(), but the data is merged into the given message, as if
// using Message::MergeFrom(). // using Message::MergeFrom().
@ -626,6 +629,8 @@ class PROTOBUF_EXPORT TextFormat {
bool Parse(io::ZeroCopyInputStream* input, Message* output); bool Parse(io::ZeroCopyInputStream* input, Message* output);
// Like TextFormat::ParseFromString(). // Like TextFormat::ParseFromString().
bool ParseFromString(absl::string_view input, Message* output); bool ParseFromString(absl::string_view input, Message* output);
// Like TextFormat::ParseFromCord().
bool ParseFromCord(const absl::Cord& input, Message* output);
// Like TextFormat::Merge(). // Like TextFormat::Merge().
bool Merge(io::ZeroCopyInputStream* input, Message* output); bool Merge(io::ZeroCopyInputStream* input, Message* output);
// Like TextFormat::MergeFromString(). // Like TextFormat::MergeFromString().

@ -51,6 +51,7 @@
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/log/die_if_null.h" #include "absl/log/die_if_null.h"
#include "absl/log/scoped_mock_log.h" #include "absl/log/scoped_mock_log.h"
#include "absl/strings/cord.h"
#include "absl/strings/escaping.h" #include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
@ -836,6 +837,12 @@ TEST_F(TextFormatTest, ParseBasic) {
TestUtil::ExpectAllFieldsSet(proto_); TestUtil::ExpectAllFieldsSet(proto_);
} }
TEST_F(TextFormatTest, ParseCordBasic) {
absl::Cord cord(proto_text_format_);
TextFormat::ParseFromCord(cord, &proto_);
TestUtil::ExpectAllFieldsSet(proto_);
}
TEST_F(TextFormatExtensionsTest, ParseExtensions) { TEST_F(TextFormatExtensionsTest, ParseExtensions) {
io::ArrayInputStream input_stream(proto_text_format_.data(), io::ArrayInputStream input_stream(proto_text_format_.data(),
proto_text_format_.size()); proto_text_format_.size());

Loading…
Cancel
Save