ATLAS Offline Software
Loading...
Searching...
No Matches
Control/CxxUtils/Root/StringUtils.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
5
6#include <limits>
7#include <array>
8#include <functional>
9#include <iostream>
10#include <sstream>
11#include <charconv>
12#include <algorithm>
13
14namespace CxxUtils {
15 std::vector<std::string> tokenize(const std::string& str,
16 std::string_view delimiters) {
17
18 std::vector<std::string> tokens{};
19 // Skip delimiters at beginning.
20 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
21 // Find first "non-delimiter".
22 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
23
24 while (std::string::npos != pos || std::string::npos != lastPos) {
25 // Found a token, add it to the vector.
26 tokens.push_back(str.substr(lastPos, pos - lastPos));
27 // Skip delimiters. Note the "not_of"
28 lastPos = str.find_first_not_of(delimiters, pos);
29 // Find next "non-delimiter"
30 pos = str.find_first_of(delimiters, lastPos);
31 }
32 return tokens;
33 }
34 std::vector<double> tokenizeDouble(const std::string& the_str,
35 std::string_view delimiter){
36 const std::vector<std::string> strTokens = tokenize(the_str, delimiter);
37 std::vector<double> toReturn{};
38 std::transform(strTokens.begin(), strTokens.end(), std::back_inserter(toReturn),
39 [](const std::string& token){
40 return atof(token);
41 });
42 return toReturn;
43 }
44 std::string_view eraseWhiteSpaces(std::string_view str) {
45 if (str.empty()) return str;
46 size_t begin{0}, end{str.size() -1};
47 while (std::isspace(str[begin])){
48 ++begin;
49 }
50 while (end > 0 && std::isspace(str[end])){
51 --end;
52 }
53 return str.substr(begin, end + 1);
54 }
55 std::vector<int> tokenizeInt(const std::string& the_str,
56 std::string_view delimiter) {
57 const std::vector<std::string> strTokens = tokenize(the_str, delimiter);
58 std::vector<int> toReturn{};
59 std::transform(strTokens.begin(), strTokens.end(), std::back_inserter(toReturn),
60 [](const std::string& token){
61 return atoi(token);
62 });
63 return toReturn;
64 }
65 template <class dType> void convertToNumber(std::string_view str, dType& number) {
67 if (str.empty()) {
68 number = 0;
69 return;
70 }
71 if (std::find_if(str.begin(), str.end(), [](const char c){ return std::isspace(c);}) != str.end()) {
72 std::string_view spaceFreeStr = eraseWhiteSpaces(str);
74 if (spaceFreeStr.size() != str.size()) {
75 convertToNumber(spaceFreeStr, number);
76 return;
77 }
78 }
79 if (std::from_chars(str.data(), str.data() + str.size(), number).ec != std::errc{}) {
80 std::stringstream err_except{};
81 err_except<<"CxxUtils::convertToNumber() - The string '"<<str<<"'. Contains unallowed chars";
82 throw std::runtime_error(err_except.str());
83 }
84 }
85 int atoi(std::string_view str) {
86 int result{std::numeric_limits<int>::max()};
88 return result;
89 }
90
91 double atof(std::string_view str) {
92 double result{std::numeric_limits<double>::max()};
94 return result;
95 }
96}
std::vector< int > tokenizeInt(const std::string &the_str, std::string_view delimiter)
auto end(range_with_at< T > &s)
std::vector< std::string > tokenize(const std::string &the_str, std::string_view delimiters)
Splits the string into smaller substrings.
double atof(std::string_view str)
Converts a string into a double / float.
std::vector< double > tokenizeDouble(const std::string &the_str, std::string_view delimiter)
std::string_view eraseWhiteSpaces(std::string_view str)
Removes all trailing and starting whitespaces from a string.
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
auto begin(range_with_at< T > &s)
void convertToNumber(std::string_view str, dType &number)
std::string number(const double &d, const std::string &s)
Definition utils.cxx:186