ATLAS Offline Software
Loading...
Searching...
No Matches
CxxUtils::HexString< FormatStr > Class Template Reference

A class that formats an integer as a hexadecimal string embedded within a format string. More...

#include <HexString.h>

Collaboration diagram for CxxUtils::HexString< FormatStr >:

Public Member Functions

template<typename T>
constexpr HexString (T context)
 Constructs the formatted string by injecting the hex value of the context into the placeholder.
constexpr operator std::string_view () const noexcept
 Implicit conversion operator to std::string_view.
constexpr std::size_t size () const noexcept
 Returns the total size of the formatted string.
const char * c_str () const noexcept
 Returns a pointer to the underlying null-terminated character array.

Static Private Member Functions

static constexpr std::size_t get_placeholder_pos ()
 Finds the position of the "{}" placeholder within the format string.

Private Attributes

char m_text [s_PrefixLen+s_MaxHexDigits+s_PostfixLen+1] {}
 The internal buffer storing the fully formatted string.
std::size_t m_actual_size = 0
 The actual length of the generated string.

Static Private Attributes

static constexpr std::size_t s_FormatLen = FormatStr.length()
 The total length of the format string.
static constexpr std::size_t s_PlaceholderPos = get_placeholder_pos()
 The index of the "{}" placeholder.
static constexpr std::size_t s_PrefixLen = s_PlaceholderPos
 The index of the "{}" placeholder.
static constexpr std::size_t s_PostfixLen = s_FormatLen - s_PrefixLen - 2
 The length of the string postfix (after the placeholder).
static constexpr std::size_t s_MaxHexDigits = 16
 The maximum number of hex digits (supports up to 64-bit integers).

Friends

constexpr std::string operator+ (std::string lhs, const HexString &rhs)
constexpr std::string operator+ (const HexString &lhs, const std::string &rhs)
constexpr std::string operator+ (const char *lhs, const HexString &rhs)
constexpr std::string operator+ (const HexString &lhs, const char *rhs)

Detailed Description

template<detail::FixedString FormatStr>
class CxxUtils::HexString< FormatStr >

A class that formats an integer as a hexadecimal string embedded within a format string.

  • This class uses a compile-time format string containing a {} placeholder. When instantiated with an integer, it replaces the placeholder with the uppercase hexadecimal representation of that integer. It should be prefered over std::format in well-understood hot pathways.
  • Template Parameters
    FormatStrA FixedString containing exactly one "{}" placeholder.

Definition at line 54 of file HexString.h.

Constructor & Destructor Documentation

◆ HexString()

template<detail::FixedString FormatStr>
template<typename T>
CxxUtils::HexString< FormatStr >::HexString ( T context)
inlineexplicitconstexpr

Constructs the formatted string by injecting the hex value of the context into the placeholder.

The benefit of this is that it uses a fixed character buffer determined at compile time eliminating the need for allocations in runtime.

  • Template Parameters
    TAn integral type (e.g., int, uint32_t, size_t).
    Parameters
    contextThe integer value to be formatted as a hex string.

Definition at line 95 of file HexString.h.

95 {
96 static_assert(std::is_integral_v<T>, "HexString only accepts integer types.");
97
98 constexpr char digits[] = "0123456789ABCDEF";
99
100 // 1. Calculate hex digits needed for this specific type (e.g., sizeof(uint16_t) * 2 = 4 digits)
101 constexpr std::size_t HexDigits = sizeof(T) * 2;
102
104 static_assert(HexDigits <= s_MaxHexDigits);
105 // Cast to an unsigned equivalent of the same size to prevent sign-extension bugs during shifting
107 UnsignedT value = static_cast<UnsignedT>(context);
108
109 // 2. Write prefix
110 for (std::size_t i = 0; i < s_PrefixLen; ++i) {
111 m_text[i] = FormatStr.buf[i];
112 }
113
114 // 3. Write hex digits dynamically based on type size
115 for (std::size_t i = 0; i < HexDigits; ++i) {
116 std::size_t shift = (HexDigits - 1 - i) * 4;
117 m_text[s_PrefixLen + i] = digits[(value >> shift) & 0xFU];
118 }
119
120 // 4. Write postfix
121 for (std::size_t i = 0; i < s_PostfixLen; ++i) {
123 }
124
125 // 5. Null terminator
126 m_text[m_actual_size] = '\0';
127 }
A class that formats an integer as a hexadecimal string embedded within a format string.
Definition HexString.h:54
std::size_t m_actual_size
The actual length of the generated string.
Definition HexString.h:84
static constexpr std::size_t s_PostfixLen
The length of the string postfix (after the placeholder).
Definition HexString.h:77
static constexpr std::size_t s_MaxHexDigits
The maximum number of hex digits (supports up to 64-bit integers).
Definition HexString.h:79
static constexpr std::size_t s_PrefixLen
The index of the "{}" placeholder.
Definition HexString.h:75
char m_text[s_PrefixLen+s_MaxHexDigits+s_PostfixLen+1]
The internal buffer storing the fully formatted string.
Definition HexString.h:82

Member Function Documentation

◆ c_str()

template<detail::FixedString FormatStr>
const char * CxxUtils::HexString< FormatStr >::c_str ( ) const
inlinenoexcept

Returns a pointer to the underlying null-terminated character array.

Returns
A const char pointer to the string data.

Definition at line 171 of file HexString.h.

171{ return m_text; }

◆ get_placeholder_pos()

template<detail::FixedString FormatStr>
constexpr std::size_t CxxUtils::HexString< FormatStr >::get_placeholder_pos ( )
inlinestaticconstexprprivate

Finds the position of the "{}" placeholder within the format string.

Returns
The index of the '{' character, or -1 if not found.

Definition at line 63 of file HexString.h.

63 {
64 for (std::size_t i = 0; i + 1 < s_FormatLen; ++i) {
65 if (FormatStr.buf[i] == '{' && FormatStr.buf[i + 1] == '}') return i;
66 }
67 return static_cast<std::size_t>(-1);
68 }
static constexpr std::size_t s_FormatLen
The total length of the format string.
Definition HexString.h:57

◆ operator std::string_view()

template<detail::FixedString FormatStr>
CxxUtils::HexString< FormatStr >::operator std::string_view ( ) const
inlineconstexprnoexcept

Implicit conversion operator to std::string_view.

Returns
A std::string_view representing the formatted string.

Definition at line 133 of file HexString.h.

133 {
135 }

◆ size()

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::size ( ) const
inlineconstexprnoexcept

Returns the total size of the formatted string.

Returns
The number of characters in the string, excluding the null terminator.

Definition at line 165 of file HexString.h.

165{ return m_actual_size; }

◆ operator+ [1/4]

template<detail::FixedString FormatStr>
std::string operator+ ( const char * lhs,
const HexString< FormatStr > & rhs )
friend

Definition at line 153 of file HexString.h.

153 {
154 return std::string(lhs) + rhs;
155 }

◆ operator+ [2/4]

template<detail::FixedString FormatStr>
std::string operator+ ( const HexString< FormatStr > & lhs,
const char * rhs )
friend

Definition at line 157 of file HexString.h.

157 {
158 return lhs + std::string(rhs);
159 }

◆ operator+ [3/4]

template<detail::FixedString FormatStr>
std::string operator+ ( const HexString< FormatStr > & lhs,
const std::string & rhs )
friend

Definition at line 145 of file HexString.h.

145 {
148 result.reserve(lhs_view.size() + rhs.size());
149 result.append(lhs_view).append(rhs);
150 return result;
151 }
constexpr std::size_t size() const noexcept
Returns the total size of the formatted string.
Definition HexString.h:165

◆ operator+ [4/4]

template<detail::FixedString FormatStr>
std::string operator+ ( std::string lhs,
const HexString< FormatStr > & rhs )
friend

Definition at line 140 of file HexString.h.

140 {
141 lhs.append(static_cast<std::string_view>(rhs));
142 return lhs;
143 }

Member Data Documentation

◆ m_actual_size

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::m_actual_size = 0
private

The actual length of the generated string.

Definition at line 84 of file HexString.h.

◆ m_text

template<detail::FixedString FormatStr>
char CxxUtils::HexString< FormatStr >::m_text[s_PrefixLen+s_MaxHexDigits+s_PostfixLen+1] {}
private

The internal buffer storing the fully formatted string.

Definition at line 82 of file HexString.h.

82{};

◆ s_FormatLen

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::s_FormatLen = FormatStr.length()
staticconstexprprivate

The total length of the format string.

Definition at line 57 of file HexString.h.

◆ s_MaxHexDigits

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::s_MaxHexDigits = 16
staticconstexprprivate

The maximum number of hex digits (supports up to 64-bit integers).

Definition at line 79 of file HexString.h.

◆ s_PlaceholderPos

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::s_PlaceholderPos = get_placeholder_pos()
staticconstexprprivate

The index of the "{}" placeholder.

Definition at line 71 of file HexString.h.

◆ s_PostfixLen

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::s_PostfixLen = s_FormatLen - s_PrefixLen - 2
staticconstexprprivate

The length of the string postfix (after the placeholder).

Definition at line 77 of file HexString.h.

◆ s_PrefixLen

template<detail::FixedString FormatStr>
std::size_t CxxUtils::HexString< FormatStr >::s_PrefixLen = s_PlaceholderPos
staticconstexprprivate

The index of the "{}" placeholder.

Definition at line 75 of file HexString.h.


The documentation for this class was generated from the following file: