kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/verifier/pretty_printer.h (about) 1 /* 2 * Copyright 2014 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef KYTHE_CXX_VERIFIER_PRETTY_PRINTER_H_ 18 #define KYTHE_CXX_VERIFIER_PRETTY_PRINTER_H_ 19 20 #include <sstream> 21 22 #include "absl/strings/string_view.h" 23 24 namespace kythe { 25 namespace verifier { 26 27 /// \brief Prints human-readable representations of various objects. 28 class PrettyPrinter { 29 public: 30 /// \brief Prints `string`. 31 virtual void Print(absl::string_view string) = 0; 32 33 /// \brief Prints `string`. 34 virtual void Print(const char* string) = 0; 35 36 /// \brief Prints `ptr` in hex with a 0x prefix (or 0 for null pointers). 37 virtual void Print(const void* ptr) = 0; 38 39 virtual ~PrettyPrinter(); 40 }; 41 42 /// \brief A `PrettyPrinter` using a `string` as its backing store. 43 class StringPrettyPrinter : public PrettyPrinter { 44 public: 45 /// \copydoc PrettyPrinter::Print(absl::string_view) 46 void Print(absl::string_view string) override; 47 /// \copydoc PrettyPrinter::Print(const char*) 48 void Print(const char* string) override; 49 /// \copydoc PrettyPrinter::Print(const void *) 50 void Print(const void* ptr) override; 51 /// Returns the `string` printed to thus far. 52 std::string str() { return data_.str(); } 53 54 private: 55 /// The `string` storing this `PrettyPrinter`'s data. 56 std::stringstream data_; 57 }; 58 59 /// \brief A `PrettyPrinter` that directs its output to a file handle. 60 class FileHandlePrettyPrinter : public PrettyPrinter { 61 public: 62 /// \param file The file handle to print to. 63 explicit FileHandlePrettyPrinter(FILE* file) : file_(file) {} 64 /// \copydoc PrettyPrinter::Print(absl::string_view) 65 void Print(absl::string_view string) override; 66 /// \copydoc PrettyPrinter::Print(const char*) 67 void Print(const char* string) override; 68 /// \copydoc PrettyPrinter::Print(const void *) 69 void Print(const void* ptr) override; 70 71 private: 72 FILE* file_; 73 }; 74 75 /// \brief A `PrettyPrinter` that wraps another `PrettyPrinter` but escapes 76 /// to a C/JavaScript-style quotable form. 77 class QuoteEscapingPrettyPrinter : public PrettyPrinter { 78 public: 79 /// \param wrapped The `PrettyPrinter` to which transformed text should be 80 /// sent. 81 explicit QuoteEscapingPrettyPrinter(PrettyPrinter& wrapped) 82 : wrapped_(wrapped) {} 83 /// \copydoc PrettyPrinter::Print(absl::string_view) 84 void Print(absl::string_view string) override; 85 /// \copydoc PrettyPrinter::Print(const char*) 86 void Print(const char* string) override; 87 /// \copydoc PrettyPrinter::Print(const void *) 88 void Print(const void* ptr) override; 89 90 private: 91 PrettyPrinter& wrapped_; 92 }; 93 94 /// \brief A `PrettyPrinter` that wraps another `PrettyPrinter` but escapes 95 /// HTML special characters ("&<>) to HTML entities. 96 class HtmlEscapingPrettyPrinter : public PrettyPrinter { 97 public: 98 /// \param wrapped The `PrettyPrinter` to which transformed text should be 99 /// sent. 100 explicit HtmlEscapingPrettyPrinter(PrettyPrinter& wrapped) 101 : wrapped_(wrapped) {} 102 /// \copydoc PrettyPrinter::Print(absl::string_view) 103 void Print(absl::string_view string) override; 104 /// \copydoc PrettyPrinter::Print(const char*) 105 void Print(const char* string) override; 106 /// \copydoc PrettyPrinter::Print(const void *) 107 void Print(const void* ptr) override; 108 109 private: 110 PrettyPrinter& wrapped_; 111 }; 112 113 } // namespace verifier 114 } // namespace kythe 115 116 #endif // KYTHE_CXX_VERIFIER_PRETTY_PRINTER_H_