Elaboradar  0.1
 Tutto Classi Namespace File Funzioni Variabili Tipi enumerati (enum) Gruppi
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 
16 namespace radarelab {
17 namespace utils {
18 namespace str {
19 
21 inline 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 
29 inline 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 
39 template<typename ITER>
40 std::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 
58 template<typename ITEMS>
59 std::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 
78 template<typename FUN>
79 inline std::string lstrip(const std::string& str, const FUN& classifier)
80 {
81  if (str.empty())
82  return str;
83 
84  size_t beg = 0;
85  while (beg < str.size() && classifier(str[beg]))
86  ++beg;
87 
88  return str.substr(beg, str.size() - beg + 1);
89 }
90 
94 inline std::string lstrip(const std::string& str)
95 {
96  return lstrip(str, ::isspace);
97 }
98 
103 template<typename FUN>
104 inline std::string rstrip(const std::string& str, const FUN& classifier)
105 {
106  if (str.empty())
107  return str;
108 
109  size_t end = str.size();
110  while (end > 0 && classifier(str[end - 1]))
111  --end;
112 
113  if (end == 0)
114  return std::string();
115  else
116  return str.substr(0, end);
117 }
118 
122 inline std::string rstrip(const std::string& str)
123 {
124  return rstrip(str, ::isspace);
125 }
126 
131 template<typename FUN>
132 inline std::string strip(const std::string& str, const FUN& classifier)
133 {
134  if (str.empty())
135  return str;
136 
137  size_t beg = 0;
138  size_t end = str.size() - 1;
139  while (beg < end && classifier(str[beg]))
140  ++beg;
141  while (end >= beg && classifier(str[end]))
142  --end;
143 
144  return str.substr(beg, end-beg+1);
145 }
146 
150 inline std::string strip(const std::string& str)
151 {
152  return strip(str, ::isspace);
153 }
154 
156 inline std::string upper(const std::string& str)
157 {
158  std::string res;
159  res.reserve(str.size());
160  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
161  res += ::toupper(*i);
162  return res;
163 }
164 
166 inline std::string lower(const std::string& str)
167 {
168  std::string res;
169  res.reserve(str.size());
170  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
171  res += ::tolower(*i);
172  return res;
173 }
174 
176 std::string basename(const std::string& pathname);
177 
179 std::string dirname(const std::string& pathname);
180 
182 void appendpath(std::string& dest, const char* path2);
183 
185 void appendpath(std::string& dest, const std::string& path2);
186 
188 template<typename S1, typename S2, typename... Args>
189 void appendpath(std::string& dest, S1 first, S2 second, Args... next)
190 {
191  appendpath(dest, first);
192  appendpath(dest, second, next...);
193 }
194 
196 template<typename... Args>
197 std::string joinpath(Args... components)
198 {
199  std::string res;
200  appendpath(res, components...);
201  return res;
202 }
203 
209 std::string normpath(const std::string& pathname);
210 
223 struct Split
224 {
226  std::string str;
228  std::string sep;
234 
235  Split(const std::string& str, const std::string& sep, bool skip_empty=false)
236  : str(str), sep(sep), skip_empty(skip_empty) {}
237 
238  class const_iterator : public std::iterator<std::input_iterator_tag, std::string>
239  {
240  protected:
241  const Split* split = nullptr;
243  std::string cur;
245  size_t end = 0;
246 
248  void skip_separators();
249 
250  public:
252  const_iterator(const Split& split);
254  const_iterator() {}
255  ~const_iterator();
256 
257  const_iterator& operator++();
258  const std::string& operator*() const;
259  const std::string* operator->() const;
260 
261  std::string remainder() const;
262 
263  bool operator==(const const_iterator& ti) const;
264  bool operator!=(const const_iterator& ti) const;
265  };
266 
268  const_iterator begin() { return const_iterator(*this); }
269 
271  const_iterator end() { return const_iterator(); }
272 };
273 
277 std::string encode_cstring(const std::string& str);
278 
286 std::string decode_cstring(const std::string& str, size_t& lenParsed);
287 
289 std::string encode_url(const std::string& str);
290 
292 std::string decode_url(const std::string& str);
293 
295 std::string encode_base64(const std::string& str);
296 
298 std::string encode_base64(const void* data, size_t size);
299 
301 std::string decode_base64(const std::string& str);
302 
303 }
304 }
305 }
306 #endif
const_iterator end()
Return the end iterator to string split.
Definition: string.h:271
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:268
Split a string where a given substring is found.
Definition: string.h:223
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one...
Definition: string.h:233
std::string str
String to split.
Definition: string.h:226
std::string sep
Separator.
Definition: string.h:228