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
13using namespace lldb_private;
14using namespace llvm;
15
16LinePrinter::Line::~Line() {
17  if (P)
18    P->NewLine();
19}
20
21LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
22    : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {}
23
24void LinePrinter::Indent(uint32_t Amount) {
25  if (Amount == 0)
26    Amount = IndentSpaces;
27  CurrentIndent += Amount;
28}
29
30void LinePrinter::Unindent(uint32_t Amount) {
31  if (Amount == 0)
32    Amount = IndentSpaces;
33  CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
34}
35
36void LinePrinter::NewLine() {
37  OS << "\n";
38}
39
40void 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
53void 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