1 #ifndef RADARELAB_UTILS_STRING_H
2 #define RADARELAB_UTILS_STRING_H
21 inline bool startswith(
const std::string& str,
const std::string& part)
23 if (str.size() < part.size())
25 return str.substr(0, part.size()) == part;
29 inline bool endswith(
const std::string& str,
const std::string& part)
31 if (str.size() < part.size())
33 return str.substr(str.size() - part.size()) == part;
39 template<
typename ITER>
40 std::string join(
const std::string& sep,
const ITER& begin,
const ITER& end)
42 std::stringstream res;
44 for (ITER i = begin; i != end; ++i)
58 template<
typename ITEMS>
59 std::string join(
const std::string& sep,
const ITEMS& items)
61 std::stringstream res;
63 for (
const auto& i: items)
78 template<
typename FUN>
79 inline std::string lstrip(
const std::string& str,
const FUN& classifier)
85 while (beg < str.size() && classifier(str[beg]))
88 return str.substr(beg, str.size() - beg + 1);
94 inline std::string lstrip(
const std::string& str)
96 return lstrip(str, ::isspace);
103 template<
typename FUN>
104 inline std::string rstrip(
const std::string& str,
const FUN& classifier)
109 size_t end = str.size();
110 while (end > 0 && classifier(str[end - 1]))
114 return std::string();
116 return str.substr(0, end);
122 inline std::string rstrip(
const std::string& str)
124 return rstrip(str, ::isspace);
131 template<
typename FUN>
132 inline std::string strip(
const std::string& str,
const FUN& classifier)
138 size_t end = str.size() - 1;
139 while (beg < end && classifier(str[beg]))
141 while (end >= beg && classifier(str[end]))
144 return str.substr(beg, end-beg+1);
150 inline std::string strip(
const std::string& str)
152 return strip(str, ::isspace);
156 inline std::string upper(
const std::string& str)
159 res.reserve(str.size());
160 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
161 res += ::toupper(*i);
166 inline std::string lower(
const std::string& str)
169 res.reserve(str.size());
170 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
171 res += ::tolower(*i);
176 std::string basename(
const std::string& pathname);
179 std::string dirname(
const std::string& pathname);
182 void appendpath(std::string& dest,
const char* path2);
185 void appendpath(std::string& dest,
const std::string& path2);
188 template<
typename S1,
typename S2,
typename... Args>
189 void appendpath(std::string& dest, S1 first, S2 second, Args... next)
191 appendpath(dest, first);
192 appendpath(dest, second, next...);
196 template<
typename... Args>
197 std::string joinpath(Args... components)
200 appendpath(res, components...);
209 std::string normpath(
const std::string& pathname);
235 Split(
const std::string& str,
const std::string& sep,
bool skip_empty=
false)
238 class const_iterator :
public std::iterator<std::input_iterator_tag, std::string>
241 const Split* split =
nullptr;
248 void skip_separators();
252 const_iterator(
const Split& split);
257 const_iterator& operator++();
258 const std::string& operator*()
const;
259 const std::string* operator->()
const;
261 std::string remainder()
const;
263 bool operator==(
const const_iterator& ti)
const;
264 bool operator!=(
const const_iterator& ti)
const;
268 const_iterator
begin() {
return const_iterator(*
this); }
271 const_iterator
end() {
return const_iterator(); }
277 std::string encode_cstring(
const std::string& str);
286 std::string decode_cstring(
const std::string& str,
size_t& lenParsed);
289 std::string encode_url(
const std::string& str);
292 std::string decode_url(
const std::string& str);
295 std::string encode_base64(
const std::string& str);
298 std::string encode_base64(
const void* data,
size_t size);
301 std::string decode_base64(
const std::string& str);
const_iterator end()
Return the end iterator to string split.
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Split a string where a given substring is found.
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one...
std::string str
String to split.
std::string sep
Separator.