[PATCH] D116960: [ADT] Add an in-place version of toHex()

Hans Wennborg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 11 02:32:52 PST 2022


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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116960.398880.patch
Type: text/x-patch
Size: 1405 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220111/de2753e3/attachment.bin>


More information about the cfe-commits mailing list