Belle II Software  release-05-02-19
GeneralCut.cc
1 #include <framework/utilities/GeneralCut.h>
2 #include <cmath>
3 
4 namespace Belle2 {
10  bool almostEqualFloat(const float& a, const float& b)
11  {
12  static_assert(sizeof(float) == sizeof(int32_t));
13  // cppcheck-suppress invalidPointerCast
14  return std::fabs(*(int32_t*)&a - * (int32_t*)&b) <= 2 or (a == b);
15  }
16 
17  bool almostEqualDouble(const double& a, const double& b)
18  {
19  static_assert(sizeof(double) == sizeof(int64_t));
20  // cppcheck-suppress invalidPointerCast
21  return std::fabs(*(int64_t*)&a - * (int64_t*)&b) <= 2 or (a == b);
22  }
23 
24  unsigned long int findMatchedParenthesis(std::string str, char open, char close)
25  {
26  unsigned long int end = 1;
27  if (str[0] == open) {
28  unsigned int count = 1;
29  for (end = 1; end < str.size() and count > 0; ++end) {
30  if (str[end] == open) ++count;
31  else if (str[end] == close) --count;
32  }
33 
34  if (count > 0)
35  throw std::runtime_error("Variable string has an invalid format: " + str);
36  }
37  return end - 1;
38  }
39 
40  unsigned long int findIgnoringParenthesis(std::string str, std::string pattern, unsigned int begin)
41  {
42 
43  if (str.size() < pattern.size())
44  return std::string::npos;
45 
46  for (unsigned int i = begin; i < str.size() - pattern.size(); ++i) {
47  if (str[i] == '[') {
48  i += findMatchedParenthesis(str.substr(i), '[', ']');
49  continue;
50  }
51  if (str[i] == '(') {
52  i += findMatchedParenthesis(str.substr(i), '(', ')');
53  continue;
54  }
55  if (str[i] == '{') {
56  i += findMatchedParenthesis(str.substr(i), '{', '}');
57  continue;
58  }
59 
60  for (unsigned int j = 0; j < pattern.size(); ++j) {
61  if (str[i + j] != pattern[j]) {
62  break;
63  }
64  if (j == pattern.size() - 1) {
65  return i;
66  }
67  }
68  }
69  return std::string::npos;
70  }
71 
72  std::vector<std::string> splitOnDelimiterAndConserveParenthesis(std::string str, char delimiter, char open, char close)
73  {
74 
75  std::vector<std::string> result;
76  unsigned int lastdelimiter = 0;
77  for (unsigned int i = 0; i < str.size(); ++i) {
78  if (str[i] == open) {
79  i += findMatchedParenthesis(str.substr(i), open, close);
80  continue;
81  }
82  if (str[i] == delimiter) {
83  result.push_back(str.substr(lastdelimiter, i - lastdelimiter));
84  lastdelimiter = i + 1;
85  }
86  }
87  std::string last = str.substr(lastdelimiter);
88  if (last.size() != 0) {
89  result.push_back(last);
90  }
91  return result;
92  }
93 
95 }
Belle2::splitOnDelimiterAndConserveParenthesis
std::vector< std::string > splitOnDelimiterAndConserveParenthesis(std::string str, char delimiter, char open, char close)
Split into std::vector on delimiter ignoring delimiters between parenthesis.
Definition: GeneralCut.cc:72
Belle2::findIgnoringParenthesis
unsigned long int findIgnoringParenthesis(std::string str, std::string pattern, unsigned int begin=0)
Returns the position of a pattern in a string ignoring everything that is in parenthesis.
Definition: GeneralCut.cc:40
Belle2::almostEqualFloat
bool almostEqualFloat(const float &a, const float &b)
Helper function to test if two floats are almost equal.
Definition: GeneralCut.cc:10
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::almostEqualDouble
bool almostEqualDouble(const double &a, const double &b)
Helper function to test if two doubles are almost equal.
Definition: GeneralCut.cc:17
Belle2::findMatchedParenthesis
unsigned long int findMatchedParenthesis(std::string str, char open='[', char close=']')
Returns position of the matched closing parenthesis if the first character in the given string contai...
Definition: GeneralCut.cc:24