[PATCH] Teach llvm::format_hex() to support formatting without a prefix '0x'
Zachary Turner
zturner at google.com
Fri Jan 23 11:52:36 PST 2015
Hi rnk, majnemer,
Simple change, hope someone can take a look.
When you're dumping single bytes, having '0x' on the front of every byte is difficult to read and doubles the size of the output.
http://reviews.llvm.org/D7151
Files:
include/llvm/Support/Format.h
lib/Support/raw_ostream.cpp
Index: include/llvm/Support/Format.h
===================================================================
--- include/llvm/Support/Format.h
+++ include/llvm/Support/Format.h
@@ -259,21 +259,26 @@
unsigned Width;
bool Hex;
bool Upper;
+ bool HexPrefix;
friend class raw_ostream;
public:
- FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U)
- : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U) { }
+ FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
+ bool Prefix)
+ : HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
+ HexPrefix(Prefix) {}
};
/// format_hex - Output \p N as a fixed width hexadecimal. If number will not
/// fit in width, full number is still printed. Examples:
-/// OS << format_hex(255, 4) => 0xff
-/// OS << format_hex(255, 4, true) => 0xFF
-/// OS << format_hex(255, 6) => 0x00ff
-/// OS << format_hex(255, 2) => 0xff
-inline FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false) {
+/// OS << format_hex(255, 4) => 0xff
+/// OS << format_hex(255, 4, true) => 0xFF
+/// OS << format_hex(255, 6) => 0x00ff
+/// OS << format_hex(255, 2) => 0xff
+/// OS << format_hex(255, 2, true, false) => FF
+inline FormattedNumber format_hex(uint64_t N, unsigned Width,
+ bool Upper = false, bool HexPrefix = true) {
assert(Width <= 18 && "hex width must be <= 18");
- return FormattedNumber(N, 0, Width, true, Upper);
+ return FormattedNumber(N, 0, Width, true, Upper, HexPrefix);
}
/// format_decimal - Output \p N as a right justified, fixed-width decimal. If
@@ -283,7 +288,7 @@
/// OS << format_decimal(-1, 3) => " -1"
/// OS << format_decimal(12345, 3) => "12345"
inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
- return FormattedNumber(0, N, Width, false, false);
+ return FormattedNumber(0, N, Width, false, false, false);
}
Index: lib/Support/raw_ostream.cpp
===================================================================
--- lib/Support/raw_ostream.cpp
+++ lib/Support/raw_ostream.cpp
@@ -410,9 +410,12 @@
raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) {
if (FN.Hex) {
unsigned Nibbles = (64 - countLeadingZeros(FN.HexValue)+3)/4;
- unsigned Width = (FN.Width > Nibbles+2) ? FN.Width : Nibbles+2;
-
+ unsigned PrefixChars = FN.HexPrefix ? 2 : 0;
+ unsigned Width = std::max(FN.Width, Nibbles + PrefixChars);
+
char NumberBuffer[20] = "0x0000000000000000";
+ if (!FN.HexPrefix)
+ NumberBuffer[1] = '0';
char *EndPtr = NumberBuffer+Width;
char *CurPtr = EndPtr;
const char A = FN.Upper ? 'A' : 'a';
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7151.18687.patch
Type: text/x-patch
Size: 2778 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150123/c0317556/attachment.bin>
More information about the llvm-commits
mailing list