forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringUtils.h
More file actions
176 lines (146 loc) · 4.4 KB
/
StringUtils.h
File metadata and controls
176 lines (146 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///
/// \file StringUtils.h
/// \author Barthelemy von Haller
///
#ifndef ALICEO2_STRINGUTILS_H
#define ALICEO2_STRINGUTILS_H
#include <sstream>
#include <vector>
#include <algorithm>
#include "GPUCommonRtypes.h"
namespace o2
{
namespace utils
{
struct Str {
// Code for trimming coming from https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
/**
* Trim from start (in place)
* @param s
*/
static inline void ltrim(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
}
static inline void ltrim(std::string& s, const std::string& start)
{
if (beginsWith(s, start)) {
s.erase(0, start.size());
}
}
/** Trim from end (in place)
*
* @param s
*/
static inline void rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
}
static inline void rtrim(std::string& s, const std::string& ending)
{
if (endsWith(s, ending)) {
s.erase(s.size() - ending.size(), ending.size());
}
}
/**
* Trim from both ends (in place)
* @param s
*/
static inline void trim(std::string& s)
{
ltrim(s);
rtrim(s);
}
/**
* Trim from start (copying)
* @param s
* @return
*/
static inline std::string ltrim_copy(const std::string& s)
{
std::string ss = s;
ltrim(ss);
return ss;
}
static inline std::string ltrim_copy(const std::string& s, const std::string& start)
{
std::string ss = s;
ltrim(ss, start);
return ss;
}
/**
* Trim from end (copying)
* @param s
* @return
*/
static inline std::string rtrim_copy(const std::string& s)
{
std::string ss = s;
rtrim(ss);
return ss;
}
static inline std::string rtrim_copy(const std::string& s, const std::string& ending)
{
std::string ss = s;
rtrim(ss, ending);
return ss;
}
/**
* Trim from both sides (copying)
* @param s
* @return
*/
static inline std::string trim_copy(const std::string& s)
{
std::string ss = s;
rtrim(ss);
return ss;
}
static inline bool endsWith(const std::string& s, const std::string& ending)
{
return (ending.size() > s.size()) ? false : std::equal(ending.rbegin(), ending.rend(), s.rbegin());
}
static inline bool beginsWith(const std::string& s, const std::string& start)
{
return (start.size() > s.size()) ? false : std::equal(start.begin(), start.end(), s.begin());
}
// return vector of tokens from the string with provided delimiter. If requested, trim the spaces from tokens
static std::vector<std::string> tokenize(const std::string& src, char delim, bool trimToken = true, bool skipEmpty = true);
// concatenate arbitrary number of strings
template <typename... Ts>
static std::string concat_string(Ts const&... ts)
{
std::stringstream s;
(s << ... << ts);
return s.str();
}
// replace all occurencies of from by to, return count
static int replaceAll(std::string& s, const std::string& from, const std::string& to);
// generate random string of given length, suitable for file names
static std::string getRandomString(int length);
// Check if the path exists
static bool pathExists(const std::string_view p);
// Check if the path is a directory
static bool pathIsDirectory(const std::string_view p);
// create full path
static std::string getFullPath(const std::string_view p);
// rectify directory, applying convention "none"==""
static std::string rectifyDirectory(const std::string_view p);
// create unique non-existing path name starting with prefix. Loose equivalent of boost::filesystem::unique_path()
// in absence of such a function in std::filesystem
static std::string create_unique_path(const std::string_view prefix = "", int length = 16);
ClassDefNV(Str, 1);
};
} // namespace utils
} // namespace o2
#endif //ALICEO2_STRINGUTILS_H