libwreport  3.38
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 
76 std::string lstrip(const std::string& str);
77 
81 std::string rstrip(const std::string& str);
82 
86 std::string strip(const std::string& str);
87 
89 inline std::string upper(const std::string& str)
90 {
91  std::string res;
92  res.reserve(str.size());
93  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
94  res += static_cast<char>(std::toupper(static_cast<unsigned char>(*i)));
95  return res;
96 }
97 
99 inline std::string lower(const std::string& str)
100 {
101  std::string res;
102  res.reserve(str.size());
103  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
104  res += static_cast<char>(std::tolower(static_cast<unsigned char>(*i)));
105  return res;
106 }
107 
109 [[deprecated("Use path.filename")]] std::string basename(const std::string& pathname);
110 
112 [[deprecated("Use path.parent_path")]] std::string dirname(const std::string& pathname);
113 
115 [[deprecated("Use path / path")]] void appendpath(std::string& dest, const char* path2);
116 
118 [[deprecated("Use path / path")]] void appendpath(std::string& dest, const std::string& path2);
119 
121 template<typename S1, typename S2, typename... Args>
122 [[deprecated("Use path / path")]] void appendpath(std::string& dest, S1 first, S2 second, Args... next)
123 {
124 #pragma GCC diagnostic push
125 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
126  appendpath(dest, first);
127  appendpath(dest, second, next...);
128 #pragma GCC diagnostic pop
129 }
130 
132 template<typename... Args>
133 [[deprecated("Use path / path")]] std::string joinpath(Args... components)
134 {
135 #pragma GCC diagnostic push
136 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
137  std::string res;
138  appendpath(res, components...);
139  return res;
140 #pragma GCC diagnostic pop
141 }
142 
148 [[deprecated("use path::lexically_normal or std::filesystem::canonical")]] std::string normpath(const std::string& pathname);
149 
162 struct Split
163 {
165  std::string str;
167  std::string sep;
173 
174  Split(const std::string& str_, const std::string& sep_, bool skip_empty_=false)
175  : str(str_), sep(sep_), skip_empty(skip_empty_) {}
176 
178  {
179  protected:
180  const Split* split = nullptr;
182  std::string cur;
184  size_t end = 0;
185 
188 
189  public:
190  using iterator_category = std::input_iterator_tag;
191  using value_type = std::string;
192  using difference_type = int;
193  using pointer = std::string*;
194  using reference = std::string&;
195 
197  const_iterator(const Split& split);
200  const_iterator(const const_iterator&) = default;
201  ~const_iterator();
202 
203  const_iterator& operator++();
204  const std::string& operator*() const;
205  const std::string* operator->() const;
206 
207  std::string remainder() const;
208 
209  const_iterator& operator=(const const_iterator&) = default;
210  bool operator==(const const_iterator& ti) const;
211  bool operator!=(const const_iterator& ti) const;
212  };
213 
215  const_iterator begin() { return const_iterator(*this); }
216 
219 };
220 
224 std::string encode_cstring(const std::string& str);
225 
233 std::string decode_cstring(const std::string& str, size_t& lenParsed);
234 
236 std::string encode_url(const std::string& str);
237 
239 std::string decode_url(const std::string& str);
240 
242 std::string encode_base64(const std::string& str);
243 
245 std::string encode_base64(const void* data, size_t size);
246 
248 std::string decode_base64(const std::string& str);
249 
250 }
251 }
252 #endif
Definition: string.h:178
size_t end
Position of the first character of the next token.
Definition: string.h:184
const_iterator()
End iterator.
Definition: string.h:199
std::string cur
Current token.
Definition: string.h:182
const_iterator(const Split &split)
Begin iterator.
void skip_separators()
Move end past all the consecutive separators that start at its position.
String functions.
Definition: benchmark.h:13
Split a string where a given substring is found.
Definition: string.h:163
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition: string.h:172
const_iterator end()
Return the end iterator to string split.
Definition: string.h:218
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:215
std::string sep
Separator.
Definition: string.h:167
std::string str
String to split.
Definition: string.h:165