Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 09:23:47

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: jschorr@google.com (Joseph Schorr)
0009 //  Based on original Protocol Buffers design by
0010 //  Sanjay Ghemawat, Jeff Dean, and others.
0011 //
0012 // Utilities for printing and parsing protocol messages in a human-readable,
0013 // text-based format.
0014 
0015 #ifndef GOOGLE_PROTOBUF_TEXT_FORMAT_H__
0016 #define GOOGLE_PROTOBUF_TEXT_FORMAT_H__
0017 
0018 #include <atomic>
0019 #include <memory>
0020 #include <string>
0021 #include <vector>
0022 
0023 #include "absl/container/flat_hash_map.h"
0024 #include "absl/container/flat_hash_set.h"
0025 #include "absl/strings/cord.h"
0026 #include "absl/strings/string_view.h"
0027 #include "google/protobuf/descriptor.h"
0028 #include "google/protobuf/message.h"
0029 #include "google/protobuf/message_lite.h"
0030 #include "google/protobuf/port.h"
0031 
0032 
0033 // Must be included last.
0034 #include "google/protobuf/port_def.inc"
0035 
0036 #ifdef SWIG
0037 #error "You cannot SWIG proto headers"
0038 #endif
0039 
0040 namespace google {
0041 namespace protobuf {
0042 
0043 namespace internal {
0044 PROTOBUF_EXPORT extern const char kDebugStringSilentMarker[1];
0045 PROTOBUF_EXPORT extern const char kDebugStringSilentMarkerForDetection[3];
0046 
0047 PROTOBUF_EXPORT int64_t GetRedactedFieldCount();
0048 
0049 // This enum contains all the APIs that convert protos to human-readable
0050 // formats. A higher-level API must correspond to a greater number than any
0051 // lower-level APIs it calls under the hood (e.g kDebugString >
0052 // kMemberPrintToString > kPrintWithStream).
0053 enum class PROTOBUF_EXPORT FieldReporterLevel {
0054   kNoReport = 0,
0055   kPrintMessage = 1,
0056   kPrintWithGenerator = 2,
0057   kPrintWithStream = 3,
0058   kMemberPrintToString = 4,
0059   kStaticPrintToString = 5,
0060   kAbslStringify = 6,
0061   kShortFormat = 7,
0062   kUtf8Format = 8,
0063   kDebugString = 12,
0064   kShortDebugString = 13,
0065   kUtf8DebugString = 14,
0066   kUnredactedDebugFormatForTest = 15,
0067   kUnredactedShortDebugFormatForTest = 16,
0068   kUnredactedUtf8DebugFormatForTest = 17
0069 };
0070 
0071 }  // namespace internal
0072 
0073 namespace io {
0074 class ErrorCollector;  // tokenizer.h
0075 }
0076 
0077 namespace python {
0078 namespace cmessage {
0079 class PythonFieldValuePrinter;
0080 }
0081 }  // namespace python
0082 
0083 namespace internal {
0084 // Enum used to set printing options for StringifyMessage.
0085 PROTOBUF_EXPORT enum class Option;
0086 
0087 // Converts a protobuf message to a string. Sensitive fields are redacted, and a
0088 // per-process randomized prefix is inserted.
0089 PROTOBUF_EXPORT std::string StringifyMessage(const Message& message,
0090                                              Option option,
0091                                              FieldReporterLevel reporter_level);
0092 
0093 class UnsetFieldsMetadataTextFormatTestUtil;
0094 class UnsetFieldsMetadataMessageDifferencerTestUtil;
0095 }  // namespace internal
0096 
0097 // This class implements protocol buffer text format, colloquially known as text
0098 // proto.  Printing and parsing protocol messages in text format is useful for
0099 // debugging and human editing of messages.
0100 //
0101 // This class is really a namespace that contains only static methods.
0102 class PROTOBUF_EXPORT TextFormat {
0103  public:
0104   TextFormat(const TextFormat&) = delete;
0105   TextFormat& operator=(const TextFormat&) = delete;
0106 
0107   // Outputs a textual representation of the given message to the given
0108   // output stream. Returns false if printing fails.
0109   static bool Print(const Message& message, io::ZeroCopyOutputStream* output);
0110 
0111   // Print the fields in an UnknownFieldSet.  They are printed by tag number
0112   // only.  Embedded messages are heuristically identified by attempting to
0113   // parse them. Returns false if printing fails.
0114   static bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
0115                                  io::ZeroCopyOutputStream* output);
0116 
0117   // Like Print(), but outputs directly to a string.
0118   // Note: output will be cleared prior to printing, and will be left empty
0119   // even if printing fails. Returns false if printing fails.
0120   static bool PrintToString(const Message& message, std::string* output);
0121 
0122   // Like PrintUnknownFields(), but outputs directly to a string. Returns
0123   // false if printing fails.
0124   static bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
0125                                          std::string* output);
0126 
0127   // Outputs a textual representation of the value of the field supplied on
0128   // the message supplied. For non-repeated fields, an index of -1 must
0129   // be supplied. Note that this method will print the default value for a
0130   // field if it is not set.
0131   static void PrintFieldValueToString(const Message& message,
0132                                       const FieldDescriptor* field, int index,
0133                                       std::string* output);
0134 
0135   // Forward declare `Printer` for `BaseTextGenerator::MarkerToken` which
0136   // restricts some methods of `BaseTextGenerator` to the class `Printer`.
0137   class Printer;
0138 
0139   class PROTOBUF_EXPORT BaseTextGenerator {
0140    private:
0141     // Passkey (go/totw/134#what-about-stdshared-ptr) that allows `Printer`
0142     // (but not derived classes) to call `PrintMaybeWithMarker` and its
0143     // `Printer::TextGenerator` to overload it.
0144     // This prevents users from bypassing the marker generation.
0145     class MarkerToken {
0146      private:
0147       explicit MarkerToken() = default;  // 'explicit' prevents aggregate init.
0148       friend class Printer;
0149     };
0150 
0151    public:
0152     virtual ~BaseTextGenerator();
0153 
0154     virtual void Indent() {}
0155     virtual void Outdent() {}
0156     // Returns the current indentation size in characters.
0157     virtual size_t GetCurrentIndentationSize() const { return 0; }
0158 
0159     // Print text to the output stream.
0160     virtual void Print(const char* text, size_t size) = 0;
0161 
0162     void PrintString(absl::string_view str) { Print(str.data(), str.size()); }
0163 
0164     template <size_t n>
0165     void PrintLiteral(const char (&text)[n]) {
0166       Print(text, n - 1);  // n includes the terminating zero character.
0167     }
0168 
0169     // Internal to Printer, access regulated by `MarkerToken`.
0170     virtual void PrintMaybeWithMarker(MarkerToken, absl::string_view text) {
0171       Print(text.data(), text.size());
0172     }
0173 
0174     // Internal to Printer, access regulated by `MarkerToken`.
0175     virtual void PrintMaybeWithMarker(MarkerToken, absl::string_view text_head,
0176                                       absl::string_view text_tail) {
0177       Print(text_head.data(), text_head.size());
0178       Print(text_tail.data(), text_tail.size());
0179     }
0180 
0181     friend class Printer;
0182   };
0183 
0184   // The default printer that converts scalar values from fields into their
0185   // string representation.
0186   // You can derive from this FastFieldValuePrinter if you want to have fields
0187   // to be printed in a different way and register it at the Printer.
0188   class PROTOBUF_EXPORT FastFieldValuePrinter {
0189    public:
0190     FastFieldValuePrinter();
0191     FastFieldValuePrinter(const FastFieldValuePrinter&) = delete;
0192     FastFieldValuePrinter& operator=(const FastFieldValuePrinter&) = delete;
0193     virtual ~FastFieldValuePrinter();
0194     virtual void PrintBool(bool val, BaseTextGenerator* generator) const;
0195     virtual void PrintInt32(int32_t val, BaseTextGenerator* generator) const;
0196     virtual void PrintUInt32(uint32_t val, BaseTextGenerator* generator) const;
0197     virtual void PrintInt64(int64_t val, BaseTextGenerator* generator) const;
0198     virtual void PrintUInt64(uint64_t val, BaseTextGenerator* generator) const;
0199     virtual void PrintFloat(float val, BaseTextGenerator* generator) const;
0200     virtual void PrintDouble(double val, BaseTextGenerator* generator) const;
0201     virtual void PrintString(const std::string& val,
0202                              BaseTextGenerator* generator) const;
0203     virtual void PrintBytes(const std::string& val,
0204                             BaseTextGenerator* generator) const;
0205     virtual void PrintEnum(int32_t val, const std::string& name,
0206                            BaseTextGenerator* generator) const;
0207     virtual void PrintFieldName(const Message& message, int field_index,
0208                                 int field_count, const Reflection* reflection,
0209                                 const FieldDescriptor* field,
0210                                 BaseTextGenerator* generator) const;
0211     virtual void PrintFieldName(const Message& message,
0212                                 const Reflection* reflection,
0213                                 const FieldDescriptor* field,
0214                                 BaseTextGenerator* generator) const;
0215     virtual void PrintMessageStart(const Message& message, int field_index,
0216                                    int field_count, bool single_line_mode,
0217                                    BaseTextGenerator* generator) const;
0218     // Allows to override the logic on how to print the content of a message.
0219     // Return false to use the default printing logic. Note that it is legal for
0220     // this function to print something and then return false to use the default
0221     // content printing (although at that point it would behave similarly to
0222     // PrintMessageStart).
0223     virtual bool PrintMessageContent(const Message& message, int field_index,
0224                                      int field_count, bool single_line_mode,
0225                                      BaseTextGenerator* generator) const;
0226     virtual void PrintMessageEnd(const Message& message, int field_index,
0227                                  int field_count, bool single_line_mode,
0228                                  BaseTextGenerator* generator) const;
0229   };
0230 
0231   // Deprecated: please use FastFieldValuePrinter instead.
0232   class PROTOBUF_EXPORT FieldValuePrinter {
0233    public:
0234     FieldValuePrinter();
0235     FieldValuePrinter(const FieldValuePrinter&) = delete;
0236     FieldValuePrinter& operator=(const FieldValuePrinter&) = delete;
0237     virtual ~FieldValuePrinter();
0238     virtual std::string PrintBool(bool val) const;
0239     virtual std::string PrintInt32(int32_t val) const;
0240     virtual std::string PrintUInt32(uint32_t val) const;
0241     virtual std::string PrintInt64(int64_t val) const;
0242     virtual std::string PrintUInt64(uint64_t val) const;
0243     virtual std::string PrintFloat(float val) const;
0244     virtual std::string PrintDouble(double val) const;
0245     virtual std::string PrintString(const std::string& val) const;
0246     virtual std::string PrintBytes(const std::string& val) const;
0247     virtual std::string PrintEnum(int32_t val, const std::string& name) const;
0248     virtual std::string PrintFieldName(const Message& message,
0249                                        const Reflection* reflection,
0250                                        const FieldDescriptor* field) const;
0251     virtual std::string PrintMessageStart(const Message& message,
0252                                           int field_index, int field_count,
0253                                           bool single_line_mode) const;
0254     virtual std::string PrintMessageEnd(const Message& message, int field_index,
0255                                         int field_count,
0256                                         bool single_line_mode) const;
0257 
0258    private:
0259     FastFieldValuePrinter delegate_;
0260   };
0261 
0262   class PROTOBUF_EXPORT MessagePrinter {
0263    public:
0264     MessagePrinter() {}
0265     MessagePrinter(const MessagePrinter&) = delete;
0266     MessagePrinter& operator=(const MessagePrinter&) = delete;
0267     virtual ~MessagePrinter() {}
0268     virtual void Print(const Message& message, bool single_line_mode,
0269                        BaseTextGenerator* generator) const = 0;
0270   };
0271 
0272   // Interface that Printers or Parsers can use to find extensions, or types
0273   // referenced in Any messages.
0274   class PROTOBUF_EXPORT Finder {
0275    public:
0276     virtual ~Finder();
0277 
0278     // Try to find an extension of *message by fully-qualified field
0279     // name.  Returns nullptr if no extension is known for this name or number.
0280     // The base implementation uses the extensions already known by the message.
0281     virtual const FieldDescriptor* FindExtension(Message* message,
0282                                                  const std::string& name) const;
0283 
0284     // Similar to FindExtension, but uses a Descriptor and the extension number
0285     // instead of using a Message and the name when doing the look up.
0286     virtual const FieldDescriptor* FindExtensionByNumber(
0287         const Descriptor* descriptor, int number) const;
0288 
0289     // Find the message type for an Any proto.
0290     // Returns nullptr if no message is known for this name.
0291     // The base implementation only accepts prefixes of type.googleprod.com/ or
0292     // type.googleapis.com/, and searches the DescriptorPool of the parent
0293     // message.
0294     virtual const Descriptor* FindAnyType(const Message& message,
0295                                           const std::string& prefix,
0296                                           const std::string& name) const;
0297 
0298     // Find the message factory for the given extension field. This can be used
0299     // to generalize the Parser to add extension fields to a message in the same
0300     // way as the "input" message for the Parser.
0301     virtual MessageFactory* FindExtensionFactory(
0302         const FieldDescriptor* field) const;
0303   };
0304 
0305   // Class for those users which require more fine-grained control over how
0306   // a protobuffer message is printed out.
0307   class PROTOBUF_EXPORT Printer {
0308    public:
0309     Printer();
0310 
0311     // Like TextFormat::Print
0312     bool Print(const Message& message, io::ZeroCopyOutputStream* output) const;
0313     // Like TextFormat::Printer::Print but takes an additional
0314     // internal::FieldReporterLevel
0315     bool Print(const Message& message, io::ZeroCopyOutputStream* output,
0316                internal::FieldReporterLevel reporter) const;
0317     // Like TextFormat::PrintUnknownFields
0318     bool PrintUnknownFields(const UnknownFieldSet& unknown_fields,
0319                             io::ZeroCopyOutputStream* output) const;
0320     // Like TextFormat::PrintToString
0321     bool PrintToString(const Message& message, std::string* output) const;
0322     // Like TextFormat::PrintUnknownFieldsToString
0323     bool PrintUnknownFieldsToString(const UnknownFieldSet& unknown_fields,
0324                                     std::string* output) const;
0325     // Like TextFormat::PrintFieldValueToString
0326     void PrintFieldValueToString(const Message& message,
0327                                  const FieldDescriptor* field, int index,
0328                                  std::string* output) const;
0329 
0330     // Adjust the initial indent level of all output.  Each indent level is
0331     // equal to two spaces.
0332     void SetInitialIndentLevel(int indent_level) {
0333       initial_indent_level_ = indent_level;
0334     }
0335 
0336     // If printing in single line mode, then the entire message will be output
0337     // on a single line with no line breaks.
0338     void SetSingleLineMode(bool single_line_mode) {
0339       single_line_mode_ = single_line_mode;
0340     }
0341 
0342     bool IsInSingleLineMode() const { return single_line_mode_; }
0343 
0344     // If use_field_number is true, uses field number instead of field name.
0345     void SetUseFieldNumber(bool use_field_number) {
0346       use_field_number_ = use_field_number;
0347     }
0348 
0349     // Set true to print repeated primitives in a format like:
0350     //   field_name: [1, 2, 3, 4]
0351     // instead of printing each value on its own line.  Short format applies
0352     // only to primitive values -- i.e. everything except strings and
0353     // sub-messages/groups.
0354     void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives) {
0355       use_short_repeated_primitives_ = use_short_repeated_primitives;
0356     }
0357 
0358     // Set true to output UTF-8 instead of ASCII.  The only difference
0359     // is that bytes >= 0x80 in string fields will not be escaped,
0360     // because they are assumed to be part of UTF-8 multi-byte
0361     // sequences. This will change the default FastFieldValuePrinter.
0362     void SetUseUtf8StringEscaping(bool as_utf8);
0363 
0364     // Set the default FastFieldValuePrinter that is used for all fields that
0365     // don't have a field-specific printer registered.
0366     // Takes ownership of the printer.
0367     void SetDefaultFieldValuePrinter(const FastFieldValuePrinter* printer);
0368 
0369     [[deprecated("Please use FastFieldValuePrinter")]] void
0370     SetDefaultFieldValuePrinter(const FieldValuePrinter* printer);
0371 
0372     // Sets whether we want to hide unknown fields or not.
0373     // Usually unknown fields are printed in a generic way that includes the
0374     // tag number of the field instead of field name. However, sometimes it
0375     // is useful to be able to print the message without unknown fields (e.g.
0376     // for the python protobuf version to maintain consistency between its pure
0377     // python and c++ implementations).
0378     void SetHideUnknownFields(bool hide) { hide_unknown_fields_ = hide; }
0379 
0380     // If print_message_fields_in_index_order is true, fields of a proto message
0381     // will be printed using the order defined in source code instead of the
0382     // field number, extensions will be printed at the end of the message
0383     // and their relative order is determined by the extension number.
0384     // By default, use the field number order.
0385     void SetPrintMessageFieldsInIndexOrder(
0386         bool print_message_fields_in_index_order) {
0387       print_message_fields_in_index_order_ =
0388           print_message_fields_in_index_order;
0389     }
0390 
0391     // If expand==true, expand google.protobuf.Any payloads. The output
0392     // will be of form
0393     //    [type_url] { <value_printed_in_text> }
0394     //
0395     // If expand==false, print Any using the default printer. The output will
0396     // look like
0397     //    type_url: "<type_url>"  value: "serialized_content"
0398     void SetExpandAny(bool expand) { expand_any_ = expand; }
0399 
0400     // Set how parser finds message for Any payloads.
0401     void SetFinder(const Finder* finder) { finder_ = finder; }
0402 
0403     // If non-zero, we truncate all string fields that are  longer than
0404     // this threshold.  This is useful when the proto message has very long
0405     // strings, e.g., dump of encoded image file.
0406     //
0407     // NOTE:  Setting a non-zero value breaks round-trip safe
0408     // property of TextFormat::Printer.  That is, from the printed message, we
0409     // cannot fully recover the original string field any more.
0410     void SetTruncateStringFieldLongerThan(
0411         const int64_t truncate_string_field_longer_than) {
0412       truncate_string_field_longer_than_ = truncate_string_field_longer_than;
0413     }
0414 
0415     // Sets whether sensitive fields found in the message will be reported or
0416     // not.
0417     void SetReportSensitiveFields(internal::FieldReporterLevel reporter) {
0418       if (report_sensitive_fields_ < reporter) {
0419         report_sensitive_fields_ = reporter;
0420       }
0421     }
0422 
0423     // Sets whether strings will be redacted and thus unparsable.
0424     void SetRedactDebugString(bool redact) { redact_debug_string_ = redact; }
0425 
0426     // Register a custom field-specific FastFieldValuePrinter for fields
0427     // with a particular FieldDescriptor.
0428     // Returns "true" if the registration succeeded, or "false", if there is
0429     // already a printer for that FieldDescriptor.
0430     // Takes ownership of the printer on successful registration.
0431     bool RegisterFieldValuePrinter(const FieldDescriptor* field,
0432                                    const FastFieldValuePrinter* printer);
0433 
0434     [[deprecated("Please use FastFieldValuePrinter")]] bool
0435     RegisterFieldValuePrinter(const FieldDescriptor* field,
0436                               const FieldValuePrinter* printer);
0437 
0438     // Register a custom message-specific MessagePrinter for messages with a
0439     // particular Descriptor.
0440     // Returns "true" if the registration succeeded, or "false" if there is
0441     // already a printer for that Descriptor.
0442     // Takes ownership of the printer on successful registration.
0443     bool RegisterMessagePrinter(const Descriptor* descriptor,
0444                                 const MessagePrinter* printer);
0445 
0446     // Default printing for messages, which allows registered message printers
0447     // to fall back to default printing without losing the ability to control
0448     // sub-messages or fields.
0449     // NOTE: If the passed in `text_generaor` is not actually the current
0450     // `TextGenerator`, then no output will be produced.
0451     void PrintMessage(const Message& message,
0452                       BaseTextGenerator* generator) const;
0453 
0454    private:
0455     friend std::string Message::DebugString() const;
0456     friend std::string Message::ShortDebugString() const;
0457     friend std::string Message::Utf8DebugString() const;
0458     friend std::string internal::StringifyMessage(
0459         const Message& message, internal::Option option,
0460         internal::FieldReporterLevel reporter_level);
0461 
0462     // Sets whether silent markers will be inserted.
0463     void SetInsertSilentMarker(bool v) { insert_silent_marker_ = v; }
0464 
0465     // Sets whether the output string should be made non-deterministic.
0466     // This discourages equality checks based on serialized string comparisons.
0467     void SetRandomizeDebugString(bool randomize) {
0468       randomize_debug_string_ = randomize;
0469     }
0470 
0471     // Forward declaration of an internal class used to print the text
0472     // output to the OutputStream (see text_format.cc for implementation).
0473     class TextGenerator;
0474     using MarkerToken = BaseTextGenerator::MarkerToken;
0475 
0476     // Forward declaration of an internal class used to print field values for
0477     // DebugString APIs (see text_format.cc for implementation).
0478     class DebugStringFieldValuePrinter;
0479 
0480     // Forward declaration of an internal class used to print UTF-8 escaped
0481     // strings (see text_format.cc for implementation).
0482     class FastFieldValuePrinterUtf8Escaping;
0483 
0484     // Internal Print method, used for writing to the OutputStream via
0485     // the TextGenerator class.
0486     void Print(const Message& message, BaseTextGenerator* generator) const;
0487 
0488     // Print a single field.
0489     void PrintField(const Message& message, const Reflection* reflection,
0490                     const FieldDescriptor* field,
0491                     BaseTextGenerator* generator) const;
0492 
0493     // Print a repeated primitive field in short form.
0494     void PrintShortRepeatedField(const Message& message,
0495                                  const Reflection* reflection,
0496                                  const FieldDescriptor* field,
0497                                  BaseTextGenerator* generator) const;
0498 
0499     // Print the name of a field -- i.e. everything that comes before the
0500     // ':' for a single name/value pair.
0501     void PrintFieldName(const Message& message, int field_index,
0502                         int field_count, const Reflection* reflection,
0503                         const FieldDescriptor* field,
0504                         BaseTextGenerator* generator) const;
0505 
0506     // Outputs a textual representation of the value of the field supplied on
0507     // the message supplied or the default value if not set.
0508     void PrintFieldValue(const Message& message, const Reflection* reflection,
0509                          const FieldDescriptor* field, int index,
0510                          BaseTextGenerator* generator) const;
0511 
0512     // Print the fields in an UnknownFieldSet.  They are printed by tag number
0513     // only.  Embedded messages are heuristically identified by attempting to
0514     // parse them (subject to the recursion budget).
0515     void PrintUnknownFields(const UnknownFieldSet& unknown_fields,
0516                             BaseTextGenerator* generator,
0517                             int recursion_budget) const;
0518 
0519     bool PrintAny(const Message& message, BaseTextGenerator* generator) const;
0520 
0521     // Try to redact a field value based on the annotations associated with
0522     // the field. This function returns true if it redacts the field value.
0523     bool TryRedactFieldValue(const Message& message,
0524                              const FieldDescriptor* field,
0525                              BaseTextGenerator* generator,
0526                              bool insert_value_separator) const;
0527 
0528     const FastFieldValuePrinter* GetFieldPrinter(
0529         const FieldDescriptor* field) const {
0530       auto it = custom_printers_.find(field);
0531       return it == custom_printers_.end() ? default_field_value_printer_.get()
0532                                           : it->second.get();
0533     }
0534 
0535     friend class google::protobuf::python::cmessage::PythonFieldValuePrinter;
0536     static void HardenedPrintString(absl::string_view src,
0537                                     TextFormat::BaseTextGenerator* generator);
0538 
0539     int initial_indent_level_;
0540     bool single_line_mode_;
0541     bool use_field_number_;
0542     bool use_short_repeated_primitives_;
0543     bool insert_silent_marker_;
0544     bool redact_debug_string_;
0545     bool randomize_debug_string_;
0546     internal::FieldReporterLevel report_sensitive_fields_;
0547     bool hide_unknown_fields_;
0548     bool print_message_fields_in_index_order_;
0549     bool expand_any_;
0550     int64_t truncate_string_field_longer_than_;
0551 
0552     std::unique_ptr<const FastFieldValuePrinter> default_field_value_printer_;
0553     absl::flat_hash_map<const FieldDescriptor*,
0554                         std::unique_ptr<const FastFieldValuePrinter>>
0555         custom_printers_;
0556 
0557     absl::flat_hash_map<const Descriptor*,
0558                         std::unique_ptr<const MessagePrinter>>
0559         custom_message_printers_;
0560 
0561     const Finder* finder_;
0562   };
0563 
0564   // Parses a text-format protocol message from the given input stream to
0565   // the given message object. This function parses the human-readable
0566   // serialization format written by Print(). Returns true on success. The
0567   // message is cleared first, even if the function fails -- See Merge() to
0568   // avoid this behavior.
0569   //
0570   // Example input: "user {\n id: 123 extra { gender: MALE language: 'en' }\n}"
0571   //
0572   // One common use for this function is parsing handwritten strings in test
0573   // code.
0574   //
0575   // If you would like to read a protocol buffer serialized in the
0576   // (non-human-readable) binary wire format, see
0577   // google::protobuf::MessageLite::ParseFromString().
0578   static bool Parse(io::ZeroCopyInputStream* input, Message* output);
0579   // Like Parse(), but reads directly from a string.
0580   static bool ParseFromString(absl::string_view input, Message* output);
0581   // Like Parse(), but reads directly from a Cord.
0582   static bool ParseFromCord(const absl::Cord& input, Message* output);
0583 
0584   // Like Parse(), but the data is merged into the given message, as if
0585   // using Message::MergeFrom().
0586   static bool Merge(io::ZeroCopyInputStream* input, Message* output);
0587   // Like Merge(), but reads directly from a string.
0588   static bool MergeFromString(absl::string_view input, Message* output);
0589 
0590   // Parse the given text as a single field value and store it into the
0591   // given field of the given message. If the field is a repeated field,
0592   // the new value will be added to the end
0593   static bool ParseFieldValueFromString(absl::string_view input,
0594                                         const FieldDescriptor* field,
0595                                         Message* message);
0596 
0597   // A location in the parsed text.
0598   struct ParseLocation {
0599     int line;
0600     int column;
0601 
0602     ParseLocation() : line(-1), column(-1) {}
0603     ParseLocation(int line_param, int column_param)
0604         : line(line_param), column(column_param) {}
0605   };
0606 
0607   // A range of locations in the parsed text, including `start` and excluding
0608   // `end`.
0609   struct ParseLocationRange {
0610     ParseLocation start;
0611     ParseLocation end;
0612     ParseLocationRange() : start(), end() {}
0613     ParseLocationRange(ParseLocation start_param, ParseLocation end_param)
0614         : start(start_param), end(end_param) {}
0615   };
0616 
0617   struct RedactionState {
0618     bool redact;
0619     bool report;
0620   };
0621 
0622   static TextFormat::RedactionState GetRedactionState(
0623       const FieldDescriptor* field);
0624 
0625   static TextFormat::RedactionState IsOptionSensitive(
0626       const Message& opts, const Reflection* reflection,
0627       const FieldDescriptor* option);
0628   // Data structure which is populated with the locations of each field
0629   // value parsed from the text.
0630   class PROTOBUF_EXPORT ParseInfoTree {
0631    public:
0632     ParseInfoTree() = default;
0633     ParseInfoTree(const ParseInfoTree&) = delete;
0634     ParseInfoTree& operator=(const ParseInfoTree&) = delete;
0635 
0636     // Returns the parse location range for index-th value of the field in
0637     // the parsed text. If none exists, returns a location with start and end
0638     // line -1. Index should be -1 for not-repeated fields.
0639     ParseLocationRange GetLocationRange(const FieldDescriptor* field,
0640                                         int index) const;
0641 
0642     // Returns the starting parse location for index-th value of the field in
0643     // the parsed text. If none exists, returns a location with line = -1. Index
0644     // should be -1 for not-repeated fields.
0645     ParseLocation GetLocation(const FieldDescriptor* field, int index) const {
0646       return GetLocationRange(field, index).start;
0647     }
0648 
0649     // Returns the parse info tree for the given field, which must be a message
0650     // type. The nested information tree is owned by the root tree and will be
0651     // deleted when it is deleted.
0652     ParseInfoTree* GetTreeForNested(const FieldDescriptor* field,
0653                                     int index) const;
0654 
0655    private:
0656     // Allow the text format parser to record information into the tree.
0657     friend class TextFormat;
0658 
0659     // Records the starting and ending locations of a single value for a field.
0660     void RecordLocation(const FieldDescriptor* field, ParseLocationRange range);
0661 
0662     // Create and records a nested tree for a nested message field.
0663     ParseInfoTree* CreateNested(const FieldDescriptor* field);
0664 
0665     // Defines the map from the index-th field descriptor to its parse location.
0666     absl::flat_hash_map<const FieldDescriptor*, std::vector<ParseLocationRange>>
0667         locations_;
0668     // Defines the map from the index-th field descriptor to the nested parse
0669     // info tree.
0670     absl::flat_hash_map<const FieldDescriptor*,
0671                         std::vector<std::unique_ptr<ParseInfoTree>>>
0672         nested_;
0673   };
0674 
0675   // For more control over parsing, use this class.
0676   class PROTOBUF_EXPORT Parser {
0677    public:
0678     Parser();
0679     ~Parser();
0680 
0681     // Like TextFormat::Parse().
0682     bool Parse(io::ZeroCopyInputStream* input, Message* output);
0683     // Like TextFormat::ParseFromString().
0684     bool ParseFromString(absl::string_view input, Message* output);
0685     // Like TextFormat::ParseFromCord().
0686     bool ParseFromCord(const absl::Cord& input, Message* output);
0687     // Like TextFormat::Merge().
0688     bool Merge(io::ZeroCopyInputStream* input, Message* output);
0689     // Like TextFormat::MergeFromString().
0690     bool MergeFromString(absl::string_view input, Message* output);
0691 
0692     // Set where to report parse errors.  If nullptr (the default), errors will
0693     // be printed to stderr.
0694     void RecordErrorsTo(io::ErrorCollector* error_collector) {
0695       error_collector_ = error_collector;
0696     }
0697 
0698     // Set how parser finds extensions.  If nullptr (the default), the
0699     // parser will use the standard Reflection object associated with
0700     // the message being parsed.
0701     void SetFinder(const Finder* finder) { finder_ = finder; }
0702 
0703     // Sets where location information about the parse will be written. If
0704     // nullptr
0705     // (the default), then no location will be written.
0706     void WriteLocationsTo(ParseInfoTree* tree) { parse_info_tree_ = tree; }
0707 
0708     // Normally parsing fails if, after parsing, output->IsInitialized()
0709     // returns false.  Call AllowPartialMessage(true) to skip this check.
0710     void AllowPartialMessage(bool allow) { allow_partial_ = allow; }
0711 
0712     // Allow field names to be matched case-insensitively.
0713     // This is not advisable if there are fields that only differ in case, or
0714     // if you want to enforce writing in the canonical form.
0715     // This is 'false' by default.
0716     void AllowCaseInsensitiveField(bool allow) {
0717       allow_case_insensitive_field_ = allow;
0718     }
0719 
0720     // Like TextFormat::ParseFieldValueFromString
0721     bool ParseFieldValueFromString(absl::string_view input,
0722                                    const FieldDescriptor* field,
0723                                    Message* output);
0724 
0725     // When an unknown extension is met, parsing will fail if this option is
0726     // set to false (the default). If true, unknown extensions will be ignored
0727     // and a warning message will be generated.
0728     // Beware! Setting this option true may hide some errors (e.g. spelling
0729     // error on extension name).  This allows data loss; unlike binary format,
0730     // text format cannot preserve unknown extensions.  Avoid using this option
0731     // if possible.
0732     void AllowUnknownExtension(bool allow) { allow_unknown_extension_ = allow; }
0733 
0734     // When an unknown field is met, parsing will fail if this option is set
0735     // to false (the default). If true, unknown fields will be ignored and
0736     // a warning message will be generated.
0737     // Beware! Setting this option true may hide some errors (e.g. spelling
0738     // error on field name). This allows data loss; unlike binary format, text
0739     // format cannot preserve unknown fields.  Avoid using this option
0740     // if possible.
0741     void AllowUnknownField(bool allow) { allow_unknown_field_ = allow; }
0742 
0743 
0744     void AllowFieldNumber(bool allow) { allow_field_number_ = allow; }
0745 
0746     // Sets maximum recursion depth which parser can use. This is effectively
0747     // the maximum allowed nesting of proto messages.
0748     void SetRecursionLimit(int limit) { recursion_limit_ = limit; }
0749 
0750     // Metadata representing all the fields that were explicitly unset in
0751     // textproto. Example:
0752     // "some_int_field: 0"
0753     // where some_int_field has implicit presence.
0754     //
0755     // This class should only be used to pass data between TextFormat and the
0756     // MessageDifferencer.
0757     class UnsetFieldsMetadata {
0758      public:
0759       UnsetFieldsMetadata() = default;
0760 
0761      private:
0762       using Id = std::pair<const Message*, const FieldDescriptor*>;
0763       // Return an id representing the unset field in the given message.
0764       static Id GetUnsetFieldId(const Message& message,
0765                                 const FieldDescriptor& fd);
0766 
0767       // List of ids of explicitly unset proto fields.
0768       absl::flat_hash_set<Id> ids_;
0769 
0770       friend class ::google::protobuf::internal::
0771           UnsetFieldsMetadataMessageDifferencerTestUtil;
0772       friend class ::google::protobuf::internal::UnsetFieldsMetadataTextFormatTestUtil;
0773       friend class ::google::protobuf::util::MessageDifferencer;
0774       friend class ::google::protobuf::TextFormat::Parser;
0775     };
0776 
0777     // If called, the parser will report the parsed fields that had no
0778     // effect on the resulting proto (for example, fields with no presence that
0779     // were set to their default value). These can be passed to the Partially()
0780     // matcher as an indicator to explicitly check these fields are missing
0781     // in the actual.
0782     void OutputNoOpFields(UnsetFieldsMetadata* no_op_fields) {
0783       no_op_fields_ = no_op_fields;
0784     }
0785 
0786    private:
0787     // Forward declaration of an internal class used to parse text
0788     // representations (see text_format.cc for implementation).
0789     class ParserImpl;
0790 
0791     // Like TextFormat::Merge().  The provided implementation is used
0792     // to do the parsing.
0793     bool MergeUsingImpl(io::ZeroCopyInputStream* input, Message* output,
0794                         ParserImpl* parser_impl);
0795 
0796     io::ErrorCollector* error_collector_;
0797     const Finder* finder_;
0798     ParseInfoTree* parse_info_tree_;
0799     bool allow_partial_;
0800     bool allow_case_insensitive_field_;
0801     bool allow_unknown_field_;
0802     bool allow_unknown_extension_;
0803     bool allow_unknown_enum_;
0804     bool allow_field_number_;
0805     bool allow_relaxed_whitespace_;
0806     bool allow_singular_overwrites_;
0807     int recursion_limit_;
0808     UnsetFieldsMetadata* no_op_fields_ = nullptr;
0809   };
0810 
0811 
0812  private:
0813   // Hack: ParseInfoTree declares TextFormat as a friend which should extend
0814   // the friendship to TextFormat::Parser::ParserImpl, but unfortunately some
0815   // old compilers (e.g. GCC 3.4.6) don't implement this correctly. We provide
0816   // helpers for ParserImpl to call methods of ParseInfoTree.
0817   static inline void RecordLocation(ParseInfoTree* info_tree,
0818                                     const FieldDescriptor* field,
0819                                     ParseLocationRange location);
0820   static inline ParseInfoTree* CreateNested(ParseInfoTree* info_tree,
0821                                             const FieldDescriptor* field);
0822   // To reduce stack frame bloat we use an out-of-line function to print
0823   // strings. This avoid local std::string temporaries.
0824   template <typename... T>
0825   static void OutOfLinePrintString(BaseTextGenerator* generator,
0826                                    const T&... values);
0827 };
0828 
0829 namespace internal {
0830 void PrintTextMarker(TextFormat::BaseTextGenerator* generator, bool redact,
0831                      bool randomize, bool single_line_mode);
0832 }  // namespace internal
0833 
0834 inline void TextFormat::RecordLocation(ParseInfoTree* info_tree,
0835                                        const FieldDescriptor* field,
0836                                        ParseLocationRange location) {
0837   info_tree->RecordLocation(field, location);
0838 }
0839 
0840 inline TextFormat::ParseInfoTree* TextFormat::CreateNested(
0841     ParseInfoTree* info_tree, const FieldDescriptor* field) {
0842   return info_tree->CreateNested(field);
0843 }
0844 
0845 }  // namespace protobuf
0846 }  // namespace google
0847 
0848 #include "google/protobuf/port_undef.inc"
0849 
0850 #endif  // GOOGLE_PROTOBUF_TEXT_FORMAT_H__