1*ff137478SMichael Jones //===-- String utils for matchers -------------------------------*- C++ -*-===//
2*ff137478SMichael Jones //
3*ff137478SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*ff137478SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5*ff137478SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*ff137478SMichael Jones //
7*ff137478SMichael Jones //===----------------------------------------------------------------------===//
8*ff137478SMichael Jones 
9*ff137478SMichael Jones #ifndef LLVM_LIBC_UTILS_UNITTEST_SIMPLE_STRING_CONV_H
10*ff137478SMichael Jones #define LLVM_LIBC_UTILS_UNITTEST_SIMPLE_STRING_CONV_H
11*ff137478SMichael Jones 
12*ff137478SMichael Jones #include "src/__support/CPP/TypeTraits.h"
13*ff137478SMichael Jones 
14*ff137478SMichael Jones #include <string>
15*ff137478SMichael Jones 
16*ff137478SMichael Jones namespace __llvm_libc {
17*ff137478SMichael Jones 
18*ff137478SMichael Jones // Return the first N hex digits of an integer as a string in upper case.
19*ff137478SMichael Jones template <typename T>
20*ff137478SMichael Jones cpp::EnableIfType<cpp::IsIntegral<T>::Value, std::string>
21*ff137478SMichael Jones int_to_hex(T X, size_t Length = sizeof(T) * 2) {
22*ff137478SMichael Jones   std::string s(Length, '0');
23*ff137478SMichael Jones 
24*ff137478SMichael Jones   for (auto it = s.rbegin(), end = s.rend(); it != end; ++it, X >>= 4) {
25*ff137478SMichael Jones     unsigned char Mod = static_cast<unsigned char>(X) & 15;
26*ff137478SMichael Jones     *it = (Mod < 10 ? '0' + Mod : 'a' + Mod - 10);
27*ff137478SMichael Jones   }
28*ff137478SMichael Jones 
29*ff137478SMichael Jones   return s;
30*ff137478SMichael Jones }
31*ff137478SMichael Jones 
32*ff137478SMichael Jones } // namespace __llvm_libc
33*ff137478SMichael Jones 
34*ff137478SMichael Jones #endif // LLVM_LIBC_UTILS_UNITTEST_SIMPLE_STRING_CONV_H
35