Skip to content

Commit a170834

Browse files
committed
Add control of hex casing in APInt::toString
This will be used in implementing arbitrary precision support to FileCheck's numeric variables and expressions. Reviewed By: foad, RKSimon Differential Revision: https://reviews.llvm.org/D150879
1 parent 094ab47 commit a170834

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

llvm/include/llvm/ADT/APInt.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,9 +1635,10 @@ class [[nodiscard]] APInt {
16351635
void print(raw_ostream &OS, bool isSigned) const;
16361636

16371637
/// Converts an APInt to a string and append it to Str. Str is commonly a
1638-
/// SmallString.
1638+
/// SmallString. If Radix > 10, UpperCase determine the case of letter
1639+
/// digits.
16391640
void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1640-
bool formatAsCLiteral = false) const;
1641+
bool formatAsCLiteral = false, bool UpperCase = true) const;
16411642

16421643
/// Considers the APInt to be unsigned and converts it into a string in the
16431644
/// radix given. The radix can be 2, 8, 10 16, or 36.

llvm/lib/Support/APInt.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,8 +2136,8 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
21362136
this->negate();
21372137
}
21382138

2139-
void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
2140-
bool Signed, bool formatAsCLiteral) const {
2139+
void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
2140+
bool formatAsCLiteral, bool UpperCase) const {
21412141
assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 ||
21422142
Radix == 36) &&
21432143
"Radix should be 2, 8, 10, 16, or 36!");
@@ -2173,7 +2173,9 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
21732173
return;
21742174
}
21752175

2176-
static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2176+
static const char BothDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz"
2177+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2178+
const char *Digits = BothDigits + (UpperCase ? 36 : 0);
21772179

21782180
if (isSingleWord()) {
21792181
char Buffer[65];

llvm/unittests/ADT/APIntTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,9 @@ TEST(APIntTest, toString) {
13881388
APInt(8, 255, isSigned).toString(S, 10, isSigned, true);
13891389
EXPECT_EQ(std::string(S), "255");
13901390
S.clear();
1391+
APInt(8, 255, isSigned).toString(S, 16, isSigned, true, /*UpperCase=*/false);
1392+
EXPECT_EQ(std::string(S), "0xff");
1393+
S.clear();
13911394
APInt(8, 255, isSigned).toString(S, 16, isSigned, true);
13921395
EXPECT_EQ(std::string(S), "0xFF");
13931396
S.clear();

0 commit comments

Comments
 (0)