libwreport 3.36
string.h
1#ifndef WREPORT_STRING_H
2#define WREPORT_STRING_H
3
11#include <string>
12#include <functional>
13#include <sstream>
14#include <cctype>
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 += ::toupper(*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 += ::tolower(*i);
105 return res;
106}
107
109std::string basename(const std::string& pathname);
110
112std::string dirname(const std::string& pathname);
113
115void appendpath(std::string& dest, const char* path2);
116
118void appendpath(std::string& dest, const std::string& path2);
119
121template<typename S1, typename S2, typename... Args>
122void appendpath(std::string& dest, S1 first, S2 second, Args... next)
123{
124 appendpath(dest, first);
125 appendpath(dest, second, next...);
126}
127
129template<typename... Args>
130std::string joinpath(Args... components)
131{
132 std::string res;
133 appendpath(res, components...);
134 return res;
135}
136
142std::string normpath(const std::string& pathname);
143
156struct Split
157{
159 std::string str;
161 std::string sep;
167
168 Split(const std::string& str, const std::string& sep, bool skip_empty=false)
170
172 {
173 protected:
174 const Split* split = nullptr;
176 std::string cur;
178 size_t end = 0;
179
182
183 public:
184 using iterator_category = std::input_iterator_tag;
185 using value_type = std::string;
186 using difference_type = int;
187 using pointer = std::string*;
188 using reference = std::string&;
189
191 const_iterator(const Split& split);
195
196 const_iterator& operator++();
197 const std::string& operator*() const;
198 const std::string* operator->() const;
199
200 std::string remainder() const;
201
202 bool operator==(const const_iterator& ti) const;
203 bool operator!=(const const_iterator& ti) const;
204 };
205
208
211};
212
216std::string encode_cstring(const std::string& str);
217
225std::string decode_cstring(const std::string& str, size_t& lenParsed);
226
228std::string encode_url(const std::string& str);
229
231std::string decode_url(const std::string& str);
232
234std::string encode_base64(const std::string& str);
235
237std::string encode_base64(const void* data, size_t size);
238
240std::string decode_base64(const std::string& str);
241
242}
243}
244#endif
Definition: string.h:172
size_t end
Position of the first character of the next token.
Definition: string.h:178
const_iterator()
End iterator.
Definition: string.h:193
std::string cur
Current token.
Definition: string.h:176
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
Split a string where a given substring is found.
Definition: string.h:157
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition: string.h:166
const_iterator end()
Return the end iterator to string split.
Definition: string.h:210
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:207
std::string sep
Separator.
Definition: string.h:161
std::string str
String to split.
Definition: string.h:159