ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
CxxUtils
CxxUtils
StringUtilsTemplates.h
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3
*/
4
#ifndef CXXUTILS_STRINGUTILSTEMPLATES_H
5
#define CXXUTILS_STRINGUTILSTEMPLATES_H
6
7
#include <string>
8
#include <string_view>
9
#include <vector>
10
#include <concepts>
11
#include <
CxxUtils/StringUtils.h
>
12
#include <stdexcept>
13
14
namespace
CxxUtils
{
15
16
template
<
typename
T>
17
concept
Numeric
= std::integral<T> || std::floating_point<T>;
18
19
template
<Numeric dType,
bool
silenceEmpty = true>
20
void
convertToNumber
(std::string_view
str
, dType&
number
) {
21
str
=
trimWhiteSpaces
(
str
);
22
if
constexpr
(silenceEmpty){
23
if
(
str
.empty()) {
24
number
= 0;
25
return
;
26
}
27
}
28
auto
[ptr, ec] = std::from_chars(
str
.data(),
str
.data() +
str
.size(),
number
);
29
30
if
(ec != std::errc{}) {
31
throw
std::runtime_error(
"CxxUtils::convertToNumber() - Invalid numeric string: "
+ std::string(
str
));
32
}
33
}
34
35
// A generic tokenizer that returns views
36
template
<
typename
T = std::
string
,
typename
X = std::
string
_view>
37
std::vector<T>
tokenize
(std::string_view
str
, X delimiters) {
38
std::vector<T> tokens;
39
size_t
lastPos =
str
.find_first_not_of(delimiters, 0);
40
size_t
pos =
str
.find_first_of(delimiters, lastPos);
41
42
while
(lastPos != std::string_view::npos) {
43
std::string_view token =
str
.substr(lastPos, pos - lastPos);
44
45
if
constexpr
(std::is_same_v<T, std::string_view>) {
46
tokens.push_back(token);
47
}
else
if
constexpr
(std::is_same_v<T, std::string>) {
48
tokens.emplace_back(token);
49
}
else
{
50
// This handles the int/double cases via your existing convertToNumber
51
T value;
52
convertToNumber
(token, value);
53
tokens.push_back(value);
54
}
55
56
lastPos =
str
.find_first_not_of(delimiters, pos);
57
pos =
str
.find_first_of(delimiters, lastPos);
58
}
59
return
tokens;
60
}
61
62
}
63
64
#endif
StringUtils.h
CxxUtils::Numeric
Definition
StringUtilsTemplates.h:17
CxxUtils
Definition
aligned_vector.h:29
CxxUtils::trimWhiteSpaces
std::string_view trimWhiteSpaces(std::string_view str) noexcept
Removes all trailing and starting whitespaces from a string.
Definition
Control/CxxUtils/Root/StringUtils.cxx:27
CxxUtils::tokenize
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
Definition
Control/CxxUtils/Root/StringUtils.cxx:12
CxxUtils::convertToNumber
void convertToNumber(std::string_view str, dType &number)
Definition
StringUtilsTemplates.h:20
str
Definition
BTagTrackIpAccessor.cxx:11
number
std::string number(const double &d, const std::string &s)
Definition
utils.cxx:186
Generated on
for ATLAS Offline Software by
1.17.0