string.h
#ifndef WREPORT_STRING_H
#define WREPORT_STRING_H
#include <string>
#include <functional>
#include <sstream>
#include <cctype>
namespace wreport {
namespace str {
inline bool startswith(const std::string& str, const std::string& part)
{
if (str.size() < part.size())
return false;
return str.substr(0, part.size()) == part;
}
inline bool endswith(const std::string& str, const std::string& part)
{
if (str.size() < part.size())
return false;
return str.substr(str.size() - part.size()) == part;
}
template<typename ITER>
std::string join(const std::string& sep, const ITER& begin, const ITER& end)
{
std::stringstream res;
bool first = true;
for (ITER i = begin; i != end; ++i)
{
if (first)
first = false;
else
res << sep;
res << *i;
}
return res.str();
}
template<typename ITEMS>
std::string join(const std::string& sep, const ITEMS& items)
{
std::stringstream res;
bool first = true;
for (const auto& i: items)
{
if (first)
first = false;
else
res << sep;
res << i;
}
return res.str();
}
std::string lstrip(const std::string& str);
std::string rstrip(const std::string& str);
std::string strip(const std::string& str);
inline std::string upper(const std::string& str)
{
std::string res;
res.reserve(str.size());
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
res += ::toupper(*i);
return res;
}
inline std::string lower(const std::string& str)
{
std::string res;
res.reserve(str.size());
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
res += ::tolower(*i);
return res;
}
std::string basename(const std::string& pathname);
std::string dirname(const std::string& pathname);
void appendpath(std::string& dest, const char* path2);
void appendpath(std::string& dest, const std::string& path2);
template<typename S1, typename S2, typename... Args>
void appendpath(std::string& dest, S1 first, S2 second, Args... next)
{
appendpath(dest, first);
appendpath(dest, second, next...);
}
template<typename... Args>
std::string joinpath(Args... components)
{
std::string res;
appendpath(res, components...);
return res;
}
std::string normpath(const std::string& pathname);
struct Split
{
std::string str;
std::string sep;
bool skip_empty;
Split(const std::string& str, const std::string& sep, bool skip_empty=false)
: str(str), sep(sep), skip_empty(skip_empty) {}
class const_iterator
{
protected:
const Split* split = nullptr;
std::string cur;
size_t end = 0;
void skip_separators();
public:
using iterator_category = std::input_iterator_tag;
using value_type = std::string;
using difference_type = int;
using pointer = std::string*;
using reference = std::string&;
const_iterator(const Split& split);
const_iterator() {}
~const_iterator();
const_iterator& operator++();
const std::string& operator*() const;
const std::string* operator->() const;
std::string remainder() const;
bool operator==(const const_iterator& ti) const;
bool operator!=(const const_iterator& ti) const;
};
const_iterator begin() { return const_iterator(*this); }
const_iterator end() { return const_iterator(); }
};
std::string encode_cstring(const std::string& str);
std::string decode_cstring(const std::string& str, size_t& lenParsed);
std::string encode_url(const std::string& str);
std::string decode_url(const std::string& str);
std::string encode_base64(const std::string& str);
std::string encode_base64(const void* data, size_t size);
std::string decode_base64(const std::string& str);
}
}
#endif
wreport::str::Split::const_iterator
Definition string.h:172
wreport::str::Split::const_iterator::end
size_t end
Position of the first character of the next token.
Definition string.h:178
wreport::str::Split::const_iterator::const_iterator
const_iterator()
End iterator.
Definition string.h:193
wreport::str::Split::const_iterator::cur
std::string cur
Current token.
Definition string.h:176
wreport::str::Split::const_iterator::const_iterator
const_iterator(const Split &split)
Begin iterator.
wreport::str::Split::const_iterator::skip_separators
void skip_separators()
Move end past all the consecutive separators that start at its position.
wreport
String functions.
Definition benchmark.h:13
wreport::str::Split::skip_empty
bool skip_empty
If true, skip empty tokens, effectively grouping consecutive separators as if they were a single one.
Definition string.h:166
wreport::str::Split::end
const_iterator end()
Return the end iterator to string split.
Definition string.h:210
wreport::str::Split::begin
const_iterator begin()
Return the begin iterator to split a string on instances of sep.
Definition string.h:207
wreport::str::Split::sep
std::string sep
Separator.
Definition string.h:161
wreport::str::Split::str
std::string str
String to split.
Definition string.h:159