[PATCH] D51835: [ADT] Support converting to lowercase string in toHex

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 8 17:54:05 PDT 2018


phosek created this revision.
phosek added reviewers: aprantl, zturner.
Herald added subscribers: llvm-commits, dexonsmith.

This is useful in certain use-cases such as https://reviews.llvm.org/D51833.


Repository:
  rL LLVM

https://reviews.llvm.org/D51835

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
@@ -70,13 +70,15 @@
                     OddBytes.size());
   EXPECT_EQ(OddStr, toHex(OddData));
   EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
+  EXPECT_EQ(StringRef(OddStr).lower(), toHex(OddData, true));
 
   std::vector<uint8_t> EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
   std::string EvenStr = "A5BD0D3ECD";
   StringRef EvenData(reinterpret_cast<const char *>(EvenBytes.data()),
                      EvenBytes.size());
   EXPECT_EQ(EvenStr, toHex(EvenData));
   EXPECT_EQ(EvenData, fromHex(EvenStr));
+  EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
 }
 
 TEST(StringExtrasTest, to_float) {
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -139,8 +139,8 @@
 
 /// Convert buffer \p Input to its hexadecimal representation.
 /// The returned string is double the size of \p Input.
-inline std::string toHex(StringRef Input) {
-  static const char *const LUT = "0123456789ABCDEF";
+inline std::string toHex(StringRef Input, bool LowerCase = false) {
+  const char *const LUT = LowerCase ? "0123456789abcdef" : "0123456789ABCDEF";
   size_t Length = Input.size();
 
   std::string Output;
@@ -153,8 +153,8 @@
   return Output;
 }
 
-inline std::string toHex(ArrayRef<uint8_t> Input) {
-  return toHex(toStringRef(Input));
+inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
+  return toHex(toStringRef(Input), LowerCase);
 }
 
 inline uint8_t hexFromNibbles(char MSB, char LSB) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51835.164580.patch
Type: text/x-patch
Size: 1796 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180909/977a022a/attachment.bin>


More information about the llvm-commits mailing list