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

source code of libc/test/UnitTest/StringUtils.h