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