Elaboradar 0.1
Caricamento in corso...
Ricerca in corso...
Nessun risultato
string.h
1#ifndef RADARELAB_UTILS_STRING_H
2#define RADARELAB_UTILS_STRING_H
3
11#include <string>
12#include <functional>
13#include <sstream>
14#include <cctype>
15
16namespace radarelab {
17namespace utils {
18namespace str {
19
21inline bool startswith(const std::string& str, const std::string& part)
22{
23 if (str.size() < part.size())
24 return false;
25 return str.substr(0, part.size()) == part;
26}
27
29inline bool endswith(const std::string& str, const std::string& part)
30{
31 if (str.size() < part.size())
32 return false;
33 return str.substr(str.size() - part.size()) == part;
34}
35
39template<typename ITER>
40std::string join(const std::string& sep, const ITER& begin, const ITER& end)
41{
42 std::stringstream res;
43 bool first = true;
44 for (ITER i = begin; i != end; ++i)
45 {
46 if (first)
47 first = false;
48 else
49 res << sep;
50 res << *i;
51 }
52 return res.str();
53}
54
58template<typename ITEMS>
59std::string join(const std::string& sep, const ITEMS& items)
60{
61 std::stringstream res;
62 bool first = true;
63 for (const auto& i: items)
64 {
65 if (first)
66 first = false;
67 else
68 res << sep;
69 res << i;
70 }
71 return res.str();
72}
73
77std::string lstrip(const std::string& str);
78
82std::string rstrip(const std::string& str);
83
87std::string strip(const std::string& str);
88
90inline std::string upper(const std::string& str)
91{
92 std::string res;
93 res.reserve(str.size());
94 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
95 res += ::toupper(*i);
96 return res;
97}
98
100inline std::string lower(const std::string& str)
101{
102 std::string res;
103 res.reserve(str.size());
104 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
105 res += ::tolower(*i);
106 return res;
107}
108
110std::string basename(const std::string& pathname);
111
113std::string dirname(const std::string& pathname);
114
116void appendpath(std::string& dest, const char* path2);
117
119void appendpath(std::string& dest, const std::string& path2);
120
122template<typename S1, typename S2, typename... Args>
123void appendpath(std::string& dest, S1 first, S2 second, Args... next)
124{
125 appendpath(dest, first);
126 appendpath(dest, second, next...);
127}
128
130template<typename... Args>
131std::string joinpath(Args... components)
132{
133 std::string res;
134 appendpath(res, components...);
135 return res;
136}
137
143std::string normpath(const std::string& pathname);
144
157struct Split
158{
160 std::string str;
162 std::string sep;
168
169 Split(const std::string& str, const std::string& sep, bool skip_empty=false)
171
172 class const_iterator
173 {
174 protected:
175 const Split* split = nullptr;
177 std::string cur;
179 size_t end = 0;
180
182 void skip_separators();
183
184 public:
185 using iterator_category = std::input_iterator_tag;
186 using value_type = std::string;
187 using difference_type = int;
188 using pointer = std::string*;
189 using reference = std::string&;
190
192 const_iterator(const Split& split);
194 const_iterator() {}
195 ~const_iterator();
196
197 const_iterator& operator++();
198 const std::string& operator*() const;
199 const std::string* operator->() const;
200
201 std::string remainder() const;
202
203 bool operator==(const const_iterator& ti) const;
204 bool operator!=(const const_iterator& ti) const;
205 };
206
208 const_iterator begin() { return const_iterator(*this); }
209
211 const_iterator end() { return const_iterator(); }
212};
213
217std::string encode_cstring(const std::string& str);
218
226std::string decode_cstring(const std::string& str, size_t& lenParsed);
227
229std::string encode_url(const std::string& str);
230
232std::string decode_url(const std::string& str);
233
235std::string encode_base64(const std::string& str);
236
238std::string encode_base64(const void* data, size_t size);
239
241std::string decode_base64(const std::string& str);
242
243}
244}
245}
246#endif
String functions.
Definition: cart.cpp:4
std::string sep
Separator.
Definition: string.h:162
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:208
const_iterator end()
Return the end iterator to string split.
Definition: string.h:211
std::string str
String to split.
Definition: string.h:160
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition: string.h:167
Split a string where a given substring is found.
Definition: string.h:158