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 
~Line()16 LinePrinter::Line::~Line() {
17   if (P)
18     P->NewLine();
19 }
20 
LinePrinter(int Indent,llvm::raw_ostream & Stream)21 LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
22     : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {}
23 
Indent(uint32_t Amount)24 void LinePrinter::Indent(uint32_t Amount) {
25   if (Amount == 0)
26     Amount = IndentSpaces;
27   CurrentIndent += Amount;
28 }
29 
Unindent(uint32_t Amount)30 void LinePrinter::Unindent(uint32_t Amount) {
31   if (Amount == 0)
32     Amount = IndentSpaces;
33   CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
34 }
35 
NewLine()36 void LinePrinter::NewLine() {
37   OS << "\n";
38 }
39 
formatBinary(StringRef Label,ArrayRef<uint8_t> Data,uint32_t StartOffset)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(Data, StartOffset, 32, 4,
48                                 CurrentIndent + IndentSpaces, true);
49   NewLine();
50   line() << ")";
51 }
52 
formatBinary(StringRef Label,ArrayRef<uint8_t> Data,uint64_t Base,uint32_t StartOffset)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(Data, Base, 32, 4, CurrentIndent + IndentSpaces,
62                                 true);
63   NewLine();
64   line() << ")";
65 }
66