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_TEST_UNITTEST_STRINGUTILS_H 10 #define LLVM_LIBC_TEST_UNITTEST_STRINGUTILS_H 11 12 #include "src/__support/CPP/string.h" 13 #include "src/__support/CPP/type_traits.h" 14 #include "src/__support/big_int.h" 15 #include "src/__support/macros/config.h" 16 17 namespace LIBC_NAMESPACE_DECL { 18 19 // Return the first N hex digits of an integer as a string in upper case. 20 template <typename T> 21 cpp::enable_if_t<cpp::is_integral_v<T> || is_big_int_v<T>, cpp::string> 22 int_to_hex(T value, size_t length = sizeof(T) * 2) { 23 cpp::string s(length, '0'); 24 25 constexpr char HEXADECIMALS[16] = {'0', '1', '2', '3', '4', '5', '6', '7', 26 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 27 for (size_t i = 0; i < length; i += 2, value >>= 8) { 28 unsigned char mod = static_cast<unsigned char>(value) & 0xFF; 29 s[length - i] = HEXADECIMALS[mod & 0x0F]; 30 s[length - (i + 1)] = HEXADECIMALS[mod & 0x0F]; 31 } 32 33 return "0x" + s; 34 } 35 36 } // namespace LIBC_NAMESPACE_DECL 37 38 #endif // LLVM_LIBC_TEST_UNITTEST_STRINGUTILS_H 39