Skip to content

Commit 39571b3

Browse files
author
Zachary Turner
committedJan 26, 2015
Teach raw_ostream to support hex formatting without a prefix '0x'.
Previously using format_hex() would always print a 0x prior to the hex characters. This allows this to be optional, so that one can choose to print (e.g.) 255 as either 0xFF or just FF. Differential Revision: http://reviews.llvm.org/D7151 llvm-svn: 227108
1 parent 6ffc9bf commit 39571b3

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed
 

‎llvm/include/llvm/Support/Format.h

+26-9
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,38 @@ class FormattedNumber {
259259
unsigned Width;
260260
bool Hex;
261261
bool Upper;
262+
bool HexPrefix;
262263
friend class raw_ostream;
263264
public:
264-
FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U)
265-
: HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U) { }
265+
FormattedNumber(uint64_t HV, int64_t DV, unsigned W, bool H, bool U,
266+
bool Prefix)
267+
: HexValue(HV), DecValue(DV), Width(W), Hex(H), Upper(U),
268+
HexPrefix(Prefix) {}
266269
};
267270

268271
/// format_hex - Output \p N as a fixed width hexadecimal. If number will not
269272
/// fit in width, full number is still printed. Examples:
270-
/// OS << format_hex(255, 4) => 0xff
271-
/// OS << format_hex(255, 4, true) => 0xFF
272-
/// OS << format_hex(255, 6) => 0x00ff
273-
/// OS << format_hex(255, 2) => 0xff
274-
inline FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false) {
273+
/// OS << format_hex(255, 4) => 0xff
274+
/// OS << format_hex(255, 4, true) => 0xFF
275+
/// OS << format_hex(255, 6) => 0x00ff
276+
/// OS << format_hex(255, 2) => 0xff
277+
inline FormattedNumber format_hex(uint64_t N, unsigned Width,
278+
bool Upper = false) {
275279
assert(Width <= 18 && "hex width must be <= 18");
276-
return FormattedNumber(N, 0, Width, true, Upper);
280+
return FormattedNumber(N, 0, Width, true, Upper, true);
281+
}
282+
283+
/// format_hex_no_prefix - Output \p N as a fixed width hexadecimal. Does not
284+
/// prepend '0x' to the outputted string. If number will not fit in width,
285+
/// full number is still printed. Examples:
286+
/// OS << format_hex_no_prefix(255, 4) => ff
287+
/// OS << format_hex_no_prefix(255, 4, true) => FF
288+
/// OS << format_hex_no_prefix(255, 6) => 00ff
289+
/// OS << format_hex_no_prefix(255, 2) => ff
290+
inline FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width,
291+
bool Upper = false) {
292+
assert(Width <= 18 && "hex width must be <= 18");
293+
return FormattedNumber(N, 0, Width, true, Upper, false);
277294
}
278295

279296
/// format_decimal - Output \p N as a right justified, fixed-width decimal. If
@@ -283,7 +300,7 @@ inline FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
283300
/// OS << format_decimal(-1, 3) => " -1"
284301
/// OS << format_decimal(12345, 3) => "12345"
285302
inline FormattedNumber format_decimal(int64_t N, unsigned Width) {
286-
return FormattedNumber(0, N, Width, false, false);
303+
return FormattedNumber(0, N, Width, false, false, false);
287304
}
288305

289306

‎llvm/lib/Support/raw_ostream.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,12 @@ raw_ostream &raw_ostream::operator<<(const FormattedString &FS) {
410410
raw_ostream &raw_ostream::operator<<(const FormattedNumber &FN) {
411411
if (FN.Hex) {
412412
unsigned Nibbles = (64 - countLeadingZeros(FN.HexValue)+3)/4;
413-
unsigned Width = (FN.Width > Nibbles+2) ? FN.Width : Nibbles+2;
414-
413+
unsigned PrefixChars = FN.HexPrefix ? 2 : 0;
414+
unsigned Width = std::max(FN.Width, Nibbles + PrefixChars);
415+
415416
char NumberBuffer[20] = "0x0000000000000000";
417+
if (!FN.HexPrefix)
418+
NumberBuffer[1] = '0';
416419
char *EndPtr = NumberBuffer+Width;
417420
char *CurPtr = EndPtr;
418421
const char A = FN.Upper ? 'A' : 'a';

‎llvm/unittests/Support/raw_ostream_test.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ TEST(raw_ostreamTest, FormatHex) {
162162
EXPECT_EQ("0x1", printToString(format_hex(1, 3), 3));
163163
EXPECT_EQ("0x12", printToString(format_hex(0x12, 3), 4));
164164
EXPECT_EQ("0x123", printToString(format_hex(0x123, 3), 5));
165+
EXPECT_EQ("FF", printToString(format_hex_no_prefix(0xFF, 2, true), 4));
166+
EXPECT_EQ("ABCD", printToString(format_hex_no_prefix(0xABCD, 2, true), 4));
165167
EXPECT_EQ("0xffffffffffffffff",
166168
printToString(format_hex(UINT64_MAX, 18), 18));
167169
EXPECT_EQ("0x8000000000000000",

0 commit comments

Comments
 (0)
Please sign in to comment.