LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf - text_format.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 201 799 25.2 %
Date: 2015-10-10 Functions: 37 104 35.6 %

          Line data    Source code
       1             : // Protocol Buffers - Google's data interchange format
       2             : // Copyright 2008 Google Inc.  All rights reserved.
       3             : // https://developers.google.com/protocol-buffers/
       4             : //
       5             : // Redistribution and use in source and binary forms, with or without
       6             : // modification, are permitted provided that the following conditions are
       7             : // met:
       8             : //
       9             : //     * Redistributions of source code must retain the above copyright
      10             : // notice, this list of conditions and the following disclaimer.
      11             : //     * Redistributions in binary form must reproduce the above
      12             : // copyright notice, this list of conditions and the following disclaimer
      13             : // in the documentation and/or other materials provided with the
      14             : // distribution.
      15             : //     * Neither the name of Google Inc. nor the names of its
      16             : // contributors may be used to endorse or promote products derived from
      17             : // this software without specific prior written permission.
      18             : //
      19             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      20             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      21             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      22             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      23             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      24             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      25             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      26             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      27             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      28             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      29             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30             : 
      31             : // Author: jschorr@google.com (Joseph Schorr)
      32             : //  Based on original Protocol Buffers design by
      33             : //  Sanjay Ghemawat, Jeff Dean, and others.
      34             : 
      35             : #include <algorithm>
      36             : #include <float.h>
      37             : #include <math.h>
      38             : #include <stdio.h>
      39             : #include <stack>
      40             : #include <limits>
      41             : #include <vector>
      42             : 
      43             : #include <google/protobuf/text_format.h>
      44             : 
      45             : #include <google/protobuf/descriptor.h>
      46             : #include <google/protobuf/dynamic_message.h>
      47             : #include <google/protobuf/repeated_field.h>
      48             : #include <google/protobuf/wire_format_lite.h>
      49             : #include <google/protobuf/io/coded_stream.h>
      50             : #include <google/protobuf/io/zero_copy_stream.h>
      51             : #include <google/protobuf/io/zero_copy_stream_impl.h>
      52             : #include <google/protobuf/unknown_field_set.h>
      53             : #include <google/protobuf/descriptor.pb.h>
      54             : #include <google/protobuf/io/tokenizer.h>
      55             : #include <google/protobuf/any.h>
      56             : #include <google/protobuf/stubs/stringprintf.h>
      57             : #include <google/protobuf/stubs/strutil.h>
      58             : #include <google/protobuf/stubs/map_util.h>
      59             : #include <google/protobuf/stubs/stl_util.h>
      60             : 
      61             : namespace google {
      62             : namespace protobuf {
      63             : 
      64             : namespace {
      65             : 
      66             : inline bool IsHexNumber(const string& str) {
      67           0 :   return (str.length() >= 2 && str[0] == '0' &&
      68           0 :           (str[1] == 'x' || str[1] == 'X'));
      69             : }
      70             : 
      71             : inline bool IsOctNumber(const string& str) {
      72           0 :   return (str.length() >= 2 && str[0] == '0' &&
      73           0 :           (str[1] >= '0' && str[1] < '8'));
      74             : }
      75             : 
      76             : inline bool GetAnyFieldDescriptors(const Message& message,
      77             :                                    const FieldDescriptor** type_url_field,
      78             :                                    const FieldDescriptor** value_field) {
      79             :     const Descriptor* descriptor = message.GetDescriptor();
      80             :     *type_url_field = descriptor->FindFieldByNumber(1);
      81             :     *value_field = descriptor->FindFieldByNumber(2);
      82             :     return (*type_url_field != NULL &&
      83             :             (*type_url_field)->type() == FieldDescriptor::TYPE_STRING &&
      84             :             *value_field != NULL &&
      85             :             (*value_field)->type() == FieldDescriptor::TYPE_BYTES);
      86             : }
      87             : 
      88             : }  // namespace
      89             : 
      90           0 : string Message::DebugString() const {
      91             :   string debug_string;
      92             : 
      93           0 :   TextFormat::PrintToString(*this, &debug_string);
      94             : 
      95           0 :   return debug_string;
      96             : }
      97             : 
      98           0 : string Message::ShortDebugString() const {
      99             :   string debug_string;
     100             : 
     101           0 :   TextFormat::Printer printer;
     102           0 :   printer.SetSingleLineMode(true);
     103             : 
     104           0 :   printer.PrintToString(*this, &debug_string);
     105             :   // Single line mode currently might have an extra space at the end.
     106           0 :   if (debug_string.size() > 0 &&
     107           0 :       debug_string[debug_string.size() - 1] == ' ') {
     108           0 :     debug_string.resize(debug_string.size() - 1);
     109             :   }
     110             : 
     111           0 :   return debug_string;
     112             : }
     113             : 
     114           0 : string Message::Utf8DebugString() const {
     115             :   string debug_string;
     116             : 
     117           0 :   TextFormat::Printer printer;
     118           0 :   printer.SetUseUtf8StringEscaping(true);
     119             : 
     120           0 :   printer.PrintToString(*this, &debug_string);
     121             : 
     122           0 :   return debug_string;
     123             : }
     124             : 
     125           0 : void Message::PrintDebugString() const {
     126           0 :   printf("%s", DebugString().c_str());
     127           0 : }
     128             : 
     129             : 
     130             : // ===========================================================================
     131             : // Implementation of the parse information tree class.
     132           0 : TextFormat::ParseInfoTree::ParseInfoTree() { }
     133             : 
     134           0 : TextFormat::ParseInfoTree::~ParseInfoTree() {
     135             :   // Remove any nested information trees, as they are owned by this tree.
     136           0 :   for (NestedMap::iterator it = nested_.begin(); it != nested_.end(); ++it) {
     137           0 :     STLDeleteElements(&(it->second));
     138             :   }
     139           0 : }
     140             : 
     141           0 : void TextFormat::ParseInfoTree::RecordLocation(
     142             :     const FieldDescriptor* field,
     143             :     TextFormat::ParseLocation location) {
     144           0 :   locations_[field].push_back(location);
     145           0 : }
     146             : 
     147           0 : TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::CreateNested(
     148             :     const FieldDescriptor* field) {
     149             :   // Owned by us in the map.
     150           0 :   TextFormat::ParseInfoTree* instance = new TextFormat::ParseInfoTree();
     151           0 :   vector<TextFormat::ParseInfoTree*>* trees = &nested_[field];
     152           0 :   GOOGLE_CHECK(trees);
     153           0 :   trees->push_back(instance);
     154           0 :   return instance;
     155             : }
     156             : 
     157           0 : void CheckFieldIndex(const FieldDescriptor* field, int index) {
     158           0 :   if (field == NULL) { return; }
     159             : 
     160           0 :   if (field->is_repeated() && index == -1) {
     161           0 :     GOOGLE_LOG(DFATAL) << "Index must be in range of repeated field values. "
     162           0 :                 << "Field: " << field->name();
     163           0 :   } else if (!field->is_repeated() && index != -1) {
     164           0 :     GOOGLE_LOG(DFATAL) << "Index must be -1 for singular fields."
     165           0 :                 << "Field: " << field->name();
     166             :   }
     167             : }
     168             : 
     169           0 : TextFormat::ParseLocation TextFormat::ParseInfoTree::GetLocation(
     170             :     const FieldDescriptor* field, int index) const {
     171           0 :   CheckFieldIndex(field, index);
     172           0 :   if (index == -1) { index = 0; }
     173             : 
     174           0 :   const vector<TextFormat::ParseLocation>* locations =
     175           0 :       FindOrNull(locations_, field);
     176           0 :   if (locations == NULL || index >= locations->size()) {
     177           0 :     return TextFormat::ParseLocation();
     178             :   }
     179             : 
     180           0 :   return (*locations)[index];
     181             : }
     182             : 
     183           0 : TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested(
     184             :     const FieldDescriptor* field, int index) const {
     185           0 :   CheckFieldIndex(field, index);
     186           0 :   if (index == -1) { index = 0; }
     187             : 
     188           0 :   const vector<TextFormat::ParseInfoTree*>* trees = FindOrNull(nested_, field);
     189           0 :   if (trees == NULL || index >= trees->size()) {
     190             :     return NULL;
     191             :   }
     192             : 
     193           0 :   return (*trees)[index];
     194             : }
     195             : 
     196             : 
     197             : // ===========================================================================
     198             : // Internal class for parsing an ASCII representation of a Protocol Message.
     199             : // This class makes use of the Protocol Message compiler's tokenizer found
     200             : // in //google/protobuf/io/tokenizer.h. Note that class's Parse
     201             : // method is *not* thread-safe and should only be used in a single thread at
     202             : // a time.
     203             : 
     204             : // Makes code slightly more readable.  The meaning of "DO(foo)" is
     205             : // "Execute foo and fail if it fails.", where failure is indicated by
     206             : // returning false. Borrowed from parser.cc (Thanks Kenton!).
     207             : #define DO(STATEMENT) if (STATEMENT) {} else return false
     208             : 
     209             : class TextFormat::Parser::ParserImpl {
     210             :  public:
     211             : 
     212             :   // Determines if repeated values for non-repeated fields and
     213             :   // oneofs are permitted, e.g., the string "foo: 1 foo: 2" for a
     214             :   // required/optional field named "foo", or "baz: 1 qux: 2"
     215             :   // where "baz" and "qux" are members of the same oneof.
     216             :   enum SingularOverwritePolicy {
     217             :     ALLOW_SINGULAR_OVERWRITES = 0,   // the last value is retained
     218             :     FORBID_SINGULAR_OVERWRITES = 1,  // an error is issued
     219             :   };
     220             : 
     221           9 :   ParserImpl(const Descriptor* root_message_type,
     222             :              io::ZeroCopyInputStream* input_stream,
     223             :              io::ErrorCollector* error_collector,
     224             :              TextFormat::Finder* finder,
     225             :              ParseInfoTree* parse_info_tree,
     226             :              SingularOverwritePolicy singular_overwrite_policy,
     227             :              bool allow_case_insensitive_field,
     228             :              bool allow_unknown_field,
     229             :              bool allow_unknown_enum,
     230             :              bool allow_field_number,
     231             :              bool allow_relaxed_whitespace)
     232             :     : error_collector_(error_collector),
     233             :       finder_(finder),
     234             :       parse_info_tree_(parse_info_tree),
     235             :       tokenizer_error_collector_(this),
     236             :       tokenizer_(input_stream, &tokenizer_error_collector_),
     237             :       root_message_type_(root_message_type),
     238             :       singular_overwrite_policy_(singular_overwrite_policy),
     239             :       allow_case_insensitive_field_(allow_case_insensitive_field),
     240             :       allow_unknown_field_(allow_unknown_field),
     241             :       allow_unknown_enum_(allow_unknown_enum),
     242             :       allow_field_number_(allow_field_number),
     243          18 :       had_errors_(false) {
     244             :     // For backwards-compatibility with proto1, we need to allow the 'f' suffix
     245             :     // for floats.
     246           9 :     tokenizer_.set_allow_f_after_float(true);
     247             : 
     248             :     // '#' starts a comment.
     249           9 :     tokenizer_.set_comment_style(io::Tokenizer::SH_COMMENT_STYLE);
     250             : 
     251           9 :     if (allow_relaxed_whitespace) {
     252           0 :       tokenizer_.set_require_space_after_number(false);
     253           0 :       tokenizer_.set_allow_multiline_strings(true);
     254             :     }
     255             : 
     256             :     // Consume the starting token.
     257           9 :     tokenizer_.Next();
     258           9 :   }
     259          18 :   ~ParserImpl() { }
     260             : 
     261             :   // Parses the ASCII representation specified in input and saves the
     262             :   // information into the output pointer (a Message). Returns
     263             :   // false if an error occurs (an error will also be logged to
     264             :   // GOOGLE_LOG(ERROR)).
     265           9 :   bool Parse(Message* output) {
     266             :     // Consume fields until we cannot do so anymore.
     267             :     while (true) {
     268          23 :       if (LookingAtType(io::Tokenizer::TYPE_END)) {
     269           9 :         return !had_errors_;
     270             :       }
     271             : 
     272          14 :       DO(ConsumeField(output));
     273             :     }
     274             :   }
     275             : 
     276           0 :   bool ParseField(const FieldDescriptor* field, Message* output) {
     277             :     bool suc;
     278           0 :     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
     279           0 :       suc = ConsumeFieldMessage(output, output->GetReflection(), field);
     280             :     } else {
     281           0 :       suc = ConsumeFieldValue(output, output->GetReflection(), field);
     282             :     }
     283           0 :     return suc && LookingAtType(io::Tokenizer::TYPE_END);
     284             :   }
     285             : 
     286           0 :   void ReportError(int line, int col, const string& message) {
     287           0 :     had_errors_ = true;
     288           0 :     if (error_collector_ == NULL) {
     289           0 :       if (line >= 0) {
     290           0 :         GOOGLE_LOG(ERROR) << "Error parsing text-format "
     291           0 :                    << root_message_type_->full_name()
     292           0 :                    << ": " << (line + 1) << ":"
     293           0 :                    << (col + 1) << ": " << message;
     294             :       } else {
     295           0 :         GOOGLE_LOG(ERROR) << "Error parsing text-format "
     296           0 :                    << root_message_type_->full_name()
     297           0 :                    << ": " << message;
     298             :       }
     299             :     } else {
     300           0 :       error_collector_->AddError(line, col, message);
     301             :     }
     302           0 :   }
     303             : 
     304           0 :   void ReportWarning(int line, int col, const string& message) {
     305           0 :     if (error_collector_ == NULL) {
     306           0 :       if (line >= 0) {
     307           0 :         GOOGLE_LOG(WARNING) << "Warning parsing text-format "
     308           0 :                      << root_message_type_->full_name()
     309           0 :                      << ": " << (line + 1) << ":"
     310           0 :                      << (col + 1) << ": " << message;
     311             :       } else {
     312           0 :         GOOGLE_LOG(WARNING) << "Warning parsing text-format "
     313           0 :                      << root_message_type_->full_name()
     314           0 :                      << ": " << message;
     315             :       }
     316             :     } else {
     317           0 :       error_collector_->AddWarning(line, col, message);
     318             :     }
     319           0 :   }
     320             : 
     321             :  private:
     322             :   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserImpl);
     323             : 
     324             :   // Reports an error with the given message with information indicating
     325             :   // the position (as derived from the current token).
     326             :   void ReportError(const string& message) {
     327           0 :     ReportError(tokenizer_.current().line, tokenizer_.current().column,
     328           0 :                 message);
     329             :   }
     330             : 
     331             :   // Reports a warning with the given message with information indicating
     332             :   // the position (as derived from the current token).
     333             :   void ReportWarning(const string& message) {
     334           0 :     ReportWarning(tokenizer_.current().line, tokenizer_.current().column,
     335           0 :                   message);
     336             :   }
     337             : 
     338             :   // Consumes the specified message with the given starting delimiter.
     339             :   // This method checks to see that the end delimiter at the conclusion of
     340             :   // the consumption matches the starting delimiter passed in here.
     341           5 :   bool ConsumeMessage(Message* message, const string delimiter) {
     342          55 :     while (!LookingAt(">") &&  !LookingAt("}")) {
     343           5 :       DO(ConsumeField(message));
     344             :     }
     345             : 
     346             :     // Confirm that we have a valid ending delimiter.
     347           5 :     DO(Consume(delimiter));
     348           5 :     return true;
     349             :   }
     350             : 
     351             :   // Consume either "<" or "{".
     352           5 :   bool ConsumeMessageDelimiter(string* delimiter) {
     353          10 :     if (TryConsume("<")) {
     354             :       *delimiter = ">";
     355             :     } else {
     356          10 :       DO(Consume("{"));
     357             :       *delimiter = "}";
     358             :     }
     359             :     return true;
     360             :   }
     361             : 
     362             : 
     363             :   // Consumes the current field (as returned by the tokenizer) on the
     364             :   // passed in message.
     365          19 :   bool ConsumeField(Message* message) {
     366          19 :     const Reflection* reflection = message->GetReflection();
     367          19 :     const Descriptor* descriptor = message->GetDescriptor();
     368             : 
     369             :     string field_name;
     370             : 
     371         112 :     const FieldDescriptor* field = NULL;
     372          19 :     int start_line = tokenizer_.current().line;
     373          19 :     int start_column = tokenizer_.current().column;
     374             : 
     375             :     const FieldDescriptor* any_type_url_field;
     376             :     const FieldDescriptor* any_value_field;
     377          38 :     if (internal::GetAnyFieldDescriptors(*message, &any_type_url_field,
     378          19 :                                          &any_value_field) &&
     379          19 :         TryConsume("[")) {
     380             :       string full_type_name;
     381           0 :       DO(ConsumeAnyTypeUrl(&full_type_name));
     382           0 :       DO(Consume("]"));
     383             :       string serialized_value;
     384           0 :       DO(ConsumeAnyValue(full_type_name,
     385             :                          message->GetDescriptor()->file()->pool(),
     386             :                          &serialized_value));
     387             :       reflection->SetString(
     388             :           message, any_type_url_field,
     389           0 :           string(internal::kTypeGoogleApisComPrefix) + full_type_name);
     390           0 :       reflection->SetString(message, any_value_field, serialized_value);
     391             :       return true;
     392             :       // Fall through.
     393             :     }
     394          38 :     if (TryConsume("[")) {
     395             :       // Extension.
     396           2 :       DO(ConsumeFullTypeName(&field_name));
     397           4 :       DO(Consume("]"));
     398             : 
     399           2 :       field = (finder_ != NULL
     400           2 :                ? finder_->FindExtension(message, field_name)
     401           4 :                : reflection->FindKnownExtensionByName(field_name));
     402             : 
     403           2 :       if (field == NULL) {
     404           0 :         if (!allow_unknown_field_) {
     405           0 :           ReportError("Extension \"" + field_name + "\" is not defined or "
     406           0 :                       "is not an extension of \"" +
     407           0 :                       descriptor->full_name() + "\".");
     408           0 :           return false;
     409             :         } else {
     410           0 :           ReportWarning("Extension \"" + field_name + "\" is not defined or "
     411           0 :                         "is not an extension of \"" +
     412           0 :                         descriptor->full_name() + "\".");
     413             :         }
     414             :       }
     415             :     } else {
     416          17 :       DO(ConsumeIdentifier(&field_name));
     417             : 
     418             :       int32 field_number;
     419          17 :       if (allow_field_number_ && safe_strto32(field_name, &field_number)) {
     420           0 :         if (descriptor->IsExtensionNumber(field_number)) {
     421           0 :           field = reflection->FindKnownExtensionByNumber(field_number);
     422             :         } else {
     423           0 :           field = descriptor->FindFieldByNumber(field_number);
     424             :         }
     425             :       } else {
     426          17 :         field = descriptor->FindFieldByName(field_name);
     427             :         // Group names are expected to be capitalized as they appear in the
     428             :         // .proto file, which actually matches their type names, not their
     429             :         // field names.
     430          17 :         if (field == NULL) {
     431           0 :           string lower_field_name = field_name;
     432           0 :           LowerString(&lower_field_name);
     433           0 :           field = descriptor->FindFieldByName(lower_field_name);
     434             :           // If the case-insensitive match worked but the field is NOT a group,
     435           0 :           if (field != NULL && field->type() != FieldDescriptor::TYPE_GROUP) {
     436           0 :             field = NULL;
     437             :           }
     438             :         }
     439             :         // Again, special-case group names as described above.
     440          34 :         if (field != NULL && field->type() == FieldDescriptor::TYPE_GROUP
     441          17 :             && field->message_type()->name() != field_name) {
     442           0 :           field = NULL;
     443             :         }
     444             : 
     445          17 :         if (field == NULL && allow_case_insensitive_field_) {
     446           0 :           string lower_field_name = field_name;
     447           0 :           LowerString(&lower_field_name);
     448           0 :           field = descriptor->FindFieldByLowercaseName(lower_field_name);
     449             :         }
     450             :       }
     451             : 
     452          17 :       if (field == NULL) {
     453           0 :         if (!allow_unknown_field_) {
     454           0 :           ReportError("Message type \"" + descriptor->full_name() +
     455           0 :                       "\" has no field named \"" + field_name + "\".");
     456           0 :           return false;
     457             :         } else {
     458           0 :           ReportWarning("Message type \"" + descriptor->full_name() +
     459           0 :                         "\" has no field named \"" + field_name + "\".");
     460             :         }
     461             :       }
     462             :     }
     463             : 
     464             :     // Skips unknown field.
     465          19 :     if (field == NULL) {
     466           0 :       GOOGLE_CHECK(allow_unknown_field_);
     467             :       // Try to guess the type of this field.
     468             :       // If this field is not a message, there should be a ":" between the
     469             :       // field name and the field value and also the field value should not
     470             :       // start with "{" or "<" which indicates the beginning of a message body.
     471             :       // If there is no ":" or there is a "{" or "<" after ":", this field has
     472             :       // to be a message or the input is ill-formed.
     473           0 :       if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
     474           0 :         return SkipFieldValue();
     475             :       } else {
     476           0 :         return SkipFieldMessage();
     477             :       }
     478             :     }
     479             : 
     480          19 :     if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) {
     481             :       // Fail if the field is not repeated and it has already been specified.
     482          19 :       if (!field->is_repeated() && reflection->HasField(*message, field)) {
     483           0 :         ReportError("Non-repeated field \"" + field_name +
     484             :                     "\" is specified multiple times.");
     485           0 :         return false;
     486             :       }
     487             :       // Fail if the field is a member of a oneof and another member has already
     488             :       // been specified.
     489          19 :       const OneofDescriptor* oneof = field->containing_oneof();
     490          19 :       if (oneof != NULL && reflection->HasOneof(*message, oneof)) {
     491           0 :         const FieldDescriptor* other_field =
     492           0 :             reflection->GetOneofFieldDescriptor(*message, oneof);
     493           0 :         ReportError("Field \"" + field_name + "\" is specified along with "
     494           0 :                     "field \"" + other_field->name() + "\", another member "
     495           0 :                     "of oneof \"" + oneof->name() + "\".");
     496           0 :         return false;
     497             :       }
     498             :     }
     499             : 
     500             :     // Perform special handling for embedded message types.
     501          38 :     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
     502             :       // ':' is optional here.
     503          10 :       TryConsume(":");
     504             :     } else {
     505             :       // ':' is required here.
     506          28 :       DO(Consume(":"));
     507             :     }
     508             : 
     509          38 :     if (field->is_repeated() && TryConsume("[")) {
     510             :       // Short repeated format, e.g.  "foo: [1, 2, 3]"
     511             :       while (true) {
     512           0 :         if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
     513             :           // Perform special handling for embedded message types.
     514           0 :           DO(ConsumeFieldMessage(message, reflection, field));
     515             :         } else {
     516           0 :           DO(ConsumeFieldValue(message, reflection, field));
     517             :         }
     518           0 :         if (TryConsume("]")) {
     519             :           break;
     520             :         }
     521           0 :         DO(Consume(","));
     522             :       }
     523          38 :     } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
     524           5 :       DO(ConsumeFieldMessage(message, reflection, field));
     525             :     } else {
     526          14 :       DO(ConsumeFieldValue(message, reflection, field));
     527             :     }
     528             : 
     529             :     // For historical reasons, fields may optionally be separated by commas or
     530             :     // semicolons.
     531          57 :     TryConsume(";") || TryConsume(",");
     532             : 
     533          19 :     if (field->options().deprecated()) {
     534             :       ReportWarning("text format contains deprecated field \""
     535           0 :                     + field_name + "\"");
     536             :     }
     537             : 
     538             :     // If a parse info tree exists, add the location for the parsed
     539             :     // field.
     540          19 :     if (parse_info_tree_ != NULL) {
     541             :       RecordLocation(parse_info_tree_, field,
     542           0 :                      ParseLocation(start_line, start_column));
     543             :     }
     544             : 
     545             :     return true;
     546             :   }
     547             : 
     548             :   // Skips the next field including the field's name and value.
     549           0 :   bool SkipField() {
     550             :     string field_name;
     551           0 :     if (TryConsume("[")) {
     552             :       // Extension name.
     553           0 :       DO(ConsumeFullTypeName(&field_name));
     554           0 :       DO(Consume("]"));
     555             :     } else {
     556           0 :       DO(ConsumeIdentifier(&field_name));
     557             :     }
     558             : 
     559             :     // Try to guess the type of this field.
     560             :     // If this field is not a message, there should be a ":" between the
     561             :     // field name and the field value and also the field value should not
     562             :     // start with "{" or "<" which indicates the beginning of a message body.
     563             :     // If there is no ":" or there is a "{" or "<" after ":", this field has
     564             :     // to be a message or the input is ill-formed.
     565           0 :     if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
     566           0 :       DO(SkipFieldValue());
     567             :     } else {
     568           0 :       DO(SkipFieldMessage());
     569             :     }
     570             :     // For historical reasons, fields may optionally be separated by commas or
     571             :     // semicolons.
     572           0 :     TryConsume(";") || TryConsume(",");
     573           0 :     return true;
     574             :   }
     575             : 
     576           5 :   bool ConsumeFieldMessage(Message* message,
     577             :                            const Reflection* reflection,
     578             :                            const FieldDescriptor* field) {
     579             : 
     580             :     // If the parse information tree is not NULL, create a nested one
     581             :     // for the nested message.
     582           5 :     ParseInfoTree* parent = parse_info_tree_;
     583           5 :     if (parent != NULL) {
     584           0 :       parse_info_tree_ = CreateNested(parent, field);
     585             :     }
     586             : 
     587             :     string delimiter;
     588           5 :     DO(ConsumeMessageDelimiter(&delimiter));
     589           5 :     if (field->is_repeated()) {
     590           0 :       DO(ConsumeMessage(reflection->AddMessage(message, field), delimiter));
     591             :     } else {
     592          10 :       DO(ConsumeMessage(reflection->MutableMessage(message, field),
     593             :                         delimiter));
     594             :     }
     595             : 
     596             :     // Reset the parse information tree.
     597           5 :     parse_info_tree_ = parent;
     598           5 :     return true;
     599             :   }
     600             : 
     601             :   // Skips the whole body of a message including the beginning delimiter and
     602             :   // the ending delimiter.
     603           0 :   bool SkipFieldMessage() {
     604             :     string delimiter;
     605           0 :     DO(ConsumeMessageDelimiter(&delimiter));
     606           0 :     while (!LookingAt(">") &&  !LookingAt("}")) {
     607           0 :       DO(SkipField());
     608             :     }
     609           0 :     DO(Consume(delimiter));
     610           0 :     return true;
     611             :   }
     612             : 
     613          14 :   bool ConsumeFieldValue(Message* message,
     614             :                          const Reflection* reflection,
     615          14 :                          const FieldDescriptor* field) {
     616             : 
     617             : // Define an easy to use macro for setting fields. This macro checks
     618             : // to see if the field is repeated (in which case we need to use the Add
     619             : // methods or not (in which case we need to use the Set methods).
     620             : #define SET_FIELD(CPPTYPE, VALUE)                                  \
     621             :         if (field->is_repeated()) {                                \
     622             :           reflection->Add##CPPTYPE(message, field, VALUE);         \
     623             :         } else {                                                   \
     624             :           reflection->Set##CPPTYPE(message, field, VALUE);         \
     625             :         }                                                          \
     626             : 
     627          28 :     switch(field->cpp_type()) {
     628             :       case FieldDescriptor::CPPTYPE_INT32: {
     629             :         int64 value;
     630           4 :         DO(ConsumeSignedInteger(&value, kint32max));
     631           4 :         SET_FIELD(Int32, static_cast<int32>(value));
     632           4 :         break;
     633             :       }
     634             : 
     635             :       case FieldDescriptor::CPPTYPE_UINT32: {
     636             :         uint64 value;
     637           0 :         DO(ConsumeUnsignedInteger(&value, kuint32max));
     638           0 :         SET_FIELD(UInt32, static_cast<uint32>(value));
     639           0 :         break;
     640             :       }
     641             : 
     642             :       case FieldDescriptor::CPPTYPE_INT64: {
     643             :         int64 value;
     644           0 :         DO(ConsumeSignedInteger(&value, kint64max));
     645           0 :         SET_FIELD(Int64, value);
     646           0 :         break;
     647             :       }
     648             : 
     649             :       case FieldDescriptor::CPPTYPE_UINT64: {
     650             :         uint64 value;
     651           0 :         DO(ConsumeUnsignedInteger(&value, kuint64max));
     652           0 :         SET_FIELD(UInt64, value);
     653           0 :         break;
     654             :       }
     655             : 
     656             :       case FieldDescriptor::CPPTYPE_FLOAT: {
     657             :         double value;
     658           0 :         DO(ConsumeDouble(&value));
     659           0 :         SET_FIELD(Float, static_cast<float>(value));
     660           0 :         break;
     661             :       }
     662             : 
     663             :       case FieldDescriptor::CPPTYPE_DOUBLE: {
     664             :         double value;
     665           0 :         DO(ConsumeDouble(&value));
     666           0 :         SET_FIELD(Double, value);
     667           0 :         break;
     668             :       }
     669             : 
     670             :       case FieldDescriptor::CPPTYPE_STRING: {
     671             :         string value;
     672          10 :         DO(ConsumeString(&value));
     673          10 :         SET_FIELD(String, value);
     674             :         break;
     675             :       }
     676             : 
     677             :       case FieldDescriptor::CPPTYPE_BOOL: {
     678           0 :         if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     679             :           uint64 value;
     680           0 :           DO(ConsumeUnsignedInteger(&value, 1));
     681           0 :           SET_FIELD(Bool, value);
     682             :         } else {
     683             :           string value;
     684           0 :           DO(ConsumeIdentifier(&value));
     685           0 :           if (value == "true" || value == "True" || value == "t") {
     686           0 :             SET_FIELD(Bool, true);
     687           0 :           } else if (value == "false" || value == "False" || value == "f") {
     688           0 :             SET_FIELD(Bool, false);
     689             :           } else {
     690           0 :             ReportError("Invalid value for boolean field \"" + field->name()
     691           0 :                         + "\". Value: \"" + value  + "\".");
     692           0 :             return false;
     693             :           }
     694             :         }
     695             :         break;
     696             :       }
     697             : 
     698             :       case FieldDescriptor::CPPTYPE_ENUM: {
     699             :         string value;
     700           0 :         const EnumDescriptor* enum_type = field->enum_type();
     701           0 :         const EnumValueDescriptor* enum_value = NULL;
     702             : 
     703           0 :         if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
     704           0 :           DO(ConsumeIdentifier(&value));
     705             :           // Find the enumeration value.
     706           0 :           enum_value = enum_type->FindValueByName(value);
     707             : 
     708           0 :         } else if (LookingAt("-") ||
     709             :                    LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     710             :           int64 int_value;
     711           0 :           DO(ConsumeSignedInteger(&int_value, kint32max));
     712           0 :           value = SimpleItoa(int_value);        // for error reporting
     713           0 :           enum_value = enum_type->FindValueByNumber(int_value);
     714             :         } else {
     715           0 :           ReportError("Expected integer or identifier.");
     716           0 :           return false;
     717             :         }
     718             : 
     719           0 :         if (enum_value == NULL) {
     720           0 :           if (!allow_unknown_enum_) {
     721           0 :             ReportError("Unknown enumeration value of \"" + value  + "\" for "
     722           0 :                         "field \"" + field->name() + "\".");
     723           0 :             return false;
     724             :           } else {
     725           0 :             ReportWarning("Unknown enumeration value of \"" + value  + "\" for "
     726           0 :                           "field \"" + field->name() + "\".");
     727           0 :             return true;
     728             :           }
     729             :         }
     730             : 
     731           0 :         SET_FIELD(Enum, enum_value);
     732             :         break;
     733             :       }
     734             : 
     735             :       case FieldDescriptor::CPPTYPE_MESSAGE: {
     736             :         // We should never get here. Put here instead of a default
     737             :         // so that if new types are added, we get a nice compiler warning.
     738           0 :         GOOGLE_LOG(FATAL) << "Reached an unintended state: CPPTYPE_MESSAGE";
     739           0 :         break;
     740             :       }
     741             :     }
     742             : #undef SET_FIELD
     743             :     return true;
     744             :   }
     745             : 
     746           0 :   bool SkipFieldValue() {
     747           0 :     if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
     748           0 :       while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
     749           0 :         tokenizer_.Next();
     750             :       }
     751             :       return true;
     752             :     }
     753             :     // Possible field values other than string:
     754             :     //   12345        => TYPE_INTEGER
     755             :     //   -12345       => TYPE_SYMBOL + TYPE_INTEGER
     756             :     //   1.2345       => TYPE_FLOAT
     757             :     //   -1.2345      => TYPE_SYMBOL + TYPE_FLOAT
     758             :     //   inf          => TYPE_IDENTIFIER
     759             :     //   -inf         => TYPE_SYMBOL + TYPE_IDENTIFIER
     760             :     //   TYPE_INTEGER => TYPE_IDENTIFIER
     761             :     // Divides them into two group, one with TYPE_SYMBOL
     762             :     // and the other without:
     763             :     //   Group one:
     764             :     //     12345        => TYPE_INTEGER
     765             :     //     1.2345       => TYPE_FLOAT
     766             :     //     inf          => TYPE_IDENTIFIER
     767             :     //     TYPE_INTEGER => TYPE_IDENTIFIER
     768             :     //   Group two:
     769             :     //     -12345       => TYPE_SYMBOL + TYPE_INTEGER
     770             :     //     -1.2345      => TYPE_SYMBOL + TYPE_FLOAT
     771             :     //     -inf         => TYPE_SYMBOL + TYPE_IDENTIFIER
     772             :     // As we can see, the field value consists of an optional '-' and one of
     773             :     // TYPE_INTEGER, TYPE_FLOAT and TYPE_IDENTIFIER.
     774           0 :     bool has_minus = TryConsume("-");
     775           0 :     if (!LookingAtType(io::Tokenizer::TYPE_INTEGER) &&
     776           0 :         !LookingAtType(io::Tokenizer::TYPE_FLOAT) &&
     777             :         !LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
     778             :       return false;
     779             :     }
     780             :     // Combination of '-' and TYPE_IDENTIFIER may result in an invalid field
     781             :     // value while other combinations all generate valid values.
     782             :     // We check if the value of this combination is valid here.
     783             :     // TYPE_IDENTIFIER after a '-' should be one of the float values listed
     784             :     // below:
     785             :     //   inf, inff, infinity, nan
     786           0 :     if (has_minus && LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
     787           0 :       string text = tokenizer_.current().text;
     788           0 :       LowerString(&text);
     789           0 :       if (text != "inf" &&
     790           0 :           text != "infinity" &&
     791             :           text != "nan") {
     792           0 :         ReportError("Invalid float number: " + text);
     793           0 :         return false;
     794             :       }
     795             :     }
     796           0 :     tokenizer_.Next();
     797           0 :     return true;
     798             :   }
     799             : 
     800             :   // Returns true if the current token's text is equal to that specified.
     801             :   bool LookingAt(const string& text) {
     802          20 :     return tokenizer_.current().text == text;
     803             :   }
     804             : 
     805             :   // Returns true if the current token's type is equal to that specified.
     806             :   bool LookingAtType(io::Tokenizer::TokenType token_type) {
     807          79 :     return tokenizer_.current().type == token_type;
     808             :   }
     809             : 
     810             :   // Consumes an identifier and saves its value in the identifier parameter.
     811             :   // Returns false if the token is not of type IDENTFIER.
     812          22 :   bool ConsumeIdentifier(string* identifier) {
     813          22 :     if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
     814          22 :       *identifier = tokenizer_.current().text;
     815          22 :       tokenizer_.Next();
     816          22 :       return true;
     817             :     }
     818             : 
     819             :     // If allow_field_numer_ or allow_unknown_field_ is true, we should able
     820             :     // to parse integer identifiers.
     821           0 :     if ((allow_field_number_ || allow_unknown_field_)
     822           0 :         && LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     823           0 :       *identifier = tokenizer_.current().text;
     824           0 :       tokenizer_.Next();
     825           0 :       return true;
     826             :     }
     827             : 
     828           0 :     ReportError("Expected identifier.");
     829           0 :     return false;
     830             :   }
     831             : 
     832             :   // Consume a string of form "<id1>.<id2>....<idN>".
     833           2 :   bool ConsumeFullTypeName(string* name) {
     834           2 :     DO(ConsumeIdentifier(name));
     835          10 :     while (TryConsume(".")) {
     836             :       string part;
     837           3 :       DO(ConsumeIdentifier(&part));
     838             :       *name += ".";
     839             :       *name += part;
     840             :     }
     841             :     return true;
     842             :   }
     843             : 
     844             :   // Consumes a string and saves its value in the text parameter.
     845             :   // Returns false if the token is not of type STRING.
     846          10 :   bool ConsumeString(string* text) {
     847          10 :     if (!LookingAtType(io::Tokenizer::TYPE_STRING)) {
     848           0 :       ReportError("Expected string.");
     849           0 :       return false;
     850             :     }
     851             : 
     852             :     text->clear();
     853          20 :     while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
     854          10 :       io::Tokenizer::ParseStringAppend(tokenizer_.current().text, text);
     855             : 
     856          10 :       tokenizer_.Next();
     857             :     }
     858             : 
     859             :     return true;
     860             :   }
     861             : 
     862             :   // Consumes a uint64 and saves its value in the value parameter.
     863             :   // Returns false if the token is not of type INTEGER.
     864           4 :   bool ConsumeUnsignedInteger(uint64* value, uint64 max_value) {
     865           4 :     if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     866           0 :       ReportError("Expected integer.");
     867           0 :       return false;
     868             :     }
     869             : 
     870           8 :     if (!io::Tokenizer::ParseInteger(tokenizer_.current().text,
     871           4 :                                      max_value, value)) {
     872           0 :       ReportError("Integer out of range.");
     873           0 :       return false;
     874             :     }
     875             : 
     876           4 :     tokenizer_.Next();
     877           4 :     return true;
     878             :   }
     879             : 
     880             :   // Consumes an int64 and saves its value in the value parameter.
     881             :   // Note that since the tokenizer does not support negative numbers,
     882             :   // we actually may consume an additional token (for the minus sign) in this
     883             :   // method. Returns false if the token is not an integer
     884             :   // (signed or otherwise).
     885           4 :   bool ConsumeSignedInteger(int64* value, uint64 max_value) {
     886           4 :     bool negative = false;
     887             : 
     888           8 :     if (TryConsume("-")) {
     889           0 :       negative = true;
     890             :       // Two's complement always allows one more negative integer than
     891             :       // positive.
     892           0 :       ++max_value;
     893             :     }
     894             : 
     895             :     uint64 unsigned_value;
     896             : 
     897           4 :     DO(ConsumeUnsignedInteger(&unsigned_value, max_value));
     898             : 
     899           4 :     *value = static_cast<int64>(unsigned_value);
     900             : 
     901           4 :     if (negative) {
     902           0 :       *value = -*value;
     903             :     }
     904             : 
     905             :     return true;
     906             :   }
     907             : 
     908             :   // Consumes a uint64 and saves its value in the value parameter.
     909             :   // Accepts decimal numbers only, rejects hex or oct numbers.
     910           0 :   bool ConsumeUnsignedDecimalInteger(uint64* value, uint64 max_value) {
     911           0 :     if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     912           0 :       ReportError("Expected integer.");
     913           0 :       return false;
     914             :     }
     915             : 
     916           0 :     const string& text = tokenizer_.current().text;
     917           0 :     if (IsHexNumber(text) || IsOctNumber(text)) {
     918           0 :       ReportError("Expect a decimal number.");
     919           0 :       return false;
     920             :     }
     921             : 
     922           0 :     if (!io::Tokenizer::ParseInteger(text, max_value, value)) {
     923           0 :       ReportError("Integer out of range.");
     924           0 :       return false;
     925             :     }
     926             : 
     927           0 :     tokenizer_.Next();
     928           0 :     return true;
     929             :   }
     930             : 
     931             :   // Consumes a double and saves its value in the value parameter.
     932             :   // Note that since the tokenizer does not support negative numbers,
     933             :   // we actually may consume an additional token (for the minus sign) in this
     934             :   // method. Returns false if the token is not a double
     935             :   // (signed or otherwise).
     936           0 :   bool ConsumeDouble(double* value) {
     937           0 :     bool negative = false;
     938             : 
     939           0 :     if (TryConsume("-")) {
     940           0 :       negative = true;
     941             :     }
     942             : 
     943             :     // A double can actually be an integer, according to the tokenizer.
     944             :     // Therefore, we must check both cases here.
     945           0 :     if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
     946             :       // We have found an integer value for the double.
     947             :       uint64 integer_value;
     948           0 :       DO(ConsumeUnsignedDecimalInteger(&integer_value, kuint64max));
     949             : 
     950           0 :       *value = static_cast<double>(integer_value);
     951           0 :     } else if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) {
     952             :       // We have found a float value for the double.
     953           0 :       *value = io::Tokenizer::ParseFloat(tokenizer_.current().text);
     954             : 
     955             :       // Mark the current token as consumed.
     956           0 :       tokenizer_.Next();
     957           0 :     } else if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
     958           0 :       string text = tokenizer_.current().text;
     959           0 :       LowerString(&text);
     960           0 :       if (text == "inf" ||
     961             :           text == "infinity") {
     962           0 :         *value = std::numeric_limits<double>::infinity();
     963           0 :         tokenizer_.Next();
     964           0 :       } else if (text == "nan") {
     965           0 :         *value = std::numeric_limits<double>::quiet_NaN();
     966           0 :         tokenizer_.Next();
     967             :       } else {
     968           0 :         ReportError("Expected double.");
     969           0 :         return false;
     970             :       }
     971             :     } else {
     972           0 :       ReportError("Expected double.");
     973           0 :       return false;
     974             :     }
     975             : 
     976           0 :     if (negative) {
     977           0 :       *value = -*value;
     978             :     }
     979             : 
     980             :     return true;
     981             :   }
     982             : 
     983             :   // Consumes Any::type_url value, of form "type.googleapis.com/full.type.Name"
     984           0 :   bool ConsumeAnyTypeUrl(string* full_type_name) {
     985             :     // TODO(saito) Extend Consume() to consume multiple tokens at once, so that
     986             :     // this code can be written as just DO(Consume(kGoogleApisTypePrefix)).
     987             :     string url1, url2, url3;
     988           0 :     DO(ConsumeIdentifier(&url1));  // type
     989           0 :     DO(Consume("."));
     990           0 :     DO(ConsumeIdentifier(&url2));  // googleapis
     991           0 :     DO(Consume("."));
     992           0 :     DO(ConsumeIdentifier(&url3));  // com
     993           0 :     DO(Consume("/"));
     994           0 :     DO(ConsumeFullTypeName(full_type_name));
     995             : 
     996           0 :     const string prefix = url1 + "." + url2 + "." + url3 + "/";
     997           0 :     if (prefix != internal::kTypeGoogleApisComPrefix) {
     998             :       ReportError("TextFormat::Parser for Any supports only "
     999           0 :                   "type.googleapi.com, but found \"" + prefix + "\"");
    1000           0 :       return false;
    1001             :     }
    1002             :     return true;
    1003             :   }
    1004             : 
    1005             :   // A helper function for reconstructing Any::value. Consumes a text of
    1006             :   // full_type_name, then serializes it into serialized_value. "pool" is used to
    1007             :   // look up and create a temporary object with full_type_name.
    1008           0 :   bool ConsumeAnyValue(const string& full_type_name, const DescriptorPool* pool,
    1009             :                        string* serialized_value) {
    1010             :     const Descriptor* value_descriptor =
    1011           0 :         pool->FindMessageTypeByName(full_type_name);
    1012           0 :     if (value_descriptor == NULL) {
    1013           0 :       ReportError("Could not find type \"" + full_type_name +
    1014             :                   "\" stored in google.protobuf.Any.");
    1015           0 :       return false;
    1016             :     }
    1017           0 :     DynamicMessageFactory factory;
    1018           0 :     const Message* value_prototype = factory.GetPrototype(value_descriptor);
    1019           0 :     if (value_prototype == NULL) {
    1020             :       return false;
    1021             :     }
    1022           0 :     google::protobuf::scoped_ptr<Message> value(value_prototype->New());
    1023             :     string sub_delimiter;
    1024           0 :     DO(ConsumeMessageDelimiter(&sub_delimiter));
    1025           0 :     DO(ConsumeMessage(value.get(), sub_delimiter));
    1026             : 
    1027           0 :     value->AppendToString(serialized_value);
    1028           0 :     return true;
    1029             :   }
    1030             : 
    1031             :   // Consumes a token and confirms that it matches that specified in the
    1032             :   // value parameter. Returns false if the token found does not match that
    1033             :   // which was specified.
    1034          26 :   bool Consume(const string& value) {
    1035          26 :     const string& current_value = tokenizer_.current().text;
    1036             : 
    1037          26 :     if (current_value != value) {
    1038           0 :       ReportError("Expected \"" + value + "\", found \"" + current_value
    1039           0 :                   + "\".");
    1040           0 :       return false;
    1041             :     }
    1042             : 
    1043          26 :     tokenizer_.Next();
    1044             : 
    1045          26 :     return true;
    1046             :   }
    1047             : 
    1048             :   // Attempts to consume the supplied value. Returns false if a the
    1049             :   // token found does not match the value specified.
    1050          76 :   bool TryConsume(const string& value) {
    1051          76 :     if (tokenizer_.current().text == value) {
    1052           5 :       tokenizer_.Next();
    1053           5 :       return true;
    1054             :     } else {
    1055             :       return false;
    1056             :     }
    1057             :   }
    1058             : 
    1059             :   // An internal instance of the Tokenizer's error collector, used to
    1060             :   // collect any base-level parse errors and feed them to the ParserImpl.
    1061             :   class ParserErrorCollector : public io::ErrorCollector {
    1062             :    public:
    1063             :     explicit ParserErrorCollector(TextFormat::Parser::ParserImpl* parser) :
    1064          18 :         parser_(parser) { }
    1065             : 
    1066           9 :     virtual ~ParserErrorCollector() { }
    1067             : 
    1068           0 :     virtual void AddError(int line, int column, const string& message) {
    1069           0 :       parser_->ReportError(line, column, message);
    1070           0 :     }
    1071             : 
    1072           0 :     virtual void AddWarning(int line, int column, const string& message) {
    1073           0 :       parser_->ReportWarning(line, column, message);
    1074           0 :     }
    1075             : 
    1076             :    private:
    1077             :     GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserErrorCollector);
    1078             :     TextFormat::Parser::ParserImpl* parser_;
    1079             :   };
    1080             : 
    1081             :   io::ErrorCollector* error_collector_;
    1082             :   TextFormat::Finder* finder_;
    1083             :   ParseInfoTree* parse_info_tree_;
    1084             :   ParserErrorCollector tokenizer_error_collector_;
    1085             :   io::Tokenizer tokenizer_;
    1086             :   const Descriptor* root_message_type_;
    1087             :   SingularOverwritePolicy singular_overwrite_policy_;
    1088             :   const bool allow_case_insensitive_field_;
    1089             :   const bool allow_unknown_field_;
    1090             :   const bool allow_unknown_enum_;
    1091             :   const bool allow_field_number_;
    1092             :   bool had_errors_;
    1093             : };
    1094             : 
    1095             : #undef DO
    1096             : 
    1097             : // ===========================================================================
    1098             : // Internal class for writing text to the io::ZeroCopyOutputStream. Adapted
    1099             : // from the Printer found in //google/protobuf/io/printer.h
    1100             : class TextFormat::Printer::TextGenerator {
    1101             :  public:
    1102        1020 :   explicit TextGenerator(io::ZeroCopyOutputStream* output,
    1103             :                          int initial_indent_level)
    1104             :     : output_(output),
    1105             :       buffer_(NULL),
    1106             :       buffer_size_(0),
    1107             :       at_start_of_line_(true),
    1108             :       failed_(false),
    1109             :       indent_(""),
    1110        1020 :       initial_indent_level_(initial_indent_level) {
    1111        1020 :     indent_.resize(initial_indent_level_ * 2, ' ');
    1112        1020 :   }
    1113             : 
    1114        2040 :   ~TextGenerator() {
    1115             :     // Only BackUp() if we're sure we've successfully called Next() at least
    1116             :     // once.
    1117        1020 :     if (!failed_ && buffer_size_ > 0) {
    1118        1020 :       output_->BackUp(buffer_size_);
    1119             :     }
    1120        1020 :   }
    1121             : 
    1122             :   // Indent text by two spaces.  After calling Indent(), two spaces will be
    1123             :   // inserted at the beginning of each line of text.  Indent() may be called
    1124             :   // multiple times to produce deeper indents.
    1125             :   void Indent() {
    1126           0 :     indent_ += "  ";
    1127             :   }
    1128             : 
    1129             :   // Reduces the current indent level by two spaces, or crashes if the indent
    1130             :   // level is zero.
    1131           0 :   void Outdent() {
    1132           0 :     if (indent_.empty() ||
    1133           0 :         indent_.size() < initial_indent_level_ * 2) {
    1134           0 :       GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
    1135           0 :       return;
    1136             :     }
    1137             : 
    1138           0 :     indent_.resize(indent_.size() - 2);
    1139             :   }
    1140             : 
    1141             :   // Print text to the output stream.
    1142             :   void Print(const string& str) {
    1143        1020 :     Print(str.data(), str.size());
    1144             :   }
    1145             : 
    1146             :   // Print text to the output stream.
    1147           0 :   void Print(const char* text) {
    1148           0 :     Print(text, strlen(text));
    1149           0 :   }
    1150             : 
    1151             :   // Print text to the output stream.
    1152        1020 :   void Print(const char* text, int size) {
    1153        1020 :     int pos = 0;  // The number of bytes we've written so far.
    1154             : 
    1155        6455 :     for (int i = 0; i < size; i++) {
    1156        5435 :       if (text[i] == '\n') {
    1157             :         // Saw newline.  If there is more text, we may need to insert an indent
    1158             :         // here.  So, write what we have so far, including the '\n'.
    1159           0 :         Write(text + pos, i - pos + 1);
    1160           0 :         pos = i + 1;
    1161             : 
    1162             :         // Setting this true will cause the next Write() to insert an indent
    1163             :         // first.
    1164           0 :         at_start_of_line_ = true;
    1165             :       }
    1166             :     }
    1167             : 
    1168             :     // Write the rest.
    1169        1020 :     Write(text + pos, size - pos);
    1170        1020 :   }
    1171             : 
    1172             :   // True if any write to the underlying stream failed.  (We don't just
    1173             :   // crash in this case because this is an I/O failure, not a programming
    1174             :   // error.)
    1175             :   bool failed() const { return failed_; }
    1176             : 
    1177             :  private:
    1178             :   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextGenerator);
    1179             : 
    1180        2040 :   void Write(const char* data, int size) {
    1181        2040 :     if (failed_) return;
    1182        2040 :     if (size == 0) return;
    1183             : 
    1184        1020 :     if (at_start_of_line_) {
    1185             :       // Insert an indent.
    1186        1020 :       at_start_of_line_ = false;
    1187        2040 :       Write(indent_.data(), indent_.size());
    1188        1020 :       if (failed_) return;
    1189             :     }
    1190             : 
    1191        2040 :     while (size > buffer_size_) {
    1192             :       // Data exceeds space in the buffer.  Copy what we can and request a
    1193             :       // new buffer.
    1194        1020 :       memcpy(buffer_, data, buffer_size_);
    1195        1020 :       data += buffer_size_;
    1196        1020 :       size -= buffer_size_;
    1197             :       void* void_buffer;
    1198        1020 :       failed_ = !output_->Next(&void_buffer, &buffer_size_);
    1199        1020 :       if (failed_) return;
    1200        1020 :       buffer_ = reinterpret_cast<char*>(void_buffer);
    1201             :     }
    1202             : 
    1203             :     // Buffer is big enough to receive the data; copy it.
    1204        1020 :     memcpy(buffer_, data, size);
    1205        1020 :     buffer_ += size;
    1206        1020 :     buffer_size_ -= size;
    1207             :   }
    1208             : 
    1209             :   io::ZeroCopyOutputStream* const output_;
    1210             :   char* buffer_;
    1211             :   int buffer_size_;
    1212             :   bool at_start_of_line_;
    1213             :   bool failed_;
    1214             : 
    1215             :   string indent_;
    1216             :   int initial_indent_level_;
    1217             : };
    1218             : 
    1219             : // ===========================================================================
    1220             : 
    1221           9 : TextFormat::Finder::~Finder() {
    1222           9 : }
    1223             : 
    1224           9 : TextFormat::Parser::Parser()
    1225             :   : error_collector_(NULL),
    1226             :     finder_(NULL),
    1227             :     parse_info_tree_(NULL),
    1228             :     allow_partial_(false),
    1229             :     allow_case_insensitive_field_(false),
    1230             :     allow_unknown_field_(false),
    1231             :     allow_unknown_enum_(false),
    1232             :     allow_field_number_(false),
    1233             :     allow_relaxed_whitespace_(false),
    1234           9 :     allow_singular_overwrites_(false) {
    1235           9 : }
    1236             : 
    1237           9 : TextFormat::Parser::~Parser() {}
    1238             : 
    1239           9 : bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input,
    1240             :                                Message* output) {
    1241           9 :   output->Clear();
    1242             : 
    1243             :   ParserImpl::SingularOverwritePolicy overwrites_policy =
    1244             :       allow_singular_overwrites_
    1245             :       ? ParserImpl::ALLOW_SINGULAR_OVERWRITES
    1246           9 :       : ParserImpl::FORBID_SINGULAR_OVERWRITES;
    1247             : 
    1248             :   ParserImpl parser(output->GetDescriptor(), input, error_collector_,
    1249             :                     finder_, parse_info_tree_,
    1250             :                     overwrites_policy,
    1251             :                     allow_case_insensitive_field_, allow_unknown_field_,
    1252             :                     allow_unknown_enum_, allow_field_number_,
    1253          18 :                     allow_relaxed_whitespace_);
    1254           9 :   return MergeUsingImpl(input, output, &parser);
    1255             : }
    1256             : 
    1257           9 : bool TextFormat::Parser::ParseFromString(const string& input,
    1258             :                                          Message* output) {
    1259           9 :   io::ArrayInputStream input_stream(input.data(), input.size());
    1260           9 :   return Parse(&input_stream, output);
    1261             : }
    1262             : 
    1263           0 : bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input,
    1264             :                                Message* output) {
    1265             :   ParserImpl parser(output->GetDescriptor(), input, error_collector_,
    1266             :                     finder_, parse_info_tree_,
    1267             :                     ParserImpl::ALLOW_SINGULAR_OVERWRITES,
    1268             :                     allow_case_insensitive_field_, allow_unknown_field_,
    1269             :                     allow_unknown_enum_, allow_field_number_,
    1270           0 :                     allow_relaxed_whitespace_);
    1271           0 :   return MergeUsingImpl(input, output, &parser);
    1272             : }
    1273             : 
    1274           0 : bool TextFormat::Parser::MergeFromString(const string& input,
    1275             :                                          Message* output) {
    1276           0 :   io::ArrayInputStream input_stream(input.data(), input.size());
    1277           0 :   return Merge(&input_stream, output);
    1278             : }
    1279             : 
    1280           9 : bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */,
    1281             :                                         Message* output,
    1282             :                                         ParserImpl* parser_impl) {
    1283           9 :   if (!parser_impl->Parse(output)) return false;
    1284           9 :   if (!allow_partial_ && !output->IsInitialized()) {
    1285             :     vector<string> missing_fields;
    1286           0 :     output->FindInitializationErrors(&missing_fields);
    1287           0 :     parser_impl->ReportError(-1, 0, "Message missing required fields: " +
    1288           0 :                                         Join(missing_fields, ", "));
    1289           0 :     return false;
    1290             :   }
    1291             :   return true;
    1292             : }
    1293             : 
    1294           0 : bool TextFormat::Parser::ParseFieldValueFromString(
    1295             :     const string& input,
    1296             :     const FieldDescriptor* field,
    1297             :     Message* output) {
    1298           0 :   io::ArrayInputStream input_stream(input.data(), input.size());
    1299             :   ParserImpl parser(output->GetDescriptor(), &input_stream, error_collector_,
    1300             :                     finder_, parse_info_tree_,
    1301             :                     ParserImpl::ALLOW_SINGULAR_OVERWRITES,
    1302             :                     allow_case_insensitive_field_, allow_unknown_field_,
    1303             :                     allow_unknown_enum_, allow_field_number_,
    1304           0 :                     allow_relaxed_whitespace_);
    1305           0 :   return parser.ParseField(field, output);
    1306             : }
    1307             : 
    1308           0 : /* static */ bool TextFormat::Parse(io::ZeroCopyInputStream* input,
    1309             :                                     Message* output) {
    1310           0 :   return Parser().Parse(input, output);
    1311             : }
    1312             : 
    1313           0 : /* static */ bool TextFormat::Merge(io::ZeroCopyInputStream* input,
    1314             :                                     Message* output) {
    1315           0 :   return Parser().Merge(input, output);
    1316             : }
    1317             : 
    1318           0 : /* static */ bool TextFormat::ParseFromString(const string& input,
    1319             :                                               Message* output) {
    1320           0 :   return Parser().ParseFromString(input, output);
    1321             : }
    1322             : 
    1323           0 : /* static */ bool TextFormat::MergeFromString(const string& input,
    1324             :                                               Message* output) {
    1325           0 :   return Parser().MergeFromString(input, output);
    1326             : }
    1327             : 
    1328             : // ===========================================================================
    1329             : 
    1330             : // The default implementation for FieldValuePrinter. The base class just
    1331             : // does simple formatting. That way, deriving classes could decide to fallback
    1332             : // to that behavior.
    1333        1020 : TextFormat::FieldValuePrinter::FieldValuePrinter() {}
    1334        2040 : TextFormat::FieldValuePrinter::~FieldValuePrinter() {}
    1335         725 : string TextFormat::FieldValuePrinter::PrintBool(bool val) const {
    1336         725 :   return val ? "true" : "false";
    1337             : }
    1338           0 : string TextFormat::FieldValuePrinter::PrintInt32(int32 val) const {
    1339           0 :   return SimpleItoa(val);
    1340             : }
    1341           0 : string TextFormat::FieldValuePrinter::PrintUInt32(uint32 val) const {
    1342           0 :   return SimpleItoa(val);
    1343             : }
    1344           0 : string TextFormat::FieldValuePrinter::PrintInt64(int64 val) const {
    1345           0 :   return SimpleItoa(val);
    1346             : }
    1347           0 : string TextFormat::FieldValuePrinter::PrintUInt64(uint64 val) const {
    1348           0 :   return SimpleItoa(val);
    1349             : }
    1350           0 : string TextFormat::FieldValuePrinter::PrintFloat(float val) const {
    1351           0 :   return SimpleFtoa(val);
    1352             : }
    1353           0 : string TextFormat::FieldValuePrinter::PrintDouble(double val) const {
    1354           0 :   return SimpleDtoa(val);
    1355             : }
    1356           0 : string TextFormat::FieldValuePrinter::PrintString(const string& val) const {
    1357           0 :   return StrCat("\"", CEscape(val), "\"");
    1358             : }
    1359           0 : string TextFormat::FieldValuePrinter::PrintBytes(const string& val) const {
    1360           0 :   return PrintString(val);
    1361             : }
    1362         295 : string TextFormat::FieldValuePrinter::PrintEnum(int32 val,
    1363             :                                                 const string& name) const {
    1364         295 :   return name;
    1365             : }
    1366           0 : string TextFormat::FieldValuePrinter::PrintFieldName(
    1367             :     const Message& message,
    1368             :     const Reflection* reflection,
    1369           0 :     const FieldDescriptor* field) const {
    1370           0 :   if (field->is_extension()) {
    1371             :     // We special-case MessageSet elements for compatibility with proto1.
    1372           0 :     if (field->containing_type()->options().message_set_wire_format()
    1373           0 :         && field->type() == FieldDescriptor::TYPE_MESSAGE
    1374           0 :         && field->is_optional()
    1375           0 :         && field->extension_scope() == field->message_type()) {
    1376           0 :       return StrCat("[", field->message_type()->full_name(), "]");
    1377             :     } else {
    1378           0 :       return StrCat("[", field->full_name(), "]");
    1379             :     }
    1380           0 :   } else if (field->type() == FieldDescriptor::TYPE_GROUP) {
    1381             :     // Groups must be serialized with their original capitalization.
    1382           0 :     return field->message_type()->name();
    1383             :   } else {
    1384           0 :     return field->name();
    1385             :   }
    1386             : }
    1387           0 : string TextFormat::FieldValuePrinter::PrintMessageStart(
    1388             :     const Message& message,
    1389             :     int field_index,
    1390             :     int field_count,
    1391             :     bool single_line_mode) const {
    1392           0 :   return single_line_mode ? " { " : " {\n";
    1393             : }
    1394           0 : string TextFormat::FieldValuePrinter::PrintMessageEnd(
    1395             :     const Message& message,
    1396             :     int field_index,
    1397             :     int field_count,
    1398             :     bool single_line_mode) const {
    1399           0 :   return single_line_mode ? "} " : "}\n";
    1400             : }
    1401             : 
    1402             : namespace {
    1403             : // Our own specialization: for UTF8 escaped strings.
    1404           0 : class FieldValuePrinterUtf8Escaping : public TextFormat::FieldValuePrinter {
    1405             :  public:
    1406           0 :   virtual string PrintString(const string& val) const {
    1407           0 :     return StrCat("\"", strings::Utf8SafeCEscape(val), "\"");
    1408             :   }
    1409           0 :   virtual string PrintBytes(const string& val) const {
    1410           0 :     return TextFormat::FieldValuePrinter::PrintString(val);
    1411             :   }
    1412             : };
    1413             : 
    1414             : }  // namespace
    1415             : 
    1416        1020 : TextFormat::Printer::Printer()
    1417             :   : initial_indent_level_(0),
    1418             :     single_line_mode_(false),
    1419             :     use_field_number_(false),
    1420             :     use_short_repeated_primitives_(false),
    1421             :     hide_unknown_fields_(false),
    1422             :     print_message_fields_in_index_order_(false),
    1423        2040 :     expand_any_(false) {
    1424        1020 :   SetUseUtf8StringEscaping(false);
    1425        1020 : }
    1426             : 
    1427        3060 : TextFormat::Printer::~Printer() {
    1428        1020 :   STLDeleteValues(&custom_printers_);
    1429        1020 : }
    1430             : 
    1431        1020 : void TextFormat::Printer::SetUseUtf8StringEscaping(bool as_utf8) {
    1432             :   SetDefaultFieldValuePrinter(as_utf8
    1433             :                               ? new FieldValuePrinterUtf8Escaping()
    1434        2040 :                               : new FieldValuePrinter());
    1435        1020 : }
    1436             : 
    1437        1020 : void TextFormat::Printer::SetDefaultFieldValuePrinter(
    1438             :     const FieldValuePrinter* printer) {
    1439        1020 :   default_field_value_printer_.reset(printer);
    1440        1020 : }
    1441             : 
    1442           0 : bool TextFormat::Printer::RegisterFieldValuePrinter(
    1443             :     const FieldDescriptor* field,
    1444             :     const FieldValuePrinter* printer) {
    1445           0 :   return field != NULL && printer != NULL &&
    1446           0 :          custom_printers_.insert(std::make_pair(field, printer)).second;
    1447             : }
    1448             : 
    1449           0 : bool TextFormat::Printer::PrintToString(const Message& message,
    1450             :                                         string* output) const {
    1451             :   GOOGLE_DCHECK(output) << "output specified is NULL";
    1452             : 
    1453             :   output->clear();
    1454           0 :   io::StringOutputStream output_stream(output);
    1455             : 
    1456           0 :   return Print(message, &output_stream);
    1457             : }
    1458             : 
    1459           0 : bool TextFormat::Printer::PrintUnknownFieldsToString(
    1460             :     const UnknownFieldSet& unknown_fields,
    1461             :     string* output) const {
    1462             :   GOOGLE_DCHECK(output) << "output specified is NULL";
    1463             : 
    1464             :   output->clear();
    1465           0 :   io::StringOutputStream output_stream(output);
    1466           0 :   return PrintUnknownFields(unknown_fields, &output_stream);
    1467             : }
    1468             : 
    1469           0 : bool TextFormat::Printer::Print(const Message& message,
    1470             :                                 io::ZeroCopyOutputStream* output) const {
    1471           0 :   TextGenerator generator(output, initial_indent_level_);
    1472             : 
    1473           0 :   Print(message, generator);
    1474             : 
    1475             :   // Output false if the generator failed internally.
    1476           0 :   return !generator.failed();
    1477             : }
    1478             : 
    1479           0 : bool TextFormat::Printer::PrintUnknownFields(
    1480             :     const UnknownFieldSet& unknown_fields,
    1481             :     io::ZeroCopyOutputStream* output) const {
    1482           0 :   TextGenerator generator(output, initial_indent_level_);
    1483             : 
    1484           0 :   PrintUnknownFields(unknown_fields, generator);
    1485             : 
    1486             :   // Output false if the generator failed internally.
    1487           0 :   return !generator.failed();
    1488             : }
    1489             : 
    1490             : namespace {
    1491             : // Comparison functor for sorting FieldDescriptors by field index.
    1492             : struct FieldIndexSorter {
    1493             :   bool operator()(const FieldDescriptor* left,
    1494             :                   const FieldDescriptor* right) const {
    1495           0 :     return left->index() < right->index();
    1496             :   }
    1497             : };
    1498             : 
    1499             : }  // namespace
    1500             : 
    1501           0 : bool TextFormat::Printer::PrintAny(const Message& message,
    1502             :                                    TextGenerator& generator) const {
    1503             :   const FieldDescriptor* type_url_field;
    1504             :   const FieldDescriptor* value_field;
    1505           0 :   if (!internal::GetAnyFieldDescriptors(message, &type_url_field,
    1506           0 :                                         &value_field)) {
    1507             :     return false;
    1508             :   }
    1509             : 
    1510           0 :   const Reflection* reflection = message.GetReflection();
    1511             : 
    1512             :   // Extract the full type name from the type_url field.
    1513           0 :   const string& type_url = reflection->GetString(message, type_url_field);
    1514             :   string full_type_name;
    1515           0 :   if (!internal::ParseAnyTypeUrl(type_url, &full_type_name)) {
    1516             :     return false;
    1517             :   }
    1518             : 
    1519             :   // Print the "value" in text.
    1520             :   const google::protobuf::Descriptor* value_descriptor =
    1521             :       message.GetDescriptor()->file()->pool()->FindMessageTypeByName(
    1522           0 :           full_type_name);
    1523           0 :   if (value_descriptor == NULL) {
    1524           0 :     GOOGLE_LOG(WARNING) << "Proto type " << type_url << " not found";
    1525           0 :     return false;
    1526             :   }
    1527           0 :   DynamicMessageFactory factory;
    1528             :   google::protobuf::scoped_ptr<google::protobuf::Message> value_message(
    1529           0 :       factory.GetPrototype(value_descriptor)->New());
    1530           0 :   string serialized_value = reflection->GetString(message, value_field);
    1531           0 :   if (!value_message->ParseFromString(serialized_value)) {
    1532           0 :     GOOGLE_LOG(WARNING) << type_url << ": failed to parse contents";
    1533           0 :     return false;
    1534             :   }
    1535           0 :   generator.Print(StrCat("[", type_url, "]"));
    1536             :   const FieldValuePrinter* printer = FindWithDefault(
    1537           0 :       custom_printers_, value_field, default_field_value_printer_.get());
    1538             :   generator.Print(
    1539           0 :       printer->PrintMessageStart(message, -1, 0, single_line_mode_));
    1540             :   generator.Indent();
    1541           0 :   Print(*value_message, generator);
    1542           0 :   generator.Outdent();
    1543           0 :   generator.Print(printer->PrintMessageEnd(message, -1, 0, single_line_mode_));
    1544           0 :   return true;
    1545             : }
    1546             : 
    1547           0 : void TextFormat::Printer::Print(const Message& message,
    1548             :                                 TextGenerator& generator) const {
    1549           0 :   const Descriptor* descriptor = message.GetDescriptor();
    1550           0 :   const Reflection* reflection = message.GetReflection();
    1551           0 :   if (descriptor->full_name() == internal::kAnyFullTypeName && expand_any_ &&
    1552           0 :       PrintAny(message, generator)) {
    1553           0 :     return;
    1554             :   }
    1555             :   vector<const FieldDescriptor*> fields;
    1556           0 :   reflection->ListFields(message, &fields);
    1557           0 :   if (print_message_fields_in_index_order_) {
    1558           0 :     std::sort(fields.begin(), fields.end(), FieldIndexSorter());
    1559             :   }
    1560           0 :   for (int i = 0; i < fields.size(); i++) {
    1561           0 :     PrintField(message, reflection, fields[i], generator);
    1562             :   }
    1563           0 :   if (!hide_unknown_fields_) {
    1564           0 :     PrintUnknownFields(reflection->GetUnknownFields(message), generator);
    1565             :   }
    1566             : }
    1567             : 
    1568        1020 : void TextFormat::Printer::PrintFieldValueToString(
    1569             :     const Message& message,
    1570             :     const FieldDescriptor* field,
    1571             :     int index,
    1572             :     string* output) const {
    1573             : 
    1574             :   GOOGLE_DCHECK(output) << "output specified is NULL";
    1575             : 
    1576             :   output->clear();
    1577        1020 :   io::StringOutputStream output_stream(output);
    1578        2040 :   TextGenerator generator(&output_stream, initial_indent_level_);
    1579             : 
    1580        2040 :   PrintFieldValue(message, message.GetReflection(), field, index, generator);
    1581        1020 : }
    1582             : 
    1583             : class MapEntryMessageComparator {
    1584             :  public:
    1585           0 :   explicit MapEntryMessageComparator(const Descriptor* descriptor)
    1586           0 :       : field_(descriptor->field(0)) {}
    1587             : 
    1588           0 :   bool operator()(const Message* a, const Message* b) {
    1589           0 :     const Reflection* reflection = a->GetReflection();
    1590           0 :     switch (field_->cpp_type()) {
    1591             :       case FieldDescriptor::CPPTYPE_BOOL: {
    1592           0 :           bool first = reflection->GetBool(*a, field_);
    1593           0 :           bool second = reflection->GetBool(*b, field_);
    1594           0 :           return first < second;
    1595             :       }
    1596             :       case FieldDescriptor::CPPTYPE_INT32: {
    1597           0 :           int32 first = reflection->GetInt32(*a, field_);
    1598           0 :           int32 second = reflection->GetInt32(*b, field_);
    1599           0 :           return first < second;
    1600             :       }
    1601             :       case FieldDescriptor::CPPTYPE_INT64: {
    1602           0 :           int64 first = reflection->GetInt64(*a, field_);
    1603           0 :           int64 second = reflection->GetInt64(*b, field_);
    1604           0 :           return first < second;
    1605             :       }
    1606             :       case FieldDescriptor::CPPTYPE_UINT32: {
    1607           0 :           uint32 first = reflection->GetUInt32(*a, field_);
    1608           0 :           uint32 second = reflection->GetUInt32(*b, field_);
    1609           0 :           return first < second;
    1610             :       }
    1611             :       case FieldDescriptor::CPPTYPE_UINT64: {
    1612           0 :           uint64 first = reflection->GetUInt64(*a, field_);
    1613           0 :           uint64 second = reflection->GetUInt64(*b, field_);
    1614           0 :           return first < second;
    1615             :       }
    1616             :       case FieldDescriptor::CPPTYPE_STRING: {
    1617           0 :           string first = reflection->GetString(*a, field_);
    1618           0 :           string second = reflection->GetString(*b, field_);
    1619           0 :           return first < second;
    1620             :       }
    1621             :       default:
    1622           0 :         GOOGLE_LOG(DFATAL) << "Invalid key for map field.";
    1623           0 :         return true;
    1624             :     }
    1625             :   }
    1626             : 
    1627             :  private:
    1628             :   const FieldDescriptor* field_;
    1629             : };
    1630             : 
    1631           0 : void TextFormat::Printer::PrintField(const Message& message,
    1632             :                                      const Reflection* reflection,
    1633             :                                      const FieldDescriptor* field,
    1634             :                                      TextGenerator& generator) const {
    1635           0 :   if (use_short_repeated_primitives_ &&
    1636           0 :       field->is_repeated() &&
    1637           0 :       field->cpp_type() != FieldDescriptor::CPPTYPE_STRING &&
    1638           0 :       field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
    1639           0 :     PrintShortRepeatedField(message, reflection, field, generator);
    1640           0 :     return;
    1641             :   }
    1642             : 
    1643           0 :   int count = 0;
    1644             : 
    1645           0 :   if (field->is_repeated()) {
    1646           0 :     count = reflection->FieldSize(message, field);
    1647           0 :   } else if (reflection->HasField(message, field)) {
    1648           0 :     count = 1;
    1649             :   }
    1650             : 
    1651             :   std::vector<const Message*> sorted_map_field;
    1652           0 :   if (field->is_map()) {
    1653             :     const RepeatedPtrField<Message>& map_field =
    1654           0 :         reflection->GetRepeatedPtrField<Message>(message, field);
    1655           0 :     for (RepeatedPtrField<Message>::const_pointer_iterator it =
    1656           0 :              map_field.pointer_begin();
    1657           0 :          it != map_field.pointer_end(); ++it) {
    1658           0 :       sorted_map_field.push_back(*it);
    1659             :     }
    1660             : 
    1661           0 :     MapEntryMessageComparator comparator(field->message_type());
    1662             :     std::stable_sort(sorted_map_field.begin(), sorted_map_field.end(),
    1663           0 :                      comparator);
    1664             :   }
    1665             : 
    1666           0 :   for (int j = 0; j < count; ++j) {
    1667           0 :     const int field_index = field->is_repeated() ? j : -1;
    1668             : 
    1669           0 :     PrintFieldName(message, reflection, field, generator);
    1670             : 
    1671           0 :     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
    1672             :       const FieldValuePrinter* printer = FindWithDefault(
    1673           0 :           custom_printers_, field, default_field_value_printer_.get());
    1674             :       const Message& sub_message =
    1675           0 :           field->is_repeated()
    1676           0 :               ? (field->is_map()
    1677           0 :                      ? *sorted_map_field[j]
    1678           0 :                      : reflection->GetRepeatedMessage(message, field, j))
    1679           0 :               : reflection->GetMessage(message, field);
    1680             :       generator.Print(
    1681             :           printer->PrintMessageStart(
    1682           0 :               sub_message, field_index, count, single_line_mode_));
    1683             :       generator.Indent();
    1684           0 :       Print(sub_message, generator);
    1685           0 :       generator.Outdent();
    1686             :       generator.Print(
    1687             :           printer->PrintMessageEnd(
    1688           0 :               sub_message, field_index, count, single_line_mode_));
    1689             :     } else {
    1690           0 :       generator.Print(": ");
    1691             :       // Write the field value.
    1692           0 :       PrintFieldValue(message, reflection, field, field_index, generator);
    1693           0 :       if (single_line_mode_) {
    1694           0 :         generator.Print(" ");
    1695             :       } else {
    1696           0 :         generator.Print("\n");
    1697             :       }
    1698             :     }
    1699             :   }
    1700             : }
    1701             : 
    1702           0 : void TextFormat::Printer::PrintShortRepeatedField(
    1703             :     const Message& message,
    1704             :     const Reflection* reflection,
    1705             :     const FieldDescriptor* field,
    1706             :     TextGenerator& generator) const {
    1707             :   // Print primitive repeated field in short form.
    1708           0 :   PrintFieldName(message, reflection, field, generator);
    1709             : 
    1710           0 :   int size = reflection->FieldSize(message, field);
    1711           0 :   generator.Print(": [");
    1712           0 :   for (int i = 0; i < size; i++) {
    1713           0 :     if (i > 0) generator.Print(", ");
    1714           0 :     PrintFieldValue(message, reflection, field, i, generator);
    1715             :   }
    1716           0 :   if (single_line_mode_) {
    1717           0 :     generator.Print("] ");
    1718             :   } else {
    1719           0 :     generator.Print("]\n");
    1720             :   }
    1721           0 : }
    1722             : 
    1723           0 : void TextFormat::Printer::PrintFieldName(const Message& message,
    1724             :                                          const Reflection* reflection,
    1725             :                                          const FieldDescriptor* field,
    1726             :                                          TextGenerator& generator) const {
    1727             :   // if use_field_number_ is true, prints field number instead
    1728             :   // of field name.
    1729           0 :   if (use_field_number_) {
    1730           0 :     generator.Print(SimpleItoa(field->number()));
    1731           0 :     return;
    1732             :   }
    1733             : 
    1734             :   const FieldValuePrinter* printer = FindWithDefault(
    1735           0 :       custom_printers_, field, default_field_value_printer_.get());
    1736           0 :   generator.Print(printer->PrintFieldName(message, reflection, field));
    1737             : }
    1738             : 
    1739        1020 : void TextFormat::Printer::PrintFieldValue(
    1740             :     const Message& message,
    1741             :     const Reflection* reflection,
    1742             :     const FieldDescriptor* field,
    1743             :     int index,
    1744             :     TextGenerator& generator) const {
    1745        1315 :   GOOGLE_DCHECK(field->is_repeated() || (index == -1))
    1746             :       << "Index must be -1 for non-repeated fields";
    1747             : 
    1748             :   const FieldValuePrinter* printer
    1749             :       = FindWithDefault(custom_printers_, field,
    1750        3060 :                         default_field_value_printer_.get());
    1751             : 
    1752        3060 :   switch (field->cpp_type()) {
    1753             : #define OUTPUT_FIELD(CPPTYPE, METHOD)                                   \
    1754             :     case FieldDescriptor::CPPTYPE_##CPPTYPE:                            \
    1755             :       generator.Print(printer->Print##METHOD(field->is_repeated()       \
    1756             :                ? reflection->GetRepeated##METHOD(message, field, index) \
    1757             :                : reflection->Get##METHOD(message, field)));             \
    1758             :         break
    1759             : 
    1760           0 :     OUTPUT_FIELD( INT32,  Int32);
    1761           0 :     OUTPUT_FIELD( INT64,  Int64);
    1762           0 :     OUTPUT_FIELD(UINT32, UInt32);
    1763           0 :     OUTPUT_FIELD(UINT64, UInt64);
    1764           0 :     OUTPUT_FIELD( FLOAT,  Float);
    1765           0 :     OUTPUT_FIELD(DOUBLE, Double);
    1766        2175 :     OUTPUT_FIELD(  BOOL,   Bool);
    1767             : #undef OUTPUT_FIELD
    1768             : 
    1769             :     case FieldDescriptor::CPPTYPE_STRING: {
    1770             :       string scratch;
    1771           0 :       const string& value = field->is_repeated()
    1772             :           ? reflection->GetRepeatedStringReference(
    1773           0 :               message, field, index, &scratch)
    1774           0 :           : reflection->GetStringReference(message, field, &scratch);
    1775           0 :       if (field->type() == FieldDescriptor::TYPE_STRING) {
    1776           0 :         generator.Print(printer->PrintString(value));
    1777             :       } else {
    1778             :         GOOGLE_DCHECK_EQ(field->type(), FieldDescriptor::TYPE_BYTES);
    1779           0 :         generator.Print(printer->PrintBytes(value));
    1780             :       }
    1781             :       break;
    1782             :     }
    1783             : 
    1784             :     case FieldDescriptor::CPPTYPE_ENUM: {
    1785         295 :       int enum_value = field->is_repeated()
    1786           0 :           ? reflection->GetRepeatedEnumValue(message, field, index)
    1787         295 :           : reflection->GetEnumValue(message, field);
    1788         295 :       const EnumValueDescriptor* enum_desc =
    1789         590 :           field->enum_type()->FindValueByNumber(enum_value);
    1790         295 :       if (enum_desc != NULL) {
    1791         885 :         generator.Print(printer->PrintEnum(enum_value, enum_desc->name()));
    1792             :       } else {
    1793             :         // Ordinarily, enum_desc should not be null, because proto2 has the
    1794             :         // invariant that set enum field values must be in-range, but with the
    1795             :         // new integer-based API for enums (or the RepeatedField<int> loophole),
    1796             :         // it is possible for the user to force an unknown integer value.  So we
    1797             :         // simply use the integer value itself as the enum value name in this
    1798             :         // case.
    1799             :         generator.Print(printer->PrintEnum(enum_value,
    1800           0 :                                            StringPrintf("%d", enum_value)));
    1801             :       }
    1802             :       break;
    1803             :     }
    1804             : 
    1805             :     case FieldDescriptor::CPPTYPE_MESSAGE:
    1806           0 :       Print(field->is_repeated()
    1807           0 :             ? reflection->GetRepeatedMessage(message, field, index)
    1808           0 :             : reflection->GetMessage(message, field),
    1809           0 :             generator);
    1810           0 :       break;
    1811             :   }
    1812        1020 : }
    1813             : 
    1814           0 : /* static */ bool TextFormat::Print(const Message& message,
    1815             :                                     io::ZeroCopyOutputStream* output) {
    1816           0 :   return Printer().Print(message, output);
    1817             : }
    1818             : 
    1819           0 : /* static */ bool TextFormat::PrintUnknownFields(
    1820             :     const UnknownFieldSet& unknown_fields,
    1821             :     io::ZeroCopyOutputStream* output) {
    1822           0 :   return Printer().PrintUnknownFields(unknown_fields, output);
    1823             : }
    1824             : 
    1825           0 : /* static */ bool TextFormat::PrintToString(
    1826             :     const Message& message, string* output) {
    1827           0 :   return Printer().PrintToString(message, output);
    1828             : }
    1829             : 
    1830           0 : /* static */ bool TextFormat::PrintUnknownFieldsToString(
    1831             :     const UnknownFieldSet& unknown_fields, string* output) {
    1832           0 :   return Printer().PrintUnknownFieldsToString(unknown_fields, output);
    1833             : }
    1834             : 
    1835        1020 : /* static */ void TextFormat::PrintFieldValueToString(
    1836             :     const Message& message,
    1837             :     const FieldDescriptor* field,
    1838             :     int index,
    1839             :     string* output) {
    1840        1020 :   return Printer().PrintFieldValueToString(message, field, index, output);
    1841             : }
    1842             : 
    1843           0 : /* static */ bool TextFormat::ParseFieldValueFromString(
    1844             :     const string& input,
    1845             :     const FieldDescriptor* field,
    1846             :     Message* message) {
    1847           0 :   return Parser().ParseFieldValueFromString(input, field, message);
    1848             : }
    1849             : 
    1850             : // Prints an integer as hex with a fixed number of digits dependent on the
    1851             : // integer type.
    1852             : template<typename IntType>
    1853             : static string PaddedHex(IntType value) {
    1854             :   string result;
    1855             :   result.reserve(sizeof(value) * 2);
    1856             :   for (int i = sizeof(value) * 2 - 1; i >= 0; i--) {
    1857             :     result.push_back(int_to_hex_digit(value >> (i*4) & 0x0F));
    1858             :   }
    1859             :   return result;
    1860             : }
    1861             : 
    1862           0 : void TextFormat::Printer::PrintUnknownFields(
    1863             :     const UnknownFieldSet& unknown_fields, TextGenerator& generator) const {
    1864           0 :   for (int i = 0; i < unknown_fields.field_count(); i++) {
    1865           0 :     const UnknownField& field = unknown_fields.field(i);
    1866           0 :     string field_number = SimpleItoa(field.number());
    1867             : 
    1868           0 :     switch (field.type()) {
    1869             :       case UnknownField::TYPE_VARINT:
    1870             :         generator.Print(field_number);
    1871           0 :         generator.Print(": ");
    1872           0 :         generator.Print(SimpleItoa(field.varint()));
    1873           0 :         if (single_line_mode_) {
    1874           0 :           generator.Print(" ");
    1875             :         } else {
    1876           0 :           generator.Print("\n");
    1877             :         }
    1878             :         break;
    1879             :       case UnknownField::TYPE_FIXED32: {
    1880             :         generator.Print(field_number);
    1881           0 :         generator.Print(": 0x");
    1882             :         generator.Print(
    1883           0 :             StrCat(strings::Hex(field.fixed32(), strings::ZERO_PAD_8)));
    1884           0 :         if (single_line_mode_) {
    1885           0 :           generator.Print(" ");
    1886             :         } else {
    1887           0 :           generator.Print("\n");
    1888             :         }
    1889             :         break;
    1890             :       }
    1891             :       case UnknownField::TYPE_FIXED64: {
    1892             :         generator.Print(field_number);
    1893           0 :         generator.Print(": 0x");
    1894             :         generator.Print(
    1895           0 :             StrCat(strings::Hex(field.fixed64(), strings::ZERO_PAD_16)));
    1896           0 :         if (single_line_mode_) {
    1897           0 :           generator.Print(" ");
    1898             :         } else {
    1899           0 :           generator.Print("\n");
    1900             :         }
    1901             :         break;
    1902             :       }
    1903             :       case UnknownField::TYPE_LENGTH_DELIMITED: {
    1904             :         generator.Print(field_number);
    1905           0 :         const string& value = field.length_delimited();
    1906           0 :         UnknownFieldSet embedded_unknown_fields;
    1907           0 :         if (!value.empty() && embedded_unknown_fields.ParseFromString(value)) {
    1908             :           // This field is parseable as a Message.
    1909             :           // So it is probably an embedded message.
    1910           0 :           if (single_line_mode_) {
    1911           0 :             generator.Print(" { ");
    1912             :           } else {
    1913           0 :             generator.Print(" {\n");
    1914             :             generator.Indent();
    1915             :           }
    1916           0 :           PrintUnknownFields(embedded_unknown_fields, generator);
    1917           0 :           if (single_line_mode_) {
    1918           0 :             generator.Print("} ");
    1919             :           } else {
    1920           0 :             generator.Outdent();
    1921           0 :             generator.Print("}\n");
    1922             :           }
    1923             :         } else {
    1924             :           // This field is not parseable as a Message.
    1925             :           // So it is probably just a plain string.
    1926           0 :           generator.Print(": \"");
    1927           0 :           generator.Print(CEscape(value));
    1928           0 :           generator.Print("\"");
    1929           0 :           if (single_line_mode_) {
    1930           0 :             generator.Print(" ");
    1931             :           } else {
    1932           0 :             generator.Print("\n");
    1933             :           }
    1934             :         }
    1935           0 :         break;
    1936             :       }
    1937             :       case UnknownField::TYPE_GROUP:
    1938             :         generator.Print(field_number);
    1939           0 :         if (single_line_mode_) {
    1940           0 :           generator.Print(" { ");
    1941             :         } else {
    1942           0 :           generator.Print(" {\n");
    1943             :           generator.Indent();
    1944             :         }
    1945           0 :         PrintUnknownFields(field.group(), generator);
    1946           0 :         if (single_line_mode_) {
    1947           0 :           generator.Print("} ");
    1948             :         } else {
    1949           0 :           generator.Outdent();
    1950           0 :           generator.Print("}\n");
    1951             :         }
    1952             :         break;
    1953             :     }
    1954             :   }
    1955           0 : }
    1956             : 
    1957             : }  // namespace protobuf
    1958             : }  // namespace google

Generated by: LCOV version 1.10