libwreport 3.40
string.h
1#ifndef WREPORT_STRING_H
2#define WREPORT_STRING_H
3
10
11#include <cctype>
12#include <functional>
13#include <sstream>
14#include <string>
15
16namespace wreport {
17namespace str {
18
20inline bool startswith(const std::string& str, const std::string& part)
21{
22 if (str.size() < part.size())
23 return false;
24 return str.substr(0, part.size()) == part;
25}
26
28inline bool endswith(const std::string& str, const std::string& part)
29{
30 if (str.size() < part.size())
31 return false;
32 return str.substr(str.size() - part.size()) == part;
33}
34
38template <typename ITER>
39std::string join(const std::string& sep, const ITER& begin, const ITER& end)
40{
41 std::stringstream res;
42 bool first = true;
43 for (ITER i = begin; i != end; ++i)
44 {
45 if (first)
46 first = false;
47 else
48 res << sep;
49 res << *i;
50 }
51 return res.str();
52}
53
57template <typename ITEMS>
58std::string join(const std::string& sep, const ITEMS& items)
59{
60 std::stringstream res;
61 bool first = true;
62 for (const auto& i : items)
63 {
64 if (first)
65 first = false;
66 else
67 res << sep;
68 res << i;
69 }
70 return res.str();
71}
72
76std::string lstrip(const std::string& str);
77
81std::string rstrip(const std::string& str);
82
86std::string strip(const std::string& str);
87
89inline std::string upper(const std::string& str)
90{
91 std::string res;
92 res.reserve(str.size());
93 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
94 res += static_cast<char>(std::toupper(static_cast<unsigned char>(*i)));
95 return res;
96}
97
99inline std::string lower(const std::string& str)
100{
101 std::string res;
102 res.reserve(str.size());
103 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
104 res += static_cast<char>(std::tolower(static_cast<unsigned char>(*i)));
105 return res;
106}
107
109[[deprecated("Use path.filename")]] std::string
110basename(const std::string& pathname);
111
113[[deprecated("Use path.parent_path")]] std::string
114dirname(const std::string& pathname);
115
117[[deprecated("Use path / path")]] void appendpath(std::string& dest,
118 const char* path2);
119
121[[deprecated("Use path / path")]] void appendpath(std::string& dest,
122 const std::string& path2);
123
125template <typename S1, typename S2, typename... Args>
126[[deprecated("Use path / path")]] void appendpath(std::string& dest, S1 first,
127 S2 second, Args... next)
128{
129#pragma GCC diagnostic push
130#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
131 appendpath(dest, first);
132 appendpath(dest, second, next...);
133#pragma GCC diagnostic pop
134}
135
137template <typename... Args>
138[[deprecated("Use path / path")]] std::string joinpath(Args... components)
139{
140#pragma GCC diagnostic push
141#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
142 std::string res;
143 appendpath(res, components...);
144 return res;
145#pragma GCC diagnostic pop
146}
147
153[[deprecated(
154 "use path::lexically_normal or std::filesystem::canonical")]] std::string
155normpath(const std::string& pathname);
156
169struct Split
170{
172 std::string str;
174 std::string sep;
180
181 Split(const std::string& str_, const std::string& sep_,
182 bool skip_empty_ = false)
183 : str(str_), sep(sep_), skip_empty(skip_empty_)
184 {
185 }
186
188 {
189 protected:
190 const Split* split = nullptr;
192 std::string cur;
194 size_t end = 0;
195
199
200 public:
201 using iterator_category = std::input_iterator_tag;
202 using value_type = std::string;
203 using difference_type = int;
204 using pointer = std::string*;
205 using reference = std::string&;
206
208 const_iterator(const Split& split);
211 const_iterator(const const_iterator&) = default;
213
214 const_iterator& operator++();
215 const std::string& operator*() const;
216 const std::string* operator->() const;
217
218 std::string remainder() const;
219
220 const_iterator& operator=(const const_iterator&) = default;
221 bool operator==(const const_iterator& ti) const;
222 bool operator!=(const const_iterator& ti) const;
223 };
224
227
230};
231
235std::string encode_cstring(const std::string& str);
236
244std::string decode_cstring(const std::string& str, size_t& lenParsed);
245
247std::string encode_url(const std::string& str);
248
250std::string decode_url(const std::string& str);
251
253std::string encode_base64(const std::string& str);
254
256std::string encode_base64(const void* data, size_t size);
257
259std::string decode_base64(const std::string& str);
260
261} // namespace str
262} // namespace wreport
263#endif
size_t end
Position of the first character of the next token.
Definition string.h:194
const_iterator()
End iterator.
Definition string.h:210
std::string cur
Current token.
Definition string.h:192
const_iterator(const Split &split)
Begin iterator.
void skip_separators()
Move end past all the consecutive separators that start at its position.
String functions.
Definition benchmark.h:13
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition string.h:179
const_iterator end()
Return the end iterator to string split.
Definition string.h:229
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition string.h:226
std::string sep
Separator.
Definition string.h:174
std::string str
String to split.
Definition string.h:172