[PATCH] D150879: Add control of hex casing in APInt::toString
Thomas Preud'homme via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu May 18 09:13:17 PDT 2023
thopre created this revision.
thopre added reviewers: foad, lattner, craig.topper, RKSimon, lebedev.ri, spatel.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
thopre requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
This will be used in implementing arbitrary precision support to
FileCheck's numeric variables and expressions.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D150879
Files:
llvm/include/llvm/ADT/APInt.h
llvm/lib/Support/APInt.cpp
llvm/unittests/ADT/APIntTest.cpp
Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -1388,6 +1388,9 @@
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();
Index: llvm/lib/Support/APInt.cpp
===================================================================
--- llvm/lib/Support/APInt.cpp
+++ llvm/lib/Support/APInt.cpp
@@ -2136,8 +2136,8 @@
this->negate();
}
-void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
- bool Signed, bool formatAsCLiteral) const {
+void APInt::toString(SmallVectorImpl<char> &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 @@
return;
}
- static const char Digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ static const char LowerCaseDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+ static const char UpperCaseDigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ StringRef Digits = UpperCase ? UpperCaseDigits : LowerCaseDigits;
if (isSingleWord()) {
char Buffer[65];
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -1635,9 +1635,10 @@
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<char> &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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150879.523403.patch
Type: text/x-patch
Size: 2376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230518/1efd1b2c/attachment.bin>
More information about the llvm-commits
mailing list