Skip to content

Commit 246ad97

Browse files
DPL: add compile-time string concat and support std::string_view (#4975)
1 parent e1df1e5 commit 246ad97

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

Framework/Core/include/Framework/StringHelpers.h

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstdint>
1515
#include <utility>
1616
#include <type_traits>
17+
#include <string_view>
1718

1819
// CRC32 Table (zlib polynomial) static
1920
constexpr uint32_t crc_table[256] = {0x0L, 0x77073096L, 0xee0e612cL,
@@ -102,6 +103,15 @@ constexpr bool is_const_str_v(T)
102103
return is_const_str<T>::value;
103104
}
104105

106+
template <char... chars1, char... chars2>
107+
constexpr auto operator+(const ConstStr<chars1...>&, const ConstStr<chars2...>&)
108+
{
109+
return ConstStr<chars1..., chars2...>{};
110+
}
111+
112+
namespace const_str_details
113+
{
114+
105115
template <typename T, std::size_t... Is>
106116
constexpr auto as_chars_impl(std::index_sequence<Is...>)
107117
{
@@ -112,18 +122,45 @@ template <typename T>
112122
constexpr auto as_chars()
113123
{
114124
return as_chars_impl<T>(
115-
std::make_index_sequence<sizeof(T::str()) - 1>{});
125+
std::make_index_sequence<T::size() - 1>{});
126+
}
127+
128+
template <int N>
129+
constexpr const char* const get_str(const char (&str)[N])
130+
{
131+
return str;
132+
}
133+
134+
template <int N>
135+
constexpr const int get_size(const char (&str)[N])
136+
{
137+
return N;
138+
}
139+
140+
constexpr const char* const get_str(const std::string_view& str)
141+
{
142+
return str.data();
143+
}
144+
145+
constexpr const int get_size(const std::string_view& str)
146+
{
147+
return str.size() + 1;
116148
}
149+
} // namespace const_str_details
117150

118-
#define CONST_STR(literal) \
119-
[] { \
120-
struct literal_to_chars { \
121-
static constexpr decltype(auto) str() \
122-
{ \
123-
return literal; \
124-
} \
125-
}; \
126-
return as_chars<literal_to_chars>(); \
151+
#define CONST_STR(literal) \
152+
[] { \
153+
struct literal_to_chars { \
154+
static constexpr decltype(auto) str() \
155+
{ \
156+
return const_str_details::get_str(literal); \
157+
} \
158+
static constexpr decltype(auto) size() \
159+
{ \
160+
return const_str_details::get_size(literal); \
161+
} \
162+
}; \
163+
return const_str_details::as_chars<literal_to_chars>(); \
127164
}()
128165

129166
#endif // O2_FRAMEWORK_STRINGHELPERS_H

Framework/Core/test/test_StringHelpers.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,22 @@ BOOST_AUTO_TEST_CASE(StringHelpersConstStr)
4848
if constexpr (is_const_str_v(myConstStr)) {
4949
std::cout << "myConstStr is a compile-time string" << std::endl;
5050
}
51+
52+
auto myConstStr2 = CONST_STR("hello") + CONST_STR("Universe");
53+
printString(myConstStr2);
54+
static_assert(std::is_same_v<decltype(myConstStr2), ConstStr<'h', 'e', 'l', 'l', 'o', 'U', 'n', 'i', 'v', 'e', 'r', 's', 'e'>>);
55+
56+
enum ParticleSpecies {
57+
kPion,
58+
kKaon
59+
};
60+
static constexpr std::string_view hist[] = {"ptDist", "etaDist"};
61+
static constexpr std::string_view particleSuffix[] = {"_pions", "_kaons"};
62+
63+
printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kPion]));
64+
printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kKaon]));
65+
printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kPion]));
66+
printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kKaon]));
67+
68+
BOOST_CHECK_EQUAL(CONST_STR(hist[0]).hash, CONST_STR("ptDist").hash);
5169
}

0 commit comments

Comments
 (0)