libept
string.h
Go to the documentation of this file.
1 #ifndef EPT_STRING_H
2 #define EPT_STRING_H
3 
11 #include <string>
12 #include <functional>
13 #include <sstream>
14 #include <cctype>
15 
16 namespace ept {
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 
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 decode_base64(const std::string& str);
298 
299 }
300 }
301 #endif
const_iterator end()
Return the end iterator to string split.
Definition: string.h:270
const std::string * operator->() const
Definition: string.cc:236
size_t end
Position of the first character of the next token.
Definition: string.h:244
void skip_separators()
Move end past all the consecutive separators that start at its position.
Definition: string.cc:154
std::string upper(const std::string &str)
Return an uppercased copy of str.
Definition: string.h:155
Split a string where a given substring is found.
Definition: string.h:222
std::string encode_url(const std::string &str)
Urlencode a string.
Definition: string.cc:311
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
Definition: string.cc:18
std::string decode_base64(const std::string &str)
Decode a string encoded in Base64.
Definition: string.cc:392
std::string lower(const std::string &str)
Return a lowercased copy of str.
Definition: string.h:165
std::string rstrip(const std::string &str, const FUN &classifier)
Return the substring of &#39;str&#39; without all trailing characters for which &#39;classifier&#39; returns true...
Definition: string.h:103
std::string cur
Current token.
Definition: string.h:242
std::string remainder() const
Definition: string.cc:146
void appendpath(std::string &dest, const char *path2)
Append path2 to path1, adding slashes when appropriate.
Definition: string.cc:45
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition: string.h:267
String functions.
Definition: apt.cc:38
std::string basename(const std::string &pathname)
Given a pathname, return the file name without its path.
Definition: string.cc:9
const std::string & operator*() const
Definition: string.cc:235
std::string lstrip(const std::string &str, const FUN &classifier)
Return the substring of &#39;str&#39; without all leading characters for which &#39;classifier&#39; returns true...
Definition: string.h:78
const_iterator()
End iterator.
Definition: string.h:253
std::string str
String to split.
Definition: string.h:225
Split(const std::string &str, const std::string &sep, bool skip_empty=false)
Definition: string.h:234
std::string joinpath(const std::string &path1, const std::string &path2)
Definition: string.cc:97
std::string join(const std::string &sep, const ITER &begin, const ITER &end)
Stringify and join a sequence of objects.
Definition: string.h:39
std::string encode_base64(const std::string &str)
Encode a string in Base64.
Definition: string.cc:359
std::string encode_cstring(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
Definition: string.cc:253
std::string strip(const std::string &str, const FUN &classifier)
Return the substring of &#39;str&#39; without all leading and trailing characters for which &#39;classifier&#39; retu...
Definition: string.h:131
std::string decode_url(const std::string &str)
Decode an urlencoded string.
Definition: string.cc:329
const Split * split
Definition: string.h:240
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one...
Definition: string.h:232
bool endswith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition: string.h:28
std::string sep
Separator.
Definition: string.h:227
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.
Definition: string.cc:277
bool startswith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition: string.h:20
bool operator!=(const const_iterator &ti) const
Definition: string.cc:245
~const_iterator()
Definition: string.cc:142
bool operator==(const const_iterator &ti) const
Definition: string.cc:238
Definition: string.h:237
set< string > & res
Definition: packagerecord.cc:73
const_iterator & operator++()
Definition: string.cc:172
std::string normpath(const std::string &pathname)
Normalise a pathname.
Definition: string.cc:104