From 4a86af07a23a7a8ce0b8f41b912c4e77bbb984d0 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 10 Nov 2016 20:16:45 +0000 Subject: [Support] Improve flexibility of binary blob formatter. This makes it possible to indent a binary blob by a certain number of bytes, and also makes some things more idiomatic. Finally, it integrates this binary blob formatter into ScopedPrinter which used to have its own implementation of this algorithm. Differential Revision: https://reviews.llvm.org/D26477 llvm-svn: 286495 --- llvm/lib/Support/ScopedPrinter.cpp | 39 ++++++-------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) (limited to 'llvm/lib/Support/ScopedPrinter.cpp') diff --git a/llvm/lib/Support/ScopedPrinter.cpp b/llvm/lib/Support/ScopedPrinter.cpp index 0225f01156a..d8ee1efd8f3 100644 --- a/llvm/lib/Support/ScopedPrinter.cpp +++ b/llvm/lib/Support/ScopedPrinter.cpp @@ -27,45 +27,18 @@ void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str, if (Block) { startLine() << Label; - if (Str.size() > 0) + if (!Str.empty()) OS << ": " << Str; OS << " (\n"; - for (size_t addr = 0, end = Data.size(); addr < end; addr += 16) { - startLine() << format(" %04" PRIX64 ": ", uint64_t(addr)); - // Dump line of hex. - for (size_t i = 0; i < 16; ++i) { - if (i != 0 && i % 4 == 0) - OS << ' '; - if (addr + i < end) - OS << hexdigit((Data[addr + i] >> 4) & 0xF, false) - << hexdigit(Data[addr + i] & 0xF, false); - else - OS << " "; - } - // Print ascii. - OS << " |"; - for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { - if (std::isprint(Data[addr + i] & 0xFF)) - OS << Data[addr + i]; - else - OS << "."; - } - OS << "|\n"; - } - + if (!Data.empty()) + OS << format_bytes_with_ascii(Data, 0, 16, 4, (IndentLevel + 1) * 2, true) + << "\n"; startLine() << ")\n"; } else { startLine() << Label << ":"; - if (Str.size() > 0) + if (!Str.empty()) OS << " " << Str; - OS << " ("; - for (size_t i = 0; i < Data.size(); ++i) { - if (i > 0) - OS << " "; - - OS << format("%02X", static_cast(Data[i])); - } - OS << ")\n"; + OS << " (" << format_bytes(Data, None, Data.size(), 1, 0, true) << ")\n"; } } -- cgit v1.2.3