55
55
string Message::DebugString() const {
56
56
string debug_string;
57
io::StringOutputStream output_stream(&debug_string);
59
TextFormat::Print(*this, &output_stream);
58
TextFormat::PrintToString(*this, &debug_string);
61
60
return debug_string;
64
63
string Message::ShortDebugString() const {
65
64
string debug_string;
66
io::StringOutputStream output_stream(&debug_string);
68
66
TextFormat::Printer printer;
69
67
printer.SetSingleLineMode(true);
71
printer.Print(*this, &output_stream);
69
printer.PrintToString(*this, &debug_string);
72
70
// Single line mode currently might have an extra space at the end.
73
71
if (debug_string.size() > 0 &&
74
72
debug_string[debug_string.size() - 1] == ' ') {
78
76
return debug_string;
79
string Message::Utf8DebugString() const {
82
TextFormat::Printer printer;
83
printer.SetUseUtf8StringEscaping(true);
85
printer.PrintToString(*this, &debug_string);
81
90
void Message::PrintDebugString() const {
82
91
printf("%s", DebugString().c_str());
85
95
// ===========================================================================
86
96
// Internal class for parsing an ASCII representation of a Protocol Message.
87
97
// This class makes use of the Protocol Message compiler's tokenizer found
183
void ReportWarning(int line, int col, const string& message) {
184
if (error_collector_ == NULL) {
186
GOOGLE_LOG(WARNING) << "Warning parsing text-format "
187
<< root_message_type_->full_name()
188
<< ": " << (line + 1) << ":"
189
<< (col + 1) << ": " << message;
191
GOOGLE_LOG(WARNING) << "Warning parsing text-format "
192
<< root_message_type_->full_name()
196
error_collector_->AddWarning(line, col, message);
174
201
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserImpl);
210
// Reports a warning with the given message with information indicating
211
// the position (as derived from the current token).
212
void ReportWarning(const string& message) {
213
ReportWarning(tokenizer_.current().line, tokenizer_.current().column,
183
217
// Consumes the specified message with the given starting delimeter.
184
218
// This method checks to see that the end delimeter at the conclusion of
185
219
// the consumption matches the starting delimeter passed in here.
583
622
parser_->ReportError(line, column, message);
625
virtual void AddWarning(int line, int column, const string& message) {
626
parser_->ReportWarning(line, column, message);
587
630
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserErrorCollector);
588
631
TextFormat::Parser::ParserImpl* parser_;
645
688
// Print text to the output stream.
646
689
void Print(const string& str) {
690
Print(str.data(), str.size());
650
693
// Print text to the output stream.
651
694
void Print(const char* text) {
652
int size = strlen(text);
695
Print(text, strlen(text));
698
// Print text to the output stream.
699
void Print(const char* text, int size) {
653
700
int pos = 0; // The number of bytes we've written so far.
655
702
for (int i = 0; i < size; i++) {
800
847
TextFormat::Printer::Printer()
801
848
: initial_indent_level_(0),
802
single_line_mode_(false) {}
849
single_line_mode_(false),
850
use_short_repeated_primitives_(false),
851
utf8_string_escaping_(false) {}
804
853
TextFormat::Printer::~Printer() {}
876
925
const Reflection* reflection,
877
926
const FieldDescriptor* field,
878
927
TextGenerator& generator) {
928
if (use_short_repeated_primitives_ &&
929
field->is_repeated() &&
930
field->cpp_type() != FieldDescriptor::CPPTYPE_STRING &&
931
field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
932
PrintShortRepeatedField(message, reflection, field, generator);
881
938
if (field->is_repeated()) {
887
944
for (int j = 0; j < count; ++j) {
888
if (field->is_extension()) {
889
generator.Print("[");
890
// We special-case MessageSet elements for compatibility with proto1.
891
if (field->containing_type()->options().message_set_wire_format()
892
&& field->type() == FieldDescriptor::TYPE_MESSAGE
893
&& field->is_optional()
894
&& field->extension_scope() == field->message_type()) {
895
generator.Print(field->message_type()->full_name());
897
generator.Print(field->full_name());
899
generator.Print("]");
901
if (field->type() == FieldDescriptor::TYPE_GROUP) {
902
// Groups must be serialized with their original capitalization.
903
generator.Print(field->message_type()->name());
905
generator.Print(field->name());
945
PrintFieldName(message, reflection, field, generator);
909
947
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
910
948
if (single_line_mode_) {
926
964
PrintFieldValue(message, reflection, field, field_index, generator);
928
966
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
929
if (!single_line_mode_) {
967
if (single_line_mode_) {
968
generator.Print("} ");
930
970
generator.Outdent();
932
generator.Print("}");
935
if (single_line_mode_) {
936
generator.Print(" ");
938
generator.Print("\n");
971
generator.Print("}\n");
974
if (single_line_mode_) {
975
generator.Print(" ");
977
generator.Print("\n");
983
void TextFormat::Printer::PrintShortRepeatedField(const Message& message,
984
const Reflection* reflection,
985
const FieldDescriptor* field,
986
TextGenerator& generator) {
987
// Print primitive repeated field in short form.
988
PrintFieldName(message, reflection, field, generator);
990
int size = reflection->FieldSize(message, field);
991
generator.Print(": [");
992
for (int i = 0; i < size; i++) {
993
if (i > 0) generator.Print(", ");
994
PrintFieldValue(message, reflection, field, i, generator);
996
if (single_line_mode_) {
997
generator.Print("] ");
999
generator.Print("]\n");
1003
void TextFormat::Printer::PrintFieldName(const Message& message,
1004
const Reflection* reflection,
1005
const FieldDescriptor* field,
1006
TextGenerator& generator) {
1007
if (field->is_extension()) {
1008
generator.Print("[");
1009
// We special-case MessageSet elements for compatibility with proto1.
1010
if (field->containing_type()->options().message_set_wire_format()
1011
&& field->type() == FieldDescriptor::TYPE_MESSAGE
1012
&& field->is_optional()
1013
&& field->extension_scope() == field->message_type()) {
1014
generator.Print(field->message_type()->full_name());
1016
generator.Print(field->full_name());
1018
generator.Print("]");
1020
if (field->type() == FieldDescriptor::TYPE_GROUP) {
1021
// Groups must be serialized with their original capitalization.
1022
generator.Print(field->message_type()->name());
1024
generator.Print(field->name());
973
1059
reflection->GetStringReference(message, field, &scratch);
975
1061
generator.Print("\"");
976
generator.Print(CEscape(value));
1062
if (utf8_string_escaping_) {
1063
generator.Print(strings::Utf8SafeCEscape(value));
1065
generator.Print(CEscape(value));
977
1067
generator.Print("\"");