libwreport 3.42
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::str {
17
19inline bool startswith(const std::string& str, const std::string& part)
20{
21 if (str.size() < part.size())
22 return false;
23 return str.substr(0, part.size()) == part;
24}
25
27inline bool endswith(const std::string& str, const std::string& part)
28{
29 if (str.size() < part.size())
30 return false;
31 return str.substr(str.size() - part.size()) == part;
32}
33
37template <typename ITER>
38std::string join(const std::string& sep, const ITER& begin, const ITER& end)
39{
40 std::stringstream res;
41 bool first = true;
42 for (ITER i = begin; i != end; ++i)
43 {
44 if (first)
45 first = false;
46 else
47 res << sep;
48 res << *i;
49 }
50 return res.str();
51}
52
56template <typename ITEMS>
57std::string join(const std::string& sep, const ITEMS& items)
58{
59 std::stringstream res;
60 bool first = true;
61 for (const auto& i : items)
62 {
63 if (first)
64 first = false;
65 else
66 res << sep;
67 res << i;
68 }
69 return res.str();
70}
71
75std::string lstrip(const std::string& str);
76
80std::string rstrip(const std::string& str);
81
85std::string strip(const std::string& str);
86
88inline std::string upper(const std::string& str)
89{
90 std::string res;
91 res.reserve(str.size());
92 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
93 res += static_cast<char>(std::toupper(static_cast<unsigned char>(*i)));
94 return res;
95}
96
98inline std::string lower(const std::string& str)
99{
100 std::string res;
101 res.reserve(str.size());
102 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
103 res += static_cast<char>(std::tolower(static_cast<unsigned char>(*i)));
104 return res;
105}
106
108[[deprecated("Use path.filename")]] std::string
109basename(const std::string& pathname);
110
112[[deprecated("Use path.parent_path")]] std::string
113dirname(const std::string& pathname);
114
116[[deprecated("Use path / path")]] void appendpath(std::string& dest,
117 const char* path2);
118
120[[deprecated("Use path / path")]] void appendpath(std::string& dest,
121 const std::string& path2);
122
124template <typename S1, typename S2, typename... Args>
125[[deprecated("Use path / path")]] void appendpath(std::string& dest, S1 first,
126 S2 second, Args... next)
127{
128#pragma GCC diagnostic push
129#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
130 appendpath(dest, first);
131 appendpath(dest, second, next...);
132#pragma GCC diagnostic pop
133}
134
136template <typename... Args>
137[[deprecated("Use path / path")]] std::string joinpath(Args... components)
138{
139#pragma GCC diagnostic push
140#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
141 std::string res;
142 appendpath(res, components...);
143 return res;
144#pragma GCC diagnostic pop
145}
146
152[[deprecated(
153 "use path::lexically_normal or std::filesystem::canonical")]] std::string
154normpath(const std::string& pathname);
155
168struct Split
169{
171 std::string str;
173 std::string sep;
179
180 Split(const std::string& str_, const std::string& sep_,
181 bool skip_empty_ = false)
182 : str(str_), sep(sep_), skip_empty(skip_empty_)
183 {
184 }
185
187 {
188 protected:
189 const Split* split = nullptr;
191 std::string cur;
193 size_t end = 0;
194
198
199 public:
200 using iterator_category = std::input_iterator_tag;
201 using value_type = std::string;
202 using difference_type = int;
203 using pointer = std::string*;
204 using reference = std::string&;
205
207 const_iterator(const Split& split);
210 const_iterator(const const_iterator&) = default;
212
213 const_iterator& operator++();
214 const std::string& operator*() const;
215 const std::string* operator->() const;
216
217 std::string remainder() const;
218
219 const_iterator& operator=(const const_iterator&) = default;
220 bool operator==(const const_iterator& ti) const;
221 bool operator!=(const const_iterator& ti) const;
222 };
223
226
229};
230
234std::string encode_cstring(const std::string& str);
235
243std::string decode_cstring(const std::string& str, size_t& lenParsed);
244
246std::string encode_url(const std::string& str);
247
249std::string decode_url(const std::string& str);
250
252std::string encode_base64(const std::string& str);
253
255std::string encode_base64(const void* data, size_t size);
256
258std::string decode_base64(const std::string& str);
259
260} // namespace wreport::str
261#endif
size_t end
Position of the first character of the next token.
Definition string.h:193
const_iterator()
End iterator.
Definition string.h:209
std::string cur
Current token.
Definition string.h:191
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 string.h:16
std::string strip(const std::string &str)
Return the substring of 'str' without all leading and trailing spaces.
std::string encode_base64(const std::string &str)
Encode a string in Base64.
std::string basename(const std::string &pathname)
Given a pathname, return the file name without its path.
std::string decode_base64(const std::string &str)
Decode a string encoded in Base64.
std::string rstrip(const std::string &str)
Return the substring of 'str' without all trailing spaces.
std::string decode_cstring(const std::string &str, size_t &lenParsed)
Unescape a C string, stopping at the first double quotes or at the end of the string.
std::string lstrip(const std::string &str)
Return the substring of 'str' without all leading spaces.
std::string lower(const std::string &str)
Return a lowercased copy of str.
Definition string.h:98
bool endswith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition string.h:27
std::string encode_url(const std::string &str)
Urlencode a string.
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
std::string normpath(const std::string &pathname)
Normalise a pathname.
std::string joinpath(Args... components)
Join two or more paths, adding slashes when appropriate.
Definition string.h:137
std::string join(const std::string &sep, const ITER &begin, const ITER &end)
Stringify and join a sequence of objects.
Definition string.h:38
void appendpath(std::string &dest, const char *path2)
Append path2 to path1, adding slashes when appropriate.
std::string encode_cstring(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
std::string upper(const std::string &str)
Return an uppercased copy of str.
Definition string.h:88
bool startswith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition string.h:19
std::string decode_url(const std::string &str)
Decode an urlencoded string.
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition string.h:178
const_iterator end()
Return the end iterator to string split.
Definition string.h:228
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition string.h:225
std::string sep
Separator.
Definition string.h:173
std::string str
String to split.
Definition string.h:171