| 1 | //===- FormatUtil.cpp ----------------------------------------- *- C++ --*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "FormatUtil.h" |
| 10 | #include "llvm/Support/Format.h" |
| 11 | #include "llvm/Support/FormatVariadic.h" |
| 12 | |
| 13 | using namespace lldb_private; |
| 14 | using namespace llvm; |
| 15 | |
| 16 | LinePrinter::Line::~Line() { |
| 17 | if (P) |
| 18 | P->NewLine(); |
| 19 | } |
| 20 | |
| 21 | LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream) |
| 22 | : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {} |
| 23 | |
| 24 | void LinePrinter::Indent(uint32_t Amount) { |
| 25 | if (Amount == 0) |
| 26 | Amount = IndentSpaces; |
| 27 | CurrentIndent += Amount; |
| 28 | } |
| 29 | |
| 30 | void LinePrinter::Unindent(uint32_t Amount) { |
| 31 | if (Amount == 0) |
| 32 | Amount = IndentSpaces; |
| 33 | CurrentIndent = std::max<int>(a: 0, b: CurrentIndent - Amount); |
| 34 | } |
| 35 | |
| 36 | void LinePrinter::NewLine() { |
| 37 | OS << "\n" ; |
| 38 | } |
| 39 | |
| 40 | void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data, |
| 41 | uint32_t StartOffset) { |
| 42 | if (Data.empty()) { |
| 43 | line() << Label << " ()" ; |
| 44 | return; |
| 45 | } |
| 46 | line() << Label << " (" ; |
| 47 | OS << format_bytes_with_ascii(Bytes: Data, FirstByteOffset: StartOffset, NumPerLine: 32, ByteGroupSize: 4, |
| 48 | IndentLevel: CurrentIndent + IndentSpaces, Upper: true); |
| 49 | NewLine(); |
| 50 | line() << ")" ; |
| 51 | } |
| 52 | |
| 53 | void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data, |
| 54 | uint64_t Base, uint32_t StartOffset) { |
| 55 | if (Data.empty()) { |
| 56 | line() << Label << " ()" ; |
| 57 | return; |
| 58 | } |
| 59 | line() << Label << " (" ; |
| 60 | Base += StartOffset; |
| 61 | OS << format_bytes_with_ascii(Bytes: Data, FirstByteOffset: Base, NumPerLine: 32, ByteGroupSize: 4, IndentLevel: CurrentIndent + IndentSpaces, |
| 62 | Upper: true); |
| 63 | NewLine(); |
| 64 | line() << ")" ; |
| 65 | } |
| 66 | |