Belle II Software development
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
16using namespace Belle2;
17
18StringList 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
35std::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
47std::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
65std::string StringUtil::form(const std::string& str, ...)
66{
67 va_list ap;
68 static __thread char ss[1024 * 10];
69 // cppcheck-suppress va_start_referencePassed ; the format is taken by const reference throughout this API
70 va_start(ap, str);
71 vsnprintf(ss, sizeof(ss), str.c_str(), ap);
72 va_end(ap);
73 return ss;
74}
75
76std::string StringUtil::toupper(const std::string& str)
77{
78 std::string s = str;
79 transform(s.begin(), s.end(), s.begin(), ::toupper);
80 return s;
81}
82
83std::string StringUtil::tolower(const std::string& str)
84{
85 std::string s = str;
86 transform(s.begin(), s.end(), s.begin(), ::tolower);
87 return s;
88}
89
90int StringUtil::atoi(const std::string& str)
91{
92 return std::atoi(str.c_str());
93}
94
95double StringUtil::atof(const std::string& str)
96{
97 return std::atof(str.c_str());
98}
99
100long long StringUtil::atoll(const std::string& str)
101{
102 return std::atoll(str.c_str());
103}
104
105bool StringUtil::find(const std::string& s, const std::string& str)
106{
107 return s.find(str) != std::string::npos;
108}
109
110bool StringUtil::isdigit(const std::string& s)
111{
112 if (s.compare(0, 2, "0x") == 0) return true;
113 for (size_t i = 0; i < s.size(); i++) {
114 if (i == 0 && s.at(0) == '-') continue;
115 if (!::isdigit(s.at(i))) return false;
116 }
117 return s.size() > 0 && s != "-";
118}
Abstract base class for different kinds of events.