Fixed with hex numbers with raw_ostream

I've been using the nifty:
   DEBUG(errs() << "Here is the state of things:\n");
style of optional logging, but ran into an issue where I want to dump a table of information. The problem is getting the columns to line up, since the raw_ostream methods write numbers as variable length.

I've worked up a patch that adds two new methods to raw_ostream:

/// write_hex - Output \arg N as ten char hexadecimal string, including
/// 0x prefix (e.g. 0x12345678 or 0x00000001).
raw_ostream &write_hex32(uint32_t N);

/// write_hex - Output \arg N as 18 char hexadecimal string, including
/// 0x prefix (e.g. 0x0123456789abcdef or 0x0000000000000001).
raw_ostream &write_hex64(uint64_t N);

Is there already some way to do this level of formatting with raw_ostream?

raw_ostream.patch (2.09 KB)

You can get the full awesomeness of printf with include/llvm/Support/Format.h:

OS << format("%016" PRIx64, N);

I don't know if there is a significant performance difference.

/jakob

I've been using the nifty:
DEBUG(errs() << "Here is the state of things:\n");
style of optional logging, but ran into an issue where I want to dump a table of information. The problem is getting the columns to line up, since the raw_ostream methods write numbers as variable length.

I've worked up a patch that adds two new methods to raw_ostream:

/// write_hex - Output \arg N as ten char hexadecimal string, including
/// 0x prefix (e.g. 0x12345678 or 0x00000001).
raw_ostream &write_hex32(uint32_t N);

/// write_hex - Output \arg N as 18 char hexadecimal string, including
/// 0x prefix (e.g. 0x0123456789abcdef or 0x0000000000000001).
raw_ostream &write_hex64(uint64_t N);

Is there already some way to do this level of formatting with raw_ostream?

You can get the full awesomeness of printf with include/llvm/Support/Format.h:

OS << format("%016" PRIx64, N);

Excellent!

I don't know if there is a significant performance difference.

These are in DEBUG() statements, so I don't care about performance.

I've switched over to using format(). Thanks!

-Nick