libwreport  3.42
string.h
1 #ifndef WREPORT_STRING_H
2 #define WREPORT_STRING_H
3 
11 #include <cctype>
12 #include <functional>
13 #include <sstream>
14 #include <string>
15 
16 namespace wreport::str {
17 
19 inline bool startswith(const std::string& str, const std::string& part)
20 {
21  if (str.size() < part.size())
22  return false;
23  return str.substr(0, part.size()) == part;
24 }
25 
27 inline bool endswith(const std::string& str, const std::string& part)
28 {
29  if (str.size() < part.size())
30  return false;
31  return str.substr(str.size() - part.size()) == part;
32 }
33 
37 template <typename ITER>
38 std::string join(const std::string& sep, const ITER& begin, const ITER& end)
39 {
40  std::stringstream res;
41  bool first = true;
42  for (ITER i = begin; i != end; ++i)
43  {
44  if (first)
45  first = false;
46  else
47  res << sep;
48  res << *i;
49  }
50  return res.str();
51 }
52 
56 template <typename ITEMS>
57 std::string join(const std::string& sep, const ITEMS& items)
58 {
59  std::stringstream res;
60  bool first = true;
61  for (const auto& i : items)
62  {
63  if (first)
64  first = false;
65  else
66  res << sep;
67  res << i;
68  }
69  return res.str();
70 }
71 
75 std::string lstrip(const std::string& str);
76 
80 std::string rstrip(const std::string& str);
81 
85 std::string strip(const std::string& str);
86 
88 inline std::string upper(const std::string& str)
89 {
90  std::string res;
91  res.reserve(str.size());
92  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
93  res += static_cast<char>(std::toupper(static_cast<unsigned char>(*i)));
94  return res;
95 }
96 
98 inline std::string lower(const std::string& str)
99 {
100  std::string res;
101  res.reserve(str.size());
102  for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
103  res += static_cast<char>(std::tolower(static_cast<unsigned char>(*i)));
104  return res;
105 }
106 
108 [[deprecated("Use path.filename")]] std::string
109 basename(const std::string& pathname);
110 
112 [[deprecated("Use path.parent_path")]] std::string
113 dirname(const std::string& pathname);
114 
116 [[deprecated("Use path / path")]] void appendpath(std::string& dest,
117  const char* path2);
118 
120 [[deprecated("Use path / path")]] void appendpath(std::string& dest,
121  const std::string& path2);
122 
124 template <typename S1, typename S2, typename... Args>
125 [[deprecated("Use path / path")]] void appendpath(std::string& dest, S1 first,
126  S2 second, Args... next)
127 {
128 #pragma GCC diagnostic push
129 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
130  appendpath(dest, first);
131  appendpath(dest, second, next...);
132 #pragma GCC diagnostic pop
133 }
134 
136 template <typename... Args>
137 [[deprecated("Use path / path")]] std::string joinpath(Args... components)
138 {
139 #pragma GCC diagnostic push
140 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
141  std::string res;
142  appendpath(res, components...);
143  return res;
144 #pragma GCC diagnostic pop
145 }
146 
152 [[deprecated(
153  "use path::lexically_normal or std::filesystem::canonical")]] std::string
154 normpath(const std::string& pathname);
155 
168 struct Split
169 {
171  std::string str;
173  std::string sep;
179 
180  Split(const std::string& str_, const std::string& sep_,
181  bool skip_empty_ = false)
182  : str(str_), sep(sep_), skip_empty(skip_empty_)
183  {
184  }
185 
187  {
188  protected:
189  const Split* split = nullptr;
191  std::string cur;
193  size_t end = 0;
194 
197  void skip_separators();
198 
199  public:
200  using iterator_category = std::input_iterator_tag;
201  using value_type = std::string;
202  using difference_type = int;
203  using pointer = std::string*;
204  using reference = std::string&;
205 
207  const_iterator(const Split& split);
210  const_iterator(const const_iterator&) = default;
211  ~const_iterator();
212 
213  const_iterator& operator++();
214  const std::string& operator*() const;
215  const std::string* operator->() const;
216 
217  std::string remainder() const;
218 
219  const_iterator& operator=(const const_iterator&) = default;
220  bool operator==(const const_iterator& ti) const;
221  bool operator!=(const const_iterator& ti) const;
222  };
223 
225  const_iterator begin() { return const_iterator(*this); }
226 
229 };
230 
234 std::string encode_cstring(const std::string& str);
235 
243 std::string decode_cstring(const std::string& str, size_t& lenParsed);
244 
246 std::string encode_url(const std::string& str);
247 
249 std::string decode_url(const std::string& str);
250 
252 std::string encode_base64(const std::string& str);
253 
255 std::string encode_base64(const void* data, size_t size);
256 
258 std::string decode_base64(const std::string& str);
259 
260 } // namespace wreport::str
261 #endif
void skip_separators()
Move end past all the consecutive separators that start at its position.
std::string rstrip(const std::string &str)
Return the substring of &#39;str&#39; without all trailing spaces.
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
std::string strip(const std::string &str)
Return the substring of &#39;str&#39; without all leading and trailing spaces.
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one...
Definition: string.h:178
std::string basename(const std::string &pathname)
Given a pathname, return the file name without its path.
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:225
std::string normpath(const std::string &pathname)
Normalise a pathname.
void appendpath(std::string &dest, const char *path2)
Append path2 to path1, adding slashes when appropriate.
std::string decode_base64(const std::string &str)
Decode a string encoded in Base64.
Split a string where a given substring is found.
Definition: string.h:168
std::string joinpath(Args... components)
Join two or more paths, adding slashes when appropriate.
Definition: string.h:137
std::string encode_url(const std::string &str)
Urlencode a string.
Definition: string.h:186
size_t end
Position of the first character of the next token.
Definition: string.h:193
std::string lstrip(const std::string &str)
Return the substring of &#39;str&#39; without all leading spaces.
bool endswith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition: string.h:27
std::string encode_cstring(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
String functions.
Definition: string.h:16
std::string encode_base64(const std::string &str)
Encode a string in Base64.
std::string sep
Separator.
Definition: string.h:173
const_iterator end()
Return the end iterator to string split.
Definition: string.h:228
const_iterator()
End iterator.
Definition: string.h:209
std::string cur
Current token.
Definition: string.h:191
std::string decode_cstring(const std::string &str, size_t &lenParsed)
Unescape a C string, stopping at the first double quotes or at the end of the string.
std::string join(const std::string &sep, const ITER &begin, const ITER &end)
Stringify and join a sequence of objects.
Definition: string.h:38
std::string lower(const std::string &str)
Return a lowercased copy of str.
Definition: string.h:98
std::string upper(const std::string &str)
Return an uppercased copy of str.
Definition: string.h:88
std::string decode_url(const std::string &str)
Decode an urlencoded string.
bool startswith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition: string.h:19
std::string str
String to split.
Definition: string.h:171