Skip to content

Commit 4f392f9

Browse files
authored
[ADT][bugfix] Fixed extra leading zero in uhextostr (#141097)
fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints extra '0' character.
1 parent 10bd4cd commit 4f392f9

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

llvm/include/llvm/ADT/StringExtras.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ inline std::string utohexstr(uint64_t X, bool LowerCase = false,
179179
char Buffer[17];
180180
char *BufPtr = std::end(Buffer);
181181

182-
if (X == 0) *--BufPtr = '0';
182+
if (X == 0 && !Width)
183+
*--BufPtr = '0';
183184

184185
for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
185186
unsigned char Mod = static_cast<unsigned char>(X) & 15;

llvm/unittests/ADT/StringExtrasTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ TEST(StringExtrasTest, ToAndFromHex) {
144144
}
145145

146146
TEST(StringExtrasTest, UINT64ToHex) {
147+
EXPECT_EQ(utohexstr(0x0u, false, 2), "00");
147148
EXPECT_EQ(utohexstr(0xA0u), "A0");
148149
EXPECT_EQ(utohexstr(0xA0u, false, 4), "00A0");
149150
EXPECT_EQ(utohexstr(0xA0u, false, 8), "000000A0");

0 commit comments

Comments
 (0)