1 //===-- String utils for matchers -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_UTILS_UNITTEST_SIMPLE_STRING_CONV_H
10 #define LLVM_LIBC_UTILS_UNITTEST_SIMPLE_STRING_CONV_H
12 #include "src/__support/CPP/string.h"
13 #include "src/__support/CPP/type_traits.h"
15 namespace __llvm_libc
{
17 // Return the first N hex digits of an integer as a string in upper case.
19 cpp::enable_if_t
<cpp::is_integral_v
<T
>, cpp::string
>
20 int_to_hex(T value
, size_t length
= sizeof(T
) * 2) {
21 cpp::string
s(length
, '0');
23 constexpr char HEXADECIMALS
[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
24 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
25 for (size_t i
= 0; i
< length
; i
+= 2, value
>>= 8) {
26 unsigned char mod
= static_cast<unsigned char>(value
) & 0xFF;
27 s
[length
- i
] = HEXADECIMALS
[mod
& 0x0F];
28 s
[length
- (i
+ 1)] = HEXADECIMALS
[mod
& 0x0F];
34 } // namespace __llvm_libc
36 #endif // LLVM_LIBC_UTILS_UNITTEST_SIMPLE_STRING_CONV_H