This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
hans marked 2 inline comments as done.
Closed by commit rG83797c03d2ee: [ADT] Use a lookup table in hexdigit() and
call that from toHex() (authored by hans).
Changed prior to commit:
https://reviews.llvm.org/D116960?vs=398704&id=398880#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D116960/new/
https://reviews.llvm.org/D116960
Files:
llvm/include/llvm/ADT/StringExtras.h
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -35,8 +35,10 @@
/// hexdigit - Return the hexadecimal character for the
/// given number \p X (which should be less than 16).
inline char hexdigit(unsigned X, bool LowerCase = false) {
- const char HexChar = LowerCase ? 'a' : 'A';
- return X < 10 ? '0' + X : HexChar + X - 10;
+ assert(X < 16);
+ static const char LUT[] = "0123456789ABCDEF";
+ const uint8_t Offset = LowerCase ? 32 : 0;
+ return LUT[X] | Offset;
}
/// Given an array of c-style strings terminated by a null pointer, construct
@@ -165,16 +167,14 @@
/// Convert buffer \p Input to its hexadecimal representation.
/// The returned string is double the size of \p 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] | Offset);
- Output.push_back(LUT[c & 15] | Offset);
+ Output.push_back(hexdigit(c >> 4, LowerCase));
+ Output.push_back(hexdigit(c & 15, LowerCase));
}
return Output;
}
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -35,8 +35,10 @@
/// hexdigit - Return the hexadecimal character for the
/// given number \p X (which should be less than 16).
inline char hexdigit(unsigned X, bool LowerCase = false) {
- const char HexChar = LowerCase ? 'a' : 'A';
- return X < 10 ? '0' + X : HexChar + X - 10;
+ assert(X < 16);
+ static const char LUT[] = "0123456789ABCDEF";
+ const uint8_t Offset = LowerCase ? 32 : 0;
+ return LUT[X] | Offset;
}
/// Given an array of c-style strings terminated by a null pointer, construct
@@ -165,16 +167,14 @@
/// Convert buffer \p Input to its hexadecimal representation.
/// The returned string is double the size of \p 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] | Offset);
- Output.push_back(LUT[c & 15] | Offset);
+ Output.push_back(hexdigit(c >> 4, LowerCase));
+ Output.push_back(hexdigit(c & 15, LowerCase));
}
return Output;
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits