Elaboradar  0.1
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 
77 std::string lstrip(const std::string& str);
78 
82 std::string rstrip(const std::string& str);
83 
87 std::string strip(const std::string& str);
88 
90 inline 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 
100 inline 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 
110 std::string basename(const std::string& pathname);
111 
113 std::string dirname(const std::string& pathname);
114 
116 void appendpath(std::string& dest, const char* path2);
117 
119 void appendpath(std::string& dest, const std::string& path2);
120 
122 template<typename S1, typename S2, typename... Args>
123 void appendpath(std::string& dest, S1 first, S2 second, Args... next)
124 {
125  appendpath(dest, first);
126  appendpath(dest, second, next...);
127 }
128 
130 template<typename... Args>
131 std::string joinpath(Args... components)
132 {
133  std::string res;
134  appendpath(res, components...);
135  return res;
136 }
137 
143 std::string normpath(const std::string& pathname);
144 
157 struct 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)
170  : str(str), sep(sep), skip_empty(skip_empty) {}
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 
217 std::string encode_cstring(const std::string& str);
218 
226 std::string decode_cstring(const std::string& str, size_t& lenParsed);
227 
229 std::string encode_url(const std::string& str);
230 
232 std::string decode_url(const std::string& str);
233 
235 std::string encode_base64(const std::string& str);
236 
238 std::string encode_base64(const void* data, size_t size);
239 
241 std::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