ATLAS Offline Software
Loading...
Searching...
No Matches
CxxUtils::detail::BasicHexString< FormatStr, Digits > 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::detail::BasicHexString< FormatStr, Digits >:

Public Member Functions

template<typename T>
constexpr BasicHexString (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 BasicHexString &rhs)
constexpr std::string operator+ (const BasicHexString &lhs, const std::string &rhs)
constexpr std::string operator+ (const BasicHexString &lhs, std::string &&rhs)
constexpr std::string operator+ (const char *lhs, const BasicHexString &rhs)
constexpr std::string operator+ (const BasicHexString &lhs, const char *rhs)

Detailed Description

template<FixedString FormatStr, const char * Digits>
class CxxUtils::detail::BasicHexString< FormatStr, Digits >

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 47 of file HexString.h.

Constructor & Destructor Documentation

◆ BasicHexString()

template<FixedString FormatStr, const char * Digits>
template<typename T>
CxxUtils::detail::BasicHexString< FormatStr, Digits >::BasicHexString ( 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 84 of file HexString.h.

84 {
85 static_assert(std::is_integral_v<T>, "HexString only accepts integer types.");
86 // 1. Calculate hex digits needed for this specific type (e.g., sizeof(uint16_t) * 2 = 4 digits)
87 constexpr std::size_t HexDigits = sizeof(T) * 2;
89 static_assert(HexDigits <= s_MaxHexDigits);
90 // Cast to an unsigned equivalent of the same size to prevent sign-extension bugs during shifting
92 UnsignedT value = static_cast<UnsignedT>(context);
93 // 2. Write prefix
94 if constexpr (s_PrefixLen>0){
95 for (std::size_t i = 0; i < s_PrefixLen; ++i) {
96 m_text[i] = FormatStr.buf[i];
97 }
98 }
99 // 3. Write hex digits dynamically based on type size
100 for (std::size_t i = 0; i < HexDigits; ++i) {
101 const std::size_t shift = (HexDigits - 1 - i) * 4;
102 m_text[s_PrefixLen + i] = Digits[(value >> shift) & static_cast<UnsignedT>(0xF)];
103 }
104 // 4. Write postfix
105 if constexpr (s_PostfixLen>0){
106 for (std::size_t i = 0; i < s_PostfixLen; ++i) {
108 }
109 }
110 // 5. Null terminator
111 m_text[m_actual_size] = '\0';
112 }
A class that formats an integer as a hexadecimal string embedded within a format string.
Definition HexString.h:47
static constexpr std::size_t s_MaxHexDigits
The maximum number of hex digits (supports up to 64-bit integers).
Definition HexString.h:70
static constexpr std::size_t s_PrefixLen
The index of the "{}" placeholder.
Definition HexString.h:66
std::size_t m_actual_size
The actual length of the generated string.
Definition HexString.h:74
static constexpr std::size_t s_PostfixLen
The length of the string postfix (after the placeholder).
Definition HexString.h:68
char m_text[s_PrefixLen+s_MaxHexDigits+s_PostfixLen+1]
The internal buffer storing the fully formatted string.
Definition HexString.h:72

Member Function Documentation

◆ c_str()

template<FixedString FormatStr, const char * Digits>
const char * CxxUtils::detail::BasicHexString< FormatStr, Digits >::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 155 of file HexString.h.

155{ return m_text; }

◆ get_placeholder_pos()

template<FixedString FormatStr, const char * Digits>
constexpr std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::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 55 of file HexString.h.

55 {
56 for (std::size_t i = 0; i + 1 < s_FormatLen; ++i) {
57 if (FormatStr.buf[i] == '{' && FormatStr.buf[i + 1] == '}') return i;
58 }
59 return static_cast<std::size_t>(-1);
60 }
static constexpr std::size_t s_FormatLen
The total length of the format string.
Definition HexString.h:50

◆ operator std::string_view()

template<FixedString FormatStr, const char * Digits>
CxxUtils::detail::BasicHexString< FormatStr, Digits >::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 117 of file HexString.h.

117 {
119 }

◆ size()

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::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 150 of file HexString.h.

150{ return m_actual_size;}

◆ operator+ [1/5]

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

Definition at line 143 of file HexString.h.

143 {
144 return lhs + std::string(rhs);
145 }

◆ operator+ [2/5]

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

Definition at line 126 of file HexString.h.

126 {
129 result.reserve(lhs_view.size() + rhs.size());
130 result.append(lhs_view).append(rhs);
131 return result;
132 }
constexpr std::size_t size() const noexcept
Returns the total size of the formatted string.
Definition HexString.h:150

◆ operator+ [3/5]

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

Definition at line 135 of file HexString.h.

135 {
136 const std::string& rhs_l = rhs;
137 return lhs + rhs_l;
138 }

◆ operator+ [4/5]

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

Definition at line 140 of file HexString.h.

140 {
141 return std::string(lhs) + rhs;
142 }

◆ operator+ [5/5]

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

Definition at line 121 of file HexString.h.

121 {
122 lhs.append(static_cast<std::string_view>(rhs));
123 return lhs;
124
125 }

Member Data Documentation

◆ m_actual_size

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::m_actual_size = 0
private

The actual length of the generated string.

Definition at line 74 of file HexString.h.

◆ m_text

template<FixedString FormatStr, const char * Digits>
char CxxUtils::detail::BasicHexString< FormatStr, Digits >::m_text[s_PrefixLen+s_MaxHexDigits+s_PostfixLen+1] {}
private

The internal buffer storing the fully formatted string.

Definition at line 72 of file HexString.h.

72{};

◆ s_FormatLen

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::s_FormatLen = FormatStr.length()
staticconstexprprivate

The total length of the format string.

Definition at line 50 of file HexString.h.

◆ s_MaxHexDigits

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::s_MaxHexDigits = 16
staticconstexprprivate

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

Definition at line 70 of file HexString.h.

◆ s_PlaceholderPos

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::s_PlaceholderPos = get_placeholder_pos()
staticconstexprprivate

The index of the "{}" placeholder.

Definition at line 63 of file HexString.h.

◆ s_PostfixLen

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::s_PostfixLen = s_FormatLen - s_PrefixLen - 2
staticconstexprprivate

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

Definition at line 68 of file HexString.h.

◆ s_PrefixLen

template<FixedString FormatStr, const char * Digits>
std::size_t CxxUtils::detail::BasicHexString< FormatStr, Digits >::s_PrefixLen = s_PlaceholderPos
staticconstexprprivate

The index of the "{}" placeholder.

Definition at line 66 of file HexString.h.


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