ScopedPrinter was already using its own algorithm to format binary blob data. With the new method added by clayborg, I wanted to get down to a single implementation.
In the process of doing this, I needed to update the code a little bit to support some things that ScopedPrinter needed to do. Here are the material changes from the previous implementation:
- Changed name from format_hex_bytes to format_binary. I felt format_hex_bytes was too similar to format_hex.
- Added the ability to offset each line of the binary blob by an initial offset.
- Fixed a bug in the implementation where we were calling the equivalent of isprint((char)Byte), which can cause the argument to isprint() to be negative, which is undefined behavior.
- Got a little smarter about how wide to print the offset field. Previously we were just using a full 8 bytes for the offset field, but with this patch I calculate the maximum offset value we will need to print, determine how many nibbles are needed to print that value, and then align all offset values to that width.
- Remove the 0x prefix from the offset. I could have added an option to choose whether we want the prefix, and if anyone really needs that I can add it back. I removed it because already have a lot of tests that will fail if the offset is there, and keeping the API simple seems desirable if nobody really needs that functionality.
- Made a few things more idiomatic / LLVMish. For example, None instead of a default constructed Optional<uint64_t>(), remove null checking from unit test helper functions (pointless since we control all entry points into the function), changing function signatures to accept ArrayRef instead of const void * and length.