libept
string.h
Go to the documentation of this file.
1#ifndef EPT_STRING_H
2#define EPT_STRING_H
3
11#include <string>
12#include <functional>
13#include <sstream>
14#include <cctype>
15
16namespace ept {
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
77template<typename FUN>
78inline std::string lstrip(const std::string& str, const FUN& classifier)
79{
80 if (str.empty())
81 return str;
82
83 size_t beg = 0;
84 while (beg < str.size() && classifier(str[beg]))
85 ++beg;
86
87 return str.substr(beg, str.size() - beg + 1);
88}
89
93inline std::string lstrip(const std::string& str)
94{
95 return lstrip(str, ::isspace);
96}
97
102template<typename FUN>
103inline std::string rstrip(const std::string& str, const FUN& classifier)
104{
105 if (str.empty())
106 return str;
107
108 size_t end = str.size();
109 while (end > 0 && classifier(str[end - 1]))
110 --end;
111
112 if (end == 0)
113 return std::string();
114 else
115 return str.substr(0, end);
116}
117
121inline std::string rstrip(const std::string& str)
122{
123 return rstrip(str, ::isspace);
124}
125
130template<typename FUN>
131inline std::string strip(const std::string& str, const FUN& classifier)
132{
133 if (str.empty())
134 return str;
135
136 size_t beg = 0;
137 size_t end = str.size() - 1;
138 while (beg < end && classifier(str[beg]))
139 ++beg;
140 while (end >= beg && classifier(str[end]))
141 --end;
142
143 return str.substr(beg, end-beg+1);
144}
145
149inline std::string strip(const std::string& str)
150{
151 return strip(str, ::isspace);
152}
153
155inline std::string upper(const std::string& str)
156{
157 std::string res;
158 res.reserve(str.size());
159 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
160 res += ::toupper(*i);
161 return res;
162}
163
165inline std::string lower(const std::string& str)
166{
167 std::string res;
168 res.reserve(str.size());
169 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
170 res += ::tolower(*i);
171 return res;
172}
173
175std::string basename(const std::string& pathname);
176
178std::string dirname(const std::string& pathname);
179
181void appendpath(std::string& dest, const char* path2);
182
184void appendpath(std::string& dest, const std::string& path2);
185
187template<typename S1, typename S2, typename... Args>
188void appendpath(std::string& dest, S1 first, S2 second, Args... next)
189{
190 appendpath(dest, first);
191 appendpath(dest, second, next...);
192}
193
195template<typename... Args>
196std::string joinpath(Args... components)
197{
198 std::string res;
199 appendpath(res, components...);
200 return res;
201}
202
208std::string normpath(const std::string& pathname);
209
222struct Split
223{
225 std::string str;
227 std::string sep;
233
234 Split(const std::string& str, const std::string& sep, bool skip_empty=false)
236
237 class const_iterator : public std::iterator<std::input_iterator_tag, std::string>
238 {
239 protected:
240 const Split* split = nullptr;
242 std::string cur;
244 size_t end = 0;
245
247 void skip_separators();
248
249 public:
251 const_iterator(const Split& split);
255
257 const std::string& operator*() const;
258 const std::string* operator->() const;
259
260 std::string remainder() const;
261
262 bool operator==(const const_iterator& ti) const;
263 bool operator!=(const const_iterator& ti) const;
264 };
265
268
271};
272
276std::string encode_cstring(const std::string& str);
277
285std::string decode_cstring(const std::string& str, size_t& lenParsed);
286
288std::string encode_url(const std::string& str);
289
291std::string decode_url(const std::string& str);
292
294std::string encode_base64(const std::string& str);
295
297std::string decode_base64(const std::string& str);
298
299}
300}
301#endif
Definition string.h:238
std::string cur
Current token.
Definition string.h:242
const_iterator()
End iterator.
Definition string.h:253
const_iterator & operator++()
Definition string.cc:172
bool operator==(const const_iterator &ti) const
Definition string.cc:238
const std::string * operator->() const
Definition string.cc:236
const Split * split
Definition string.h:240
std::string remainder() const
Definition string.cc:146
const std::string & operator*() const
Definition string.cc:235
void skip_separators()
Move end past all the consecutive separators that start at its position.
Definition string.cc:154
bool operator!=(const const_iterator &ti) const
Definition string.cc:245
size_t end
Position of the first character of the next token.
Definition string.h:244
~const_iterator()
Definition string.cc:142
Definition string.cc:7
std::string normpath(const std::string &pathname)
Normalise a pathname.
Definition string.cc:104
std::string strip(const std::string &str, const FUN &classifier)
Return the substring of 'str' without all leading and trailing characters for which 'classifier' retu...
Definition string.h:131
std::string join(const std::string &sep, const ITER &begin, const ITER &end)
Stringify and join a sequence of objects.
Definition string.h:39
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
Definition string.cc:18
bool endswith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition string.h:28
std::string encode_cstring(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
Definition string.cc:253
std::string lstrip(const std::string &str, const FUN &classifier)
Return the substring of 'str' without all leading characters for which 'classifier' returns true.
Definition string.h:78
std::string encode_base64(const std::string &str)
Encode a string in Base64.
Definition string.cc:359
std::string decode_base64(const std::string &str)
Decode a string encoded in Base64.
Definition string.cc:392
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.
Definition string.cc:277
bool startswith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition string.h:20
std::string encode_url(const std::string &str)
Urlencode a string.
Definition string.cc:311
std::string decode_url(const std::string &str)
Decode an urlencoded string.
Definition string.cc:329
std::string lower(const std::string &str)
Return a lowercased copy of str.
Definition string.h:165
std::string basename(const std::string &pathname)
Given a pathname, return the file name without its path.
Definition string.cc:9
std::string rstrip(const std::string &str, const FUN &classifier)
Return the substring of 'str' without all trailing characters for which 'classifier' returns true.
Definition string.h:103
void appendpath(std::string &dest, const char *path2)
Append path2 to path1, adding slashes when appropriate.
Definition string.cc:45
std::string joinpath(const std::string &path1, const std::string &path2)
Definition string.cc:97
std::string upper(const std::string &str)
Return an uppercased copy of str.
Definition string.h:155
String functions.
Definition apt.cc:40
set< string > & res
Definition packagerecord.cc:73
Split a string where a given substring is found.
Definition string.h:223
std::string sep
Separator.
Definition string.h:227
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition string.h:232
Split(const std::string &str, const std::string &sep, bool skip_empty=false)
Definition string.h:234
const_iterator end()
Return the end iterator to string split.
Definition string.h:270
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition string.h:267
std::string str
String to split.
Definition string.h:225