libwreport  3.6
string.h
1 #ifndef WREPORT_STRING_H
2 #define WREPORT_STRING_H
3 
11 #include <string>
12 #include <functional>
13 #include <sstream>
14 #include <cctype>
15 
16 namespace wreport {
17 namespace str {
18 
20 inline 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 
28 inline 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 
38 template<typename ITER>
39 std::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 
57 template<typename ITEMS>
58 std::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 
77 template<typename FUN>
78 inline 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 
93 inline std::string lstrip(const std::string& str)
94 {
95  return lstrip(str, ::isspace);
96 }
97 
102 template<typename FUN>
103 inline 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 
121 inline std::string rstrip(const std::string& str)
122 {
123  return rstrip(str, ::isspace);
124 }
125 
130 template<typename FUN>
131 inline 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 
149 inline std::string strip(const std::string& str)
150 {
151  return strip(str, ::isspace);
152 }
153 
155 inline 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 
165 inline 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 
175 std::string basename(const std::string& pathname);
176 
178 std::string dirname(const std::string& pathname);
179 
181 void appendpath(std::string& dest, const char* path2);
182 
184 void appendpath(std::string& dest, const std::string& path2);
185 
187 template<typename S1, typename S2, typename... Args>
188 void appendpath(std::string& dest, S1 first, S2 second, Args... next)
189 {
190  appendpath(dest, first);
191  appendpath(dest, second, next...);
192 }
193 
195 template<typename... Args>
196 std::string joinpath(Args... components)
197 {
198  std::string res;
199  appendpath(res, components...);
200  return res;
201 }
202 
208 std::string normpath(const std::string& pathname);
209 
222 struct 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)
235  : str(str), sep(sep), skip_empty(skip_empty) {}
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);
254  ~const_iterator();
255 
256  const_iterator& operator++();
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 
267  const_iterator begin() { return const_iterator(*this); }
268 
271 };
272 
276 std::string encode_cstring(const std::string& str);
277 
285 std::string decode_cstring(const std::string& str, size_t& lenParsed);
286 
288 std::string encode_url(const std::string& str);
289 
291 std::string decode_url(const std::string& str);
292 
294 std::string encode_base64(const std::string& str);
295 
297 std::string encode_base64(const void* data, size_t size);
298 
300 std::string decode_base64(const std::string& str);
301 
302 }
303 }
304 #endif
void skip_separators()
Move end past all the consecutive separators that start at its position.
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one...
Definition: string.h:232
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:267
Split a string where a given substring is found.
Definition: string.h:222
Definition: string.h:237
String functions.
Definition: benchmark.h:13
std::string sep
Separator.
Definition: string.h:227
const_iterator end()
Return the end iterator to string split.
Definition: string.h:270
const_iterator()
End iterator.
Definition: string.h:253
std::string cur
Current token.
Definition: string.h:242
std::string str
String to split.
Definition: string.h:225