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