From a17083403fa1829d5a3ae11ca4d3e25213dc6e86 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 18 May 2023 09:10:21 +0000 Subject: [PATCH] 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 --- llvm/include/llvm/ADT/APInt.h | 5 +++-- llvm/lib/Support/APInt.cpp | 8 +++++--- llvm/unittests/ADT/APIntTest.cpp | 3 +++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index 0357165..f6f2e19 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1635,9 +1635,10 @@ public: void print(raw_ostream &OS, bool isSigned) const; /// Converts an APInt to a string and append it to Str. Str is commonly a - /// SmallString. + /// SmallString. If Radix > 10, UpperCase determine the case of letter + /// digits. void toString(SmallVectorImpl &Str, unsigned Radix, bool Signed, - bool formatAsCLiteral = false) const; + bool formatAsCLiteral = false, bool UpperCase = true) const; /// Considers the APInt to be unsigned and converts it into a string in the /// radix given. The radix can be 2, 8, 10 16, or 36. diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index e10057d..7724c65 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -2136,8 +2136,8 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { this->negate(); } -void APInt::toString(SmallVectorImpl &Str, unsigned Radix, - bool Signed, bool formatAsCLiteral) const { +void APInt::toString(SmallVectorImpl &Str, unsigned Radix, bool Signed, + bool formatAsCLiteral, bool UpperCase) const { assert((Radix == 10 || Radix == 8 || Radix == 16 || Radix == 2 || Radix == 36) && "Radix should be 2, 8, 10, 16, or 36!"); @@ -2173,7 +2173,9 @@ void APInt::toString(SmallVectorImpl &Str, unsigned Radix, return; } - static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + static const char BothDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const char *Digits = BothDigits + (UpperCase ? 36 : 0); if (isSingleWord()) { char Buffer[65]; diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index d8d7bfa..b8cf48c 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1388,6 +1388,9 @@ TEST(APIntTest, toString) { APInt(8, 255, isSigned).toString(S, 10, isSigned, true); EXPECT_EQ(std::string(S), "255"); S.clear(); + APInt(8, 255, isSigned).toString(S, 16, isSigned, true, /*UpperCase=*/false); + EXPECT_EQ(std::string(S), "0xff"); + S.clear(); APInt(8, 255, isSigned).toString(S, 16, isSigned, true); EXPECT_EQ(std::string(S), "0xFF"); S.clear(); -- 2.7.4