[PATCH] D118427: [ADT] support fixed-width output with `utohexstr`
Yuanfang Chen via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 28 10:08:16 PST 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGa41c8b8fd5ad: [ADT] support fixed-width output with `utohexstr` (authored by ychen).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D118427/new/
https://reviews.llvm.org/D118427
Files:
llvm/include/llvm/ADT/StringExtras.h
llvm/unittests/ADT/StringExtrasTest.cpp
Index: llvm/unittests/ADT/StringExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/StringExtrasTest.cpp
+++ llvm/unittests/ADT/StringExtrasTest.cpp
@@ -96,6 +96,12 @@
EXPECT_FALSE(tryGetFromHex(InvalidStr, IgnoredOutput));
}
+TEST(StringExtrasTest, UINT64ToHex) {
+ EXPECT_EQ(utohexstr(0xA0u), "A0");
+ EXPECT_EQ(utohexstr(0xA0u, false, 4), "00A0");
+ EXPECT_EQ(utohexstr(0xA0u, false, 8), "000000A0");
+}
+
TEST(StringExtrasTest, to_float) {
float F;
EXPECT_TRUE(to_float("4.7", F));
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -148,13 +148,14 @@
return x;
}
-inline std::string utohexstr(uint64_t X, bool LowerCase = false) {
+inline std::string utohexstr(uint64_t X, bool LowerCase = false,
+ unsigned Width = 0) {
char Buffer[17];
char *BufPtr = std::end(Buffer);
if (X == 0) *--BufPtr = '0';
- while (X) {
+ for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
unsigned char Mod = static_cast<unsigned char>(X) & 15;
*--BufPtr = hexdigit(Mod, LowerCase);
X >>= 4;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118427.404075.patch
Type: text/x-patch
Size: 1257 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220128/d4be312c/attachment.bin>
More information about the llvm-commits
mailing list