ATLAS Offline Software
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 */
4 #include <CxxUtils/StringUtils.h>
5 
6 #include <limits>
7 #include <array>
8 #include <functional>
9 #include <iostream>
10 #include <sstream>
11 #include <charconv>
12 #include <algorithm>
13 
14 namespace 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) {
88  return result;
89  }
90 
91  double atof(std::string_view str) {
94  return result;
95  }
96 }
CxxUtils::tokenizeDouble
std::vector< double > tokenizeDouble(const std::string &the_str, std::string_view delimiter)
Definition: Control/CxxUtils/Root/StringUtils.cxx:34
get_generator_info.result
result
Definition: get_generator_info.py:21
max
#define max(a, b)
Definition: cfImp.cxx:41
CxxUtils::eraseWhiteSpaces
std::string_view eraseWhiteSpaces(std::string_view str)
Removes all trailing and starting whitespaces from a string.
Definition: Control/CxxUtils/Root/StringUtils.cxx:44
CxxUtils::tokenizeInt
std::vector< int > tokenizeInt(const std::string &the_str, std::string_view delimiter)
Definition: Control/CxxUtils/Root/StringUtils.cxx:55
CxxUtils::tokenize
std::vector< std::string > tokenize(const std::string &the_str, std::string_view delimiters)
Splits the string into smaller substrings.
Definition: Control/CxxUtils/Root/StringUtils.cxx:15
StringUtils.h
beamspotman.tokens
tokens
Definition: beamspotman.py:1284
CxxUtils::convertToNumber
void convertToNumber(std::string_view str, dType &number)
Definition: Control/CxxUtils/Root/StringUtils.cxx:65
python.AthDsoLogger.delimiter
delimiter
Definition: AthDsoLogger.py:71
CxxUtils
Definition: aligned_vector.h:29
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
CxxUtils::end
auto end(range_with_at< T > &s)
Definition: range_with_at.h:68
python.selection.number
number
Definition: selection.py:20
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
str
Definition: BTagTrackIpAccessor.cxx:11
python.compressB64.c
def c
Definition: compressB64.py:93
CxxUtils::begin
auto begin(range_with_at< T > &s)
Definition: range_with_at.h:64