llvm.org GIT mirror llvm / 5603fd0
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341852 91177308-0d34-0410-b5e6-96231b3b80d8 Petr Hosek 1 year, 8 months ago
2 changed file(s) with 8 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
138138
139139 /// Convert buffer \p Input to its hexadecimal representation.
140140 /// The returned string is double the size of \p Input.
141 inline std::string toHex(StringRef Input) {
141 inline std::string toHex(StringRef Input, bool LowerCase = false) {
142142 static const char *const LUT = "0123456789ABCDEF";
143 const uint8_t Offset = LowerCase ? 32 : 0;
143144 size_t Length = Input.size();
144145
145146 std::string Output;
146147 Output.reserve(2 * Length);
147148 for (size_t i = 0; i < Length; ++i) {
148149 const unsigned char c = Input[i];
149 Output.push_back(LUT[c >> 4]);
150 Output.push_back(LUT[c & 15]);
150 Output.push_back(LUT[c >> 4] | Offset);
151 Output.push_back(LUT[c & 15] | Offset);
151152 }
152153 return Output;
153154 }
154155
155 inline std::string toHex(ArrayRef Input) {
156 return toHex(toStringRef(Input));
156 inline std::string toHex(ArrayRef Input, bool LowerCase = false) {
157 return toHex(toStringRef(Input), LowerCase);
157158 }
158159
159160 inline uint8_t hexFromNibbles(char MSB, char LSB) {
6969 OddBytes.size());
7070 EXPECT_EQ(OddStr, toHex(OddData));
7171 EXPECT_EQ(OddData, fromHex(StringRef(OddStr).drop_front()));
72 EXPECT_EQ(StringRef(OddStr).lower(), toHex(OddData, true));
7273
7374 std::vector EvenBytes = {0xA5, 0xBD, 0x0D, 0x3E, 0xCD};
7475 std::string EvenStr = "A5BD0D3ECD";
7677 EvenBytes.size());
7778 EXPECT_EQ(EvenStr, toHex(EvenData));
7879 EXPECT_EQ(EvenData, fromHex(EvenStr));
80 EXPECT_EQ(StringRef(EvenStr).lower(), toHex(EvenData, true));
7981 }
8082
8183 TEST(StringExtrasTest, to_float) {