Belle II Software  release-05-02-19
StringUtil.cc
1 #include "daq/slc/base/StringUtil.h"
2 
3 #include <cstdarg>
4 #include <cstdio>
5 #include <sstream>
6 #include <cctype>
7 #include <algorithm>
8 
9 using namespace Belle2;
10 
11 StringList StringUtil::split(const std::string& str, const char type, size_t max)
12 {
13  StringList str_v;
14  size_t current = 0, found;
15  while ((found = str.find_first_of(type, current)) != std::string::npos) {
16  str_v.push_back(std::string(str, current, found - current));
17  current = found + 1;
18  }
19  if (str.size() - current > 0) {
20  str_v.push_back(std::string(str, current, str.size() - current));
21  }
22  while (max > 0 && str_v.size() < max) {
23  str_v.push_back("");
24  }
25  return str_v;
26 }
27 
28 std::string StringUtil::join(StringList str_v, const std::string& s, size_t start, size_t end)
29 {
30  std::stringstream ss;
31  for (size_t i = start; i < str_v.size();) {
32  ss << str_v[i];
33  i++;
34  if ((end > 0 && i == end) || i == str_v.size()) break;
35  ss << s;
36  }
37  return ss.str();
38 }
39 
40 std::string StringUtil::replace(const std::string& source,
41  const std::string& pattern,
42  const std::string& placement)
43 {
44  std::string result;
45  std::string::size_type pos_before = 0;
46  std::string::size_type pos = 0;
47  std::string::size_type len = pattern.size();
48  while ((pos = source.find(pattern, pos)) != std::string::npos) {
49  result.append(source, pos_before, pos - pos_before);
50  result.append(placement);
51  pos += len;
52  pos_before = pos;
53  }
54  result.append(source, pos_before, source.size() - pos_before);
55  return result;
56 }
57 
58 std::string StringUtil::form(const std::string& str, ...)
59 {
60  va_list ap;
61  static __thread char ss[1024 * 10];
62  va_start(ap, str);
63  vsnprintf(ss, sizeof(ss), str.c_str(), ap);
64  va_end(ap);
65  return ss;
66 }
67 
68 std::string StringUtil::toupper(const std::string& str)
69 {
70  std::string s = str;
71  transform(s.begin(), s.end(), s.begin(), ::toupper);
72  return s;
73 }
74 
75 std::string StringUtil::tolower(const std::string& str)
76 {
77  std::string s = str;
78  transform(s.begin(), s.end(), s.begin(), ::tolower);
79  return s;
80 }
81 
82 int StringUtil::atoi(const std::string str)
83 {
84  return atoi(str.c_str());
85 }
86 
87 double StringUtil::atof(const std::string str)
88 {
89  return atof(str.c_str());
90 }
91 
92 long long StringUtil::atoll(const std::string str)
93 {
94  return atoll(str.c_str());
95 }
96 
97 bool StringUtil::find(const std::string& s, const std::string& str)
98 {
99  return s.find(str) != std::string::npos;
100 }
101 
102 bool StringUtil::isdigit(const std::string& s)
103 {
104  if (s.find("0x") == 0) return true;
105  for (size_t i = 0; i < s.size(); i++) {
106  if (i == 0 && s.at(0) == '-') continue;
107  if (!::isdigit(s.at(i))) return false;
108  }
109  return s.size() > 0 && s != "-";
110 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19