[llvm] r341852 - [ADT] Support converting to lowercase string in toHex
Petr Hosek via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 10 12:34:44 PDT 2018
Author: phosek
Date: Mon Sep 10 12:34:44 2018
New Revision: 341852
URL: http://llvm.org/viewvc/llvm-project?rev=341852&view=rev
Log:
[ADT] Support converting to lowercase string in toHex
This is useful in certain use-cases such as D51833.
Differential Revision: https://reviews.llvm.org/D51835
Modified:
llvm/trunk/include/llvm/ADT/StringExtras.h
llvm/trunk/unittests/ADT/StringExtrasTest.cpp
Modified: llvm/trunk/include/llvm/ADT/StringExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringExtras.h?rev=341852&r1=341851&r2=341852&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/StringExtras.h Mon Sep 10 12:34:44 2018
@@ -139,22 +139,23 @@ inline std::string utohexstr(uint64_t X,
/// Convert buffer \p Input to its hexadecimal representation.
/// The returned string is double the size of \p Input.
-inline std::string toHex(StringRef Input) {
+inline std::string toHex(StringRef Input, bool LowerCase = false) {
static const char *const LUT = "0123456789ABCDEF";
+ const uint8_t Offset = LowerCase ? 32 : 0;
size_t Length = Input.size();
std::string Output;
Output.reserve(2 * Length);
for (size_t i = 0; i < Length; ++i) {
const unsigned char c = Input[i];
- Output.push_back(LUT[c >> 4]);
- Output.push_back(LUT[c & 15]);
+ Output.push_back(LUT[c >> 4] | Offset);
+ Output.push_back(LUT[c & 15] | Offset);
}
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) {
Modified: llvm/trunk/unittests/ADT/StringExtrasTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringExtrasTest.cpp?rev=341852&r1=341851&r2=341852&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringExtrasTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringExtrasTest.cpp Mon Sep 10 12:34:44 2018
@@ -70,6 +70,7 @@ TEST(StringExtrasTest, ToAndFromHex) {
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";
@@ -77,6 +78,7 @@ TEST(StringExtrasTest, ToAndFromHex) {
EvenBytes.size());
EXPECT_EQ(EvenStr, toHex(EvenData));
EXPECT_EQ(EvenData, fromHex(EvenStr));
+ EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
}
TEST(StringExtrasTest, to_float) {
More information about the llvm-commits
mailing list