[LLVMdev] Fixed width hex numbers with raw_ostream

Nick Kledzik kledzik at apple.com
Fri May 18 14:04:27 PDT 2012


On May 18, 2012, at 1:44 PM, Jakob Stoklund Olesen wrote:
> On May 18, 2012, at 11:50 AM, Nick Kledzik <kledzik at apple.com> wrote:
> 
>> 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




More information about the llvm-dev mailing list