[llvm] r305411 - [StringExtras] overload toHex for ArrayRef<uint8_t>

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 14 13:11:46 PDT 2017


Author: zturner
Date: Wed Jun 14 15:11:46 2017
New Revision: 305411

URL: http://llvm.org/viewvc/llvm-project?rev=305411&view=rev
Log:
[StringExtras] overload toHex for ArrayRef<uint8_t>

Modified:
    llvm/trunk/include/llvm/ADT/StringExtras.h

Modified: llvm/trunk/include/llvm/ADT/StringExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringExtras.h?rev=305411&r1=305410&r2=305411&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringExtras.h (original)
+++ llvm/trunk/include/llvm/ADT/StringExtras.h Wed Jun 14 15:11:46 2017
@@ -14,6 +14,7 @@
 #ifndef LLVM_ADT_STRINGEXTRAS_H
 #define LLVM_ADT_STRINGEXTRAS_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include <cassert>
 #include <cstddef>
@@ -40,6 +41,11 @@ static inline StringRef toStringRef(bool
   return StringRef(B ? "true" : "false");
 }
 
+/// Construct a string ref from an array ref of unsigned chars.
+static inline StringRef toStringRef(ArrayRef<uint8_t> Input) {
+  return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size());
+}
+
 /// Interpret the given character \p C as a hexadecimal digit and return its
 /// value.
 ///
@@ -68,7 +74,7 @@ static inline std::string utohexstr(uint
 
 /// Convert buffer \p Input to its hexadecimal representation.
 /// The returned string is double the size of \p Input.
-static inline std::string toHex(StringRef Input) {
+inline std::string toHex(StringRef Input) {
   static const char *const LUT = "0123456789ABCDEF";
   size_t Length = Input.size();
 
@@ -82,6 +88,10 @@ static inline std::string toHex(StringRe
   return Output;
 }
 
+inline std::string toHex(ArrayRef<uint8_t> Input) {
+  return toHex(toStringRef(Input));
+}
+
 static inline uint8_t hexFromNibbles(char MSB, char LSB) {
   unsigned U1 = hexDigitValue(MSB);
   unsigned U2 = hexDigitValue(LSB);




More information about the llvm-commits mailing list