Add llvm::hexdigit to StringExtras (number -> hexadecimal char)
authorDaniel Dunbar <daniel@zuster.org>
Tue, 14 Oct 2008 23:26:20 +0000 (23:26 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Tue, 14 Oct 2008 23:26:20 +0000 (23:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@57536 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/StringExtras.h

index 87b8ba6596a18a1d7aab1b2b973cd3354d7ea6a0..872c8451bf56ce0fab5cc4ea176325e19f56b19e 100644 (file)
 
 namespace llvm {
 
+/// hexdigit - Return the (uppercase) hexadecimal character for the
+/// given number \arg X (which should be less than 16).
+static inline char hexdigit(unsigned X) {
+  return X < 10 ? '0' + X : 'A' + X - 10;
+}
+
 static inline std::string utohexstr(uint64_t X) {
   char Buffer[40];
   char *BufPtr = Buffer+39;
@@ -32,10 +38,7 @@ static inline std::string utohexstr(uint64_t X) {
 
   while (X) {
     unsigned char Mod = static_cast<unsigned char>(X) & 15;
-    if (Mod < 10)
-      *--BufPtr = '0' + Mod;
-    else
-      *--BufPtr = 'A' + Mod-10;
+    *--BufPtr = hexdigit(Mod);
     X >>= 4;
   }
   return std::string(BufPtr);