Skip to content

Commit

Permalink
Add ParseFromCord to TextFormat
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 515128212
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Mar 8, 2023
1 parent 03c7692 commit 055a6c6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/google/protobuf/text_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include "absl/container/btree_set.h"
#include "absl/strings/ascii.h"
#include "absl/strings/cord.h"
#include "absl/strings/escaping.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
Expand All @@ -62,6 +63,7 @@
#include "google/protobuf/io/tokenizer.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_lite.h"
#include "google/protobuf/map_field.h"
#include "google/protobuf/message.h"
#include "google/protobuf/reflection_mode.h"
Expand Down Expand Up @@ -1670,8 +1672,8 @@ TextFormat::Parser::~Parser() {}

namespace {

bool CheckParseInputSize(absl::string_view input,
io::ErrorCollector* error_collector) {
template <typename T>
bool CheckParseInputSize(T& input, io::ErrorCollector* error_collector) {
if (input.size() > INT_MAX) {
error_collector->RecordError(
-1, 0,
Expand Down Expand Up @@ -1709,6 +1711,13 @@ bool TextFormat::Parser::ParseFromString(absl::string_view input,
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,
Message* output) {
ParserImpl parser(output->GetDescriptor(), input, error_collector_, finder_,
Expand Down Expand Up @@ -1770,6 +1779,11 @@ bool TextFormat::Parser::ParseFieldValueFromString(const std::string& input,
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,
Message* output) {
return Parser().MergeFromString(input, output);
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/text_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include "google/protobuf/port.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
Expand Down Expand Up @@ -535,6 +536,8 @@ class PROTOBUF_EXPORT TextFormat {
static bool Parse(io::ZeroCopyInputStream* input, Message* output);
// Like Parse(), but reads directly from a string.
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
// using Message::MergeFrom().
Expand Down Expand Up @@ -626,6 +629,8 @@ class PROTOBUF_EXPORT TextFormat {
bool Parse(io::ZeroCopyInputStream* input, Message* output);
// Like TextFormat::ParseFromString().
bool ParseFromString(absl::string_view input, Message* output);
// Like TextFormat::ParseFromCord().
bool ParseFromCord(const absl::Cord& input, Message* output);
// Like TextFormat::Merge().
bool Merge(io::ZeroCopyInputStream* input, Message* output);
// Like TextFormat::MergeFromString().
Expand Down
7 changes: 7 additions & 0 deletions src/google/protobuf/text_format_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "absl/log/absl_check.h"
#include "absl/log/die_if_null.h"
#include "absl/log/scoped_mock_log.h"
#include "absl/strings/cord.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
Expand Down Expand Up @@ -836,6 +837,12 @@ TEST_F(TextFormatTest, ParseBasic) {
TestUtil::ExpectAllFieldsSet(proto_);
}

TEST_F(TextFormatTest, ParseCordBasic) {
absl::Cord cord(proto_text_format_);
TextFormat::ParseFromCord(cord, &proto_);
TestUtil::ExpectAllFieldsSet(proto_);
}

TEST_F(TextFormatExtensionsTest, ParseExtensions) {
io::ArrayInputStream input_stream(proto_text_format_.data(),
proto_text_format_.size());
Expand Down

0 comments on commit 055a6c6

Please sign in to comment.