1// Copyright Antony Polukhin, 2016-2024.
2//
3// Distributed under the Boost Software License, Version 1.0. (See
4// accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#ifndef BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
8#define BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
9
10#include <boost/config.hpp>
11#ifdef BOOST_HAS_PRAGMA_ONCE
12# pragma once
13#endif
14
15#include <array>
16#include <type_traits>
17
18namespace boost { namespace stacktrace { namespace detail {
19
20BOOST_STATIC_CONSTEXPR char to_hex_array_bytes[] = "0123456789ABCDEF";
21
22template <class T>
23inline std::array<char, 2 + sizeof(void*) * 2 + 1> to_hex_array(T addr) noexcept {
24 std::array<char, 2 + sizeof(void*) * 2 + 1> ret = {._M_elems: "0x"};
25 ret.back() = '\0';
26 static_assert(!std::is_pointer<T>::value, "");
27
28 const std::size_t s = sizeof(T);
29
30 char* out = ret.data() + s * 2 + 1;
31
32 for (std::size_t i = 0; i < s; ++i) {
33 const unsigned char tmp_addr = (addr & 0xFFu);
34 *out = to_hex_array_bytes[tmp_addr & 0xF];
35 -- out;
36 *out = to_hex_array_bytes[tmp_addr >> 4];
37 -- out;
38 addr >>= 8;
39 }
40
41 return ret;
42}
43
44inline std::array<char, 2 + sizeof(void*) * 2 + 1> to_hex_array(const void* addr) noexcept {
45 return to_hex_array(
46 addr: reinterpret_cast< std::make_unsigned<std::ptrdiff_t>::type >(addr)
47 );
48}
49
50}}} // namespace boost::stacktrace::detail
51
52#endif // BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
53

source code of boost/libs/stacktrace/include/boost/stacktrace/detail/to_hex_array.hpp