kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/verifier/pretty_printer.cc (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  #include "pretty_printer.h"
    18  
    19  #include <bitset>
    20  
    21  #include "absl/strings/escaping.h"
    22  #include "absl/strings/str_format.h"
    23  #include "absl/strings/string_view.h"
    24  
    25  namespace kythe {
    26  namespace verifier {
    27  
    28  PrettyPrinter::~PrettyPrinter() {}
    29  
    30  void StringPrettyPrinter::Print(absl::string_view string) { data_ << string; }
    31  void StringPrettyPrinter::Print(const char* string) { data_ << string; }
    32  
    33  void StringPrettyPrinter::Print(const void* ptr) {
    34    if (ptr) {
    35      data_ << ptr;
    36    } else {
    37      data_ << "0";
    38    }
    39  }
    40  
    41  void FileHandlePrettyPrinter::Print(absl::string_view string) {
    42    absl::FPrintF(file_, "%s", string);
    43  }
    44  
    45  void FileHandlePrettyPrinter::Print(const char* string) {
    46    absl::FPrintF(file_, "%s", string);
    47  }
    48  
    49  void FileHandlePrettyPrinter::Print(const void* ptr) {
    50    absl::FPrintF(file_, "0x%016llx", reinterpret_cast<unsigned long long>(ptr));
    51  }
    52  
    53  void QuoteEscapingPrettyPrinter::Print(absl::string_view string) {
    54    wrapped_.Print(absl::CEscape(string));
    55  }
    56  
    57  void QuoteEscapingPrettyPrinter::Print(const char* string) {
    58    Print(absl::string_view(string));
    59  }
    60  
    61  void QuoteEscapingPrettyPrinter::Print(const void* ptr) { wrapped_.Print(ptr); }
    62  
    63  void HtmlEscapingPrettyPrinter::Print(absl::string_view string) {
    64    for (char ch : string) {
    65      if (ch == '\"') {
    66        wrapped_.Print("&quot;");
    67      } else if (ch == '&') {
    68        wrapped_.Print("&amp;");
    69      } else if (ch == '<') {
    70        wrapped_.Print("&lt;");
    71      } else if (ch == '>') {
    72        wrapped_.Print("&gt;");
    73      } else {
    74        wrapped_.Print({&ch, 1});
    75      }
    76    }
    77  }
    78  
    79  void HtmlEscapingPrettyPrinter::Print(const char* string) {
    80    Print(absl::string_view(string));
    81  }
    82  
    83  void HtmlEscapingPrettyPrinter::Print(const void* ptr) { wrapped_.Print(ptr); }
    84  
    85  }  // namespace verifier
    86  }  // namespace kythe