/usr/include/ept/utils/string.h is in libept-dev 1.1+nmu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | #ifndef EPT_STRING_H
#define EPT_STRING_H
/**
* @author Enrico Zini <enrico@enricozini.org>
* @brief String functions
*
* Copyright (C) 2007--2015 Enrico Zini <enrico@debian.org>
*/
#include <string>
#include <functional>
#include <sstream>
#include <cctype>
namespace ept {
namespace str {
/// Check if a string starts with the given substring
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;
}
/// Check if a string ends with the given substring
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;
}
/**
* Stringify and join a sequence of objects
*/
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();
}
/**
* Stringify and join an iterable container
*/
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();
}
/**
* Return the substring of 'str' without all leading characters for which
* 'classifier' returns true.
*/
template<typename FUN>
inline std::string lstrip(const std::string& str, const FUN& classifier)
{
if (str.empty())
return str;
size_t beg = 0;
while (beg < str.size() && classifier(str[beg]))
++beg;
return str.substr(beg, str.size() - beg + 1);
}
/**
* Return the substring of 'str' without all leading spaces.
*/
inline std::string lstrip(const std::string& str)
{
return lstrip(str, ::isspace);
}
/**
* Return the substring of 'str' without all trailing characters for which
* 'classifier' returns true.
*/
template<typename FUN>
inline std::string rstrip(const std::string& str, const FUN& classifier)
{
if (str.empty())
return str;
size_t end = str.size();
while (end > 0 && classifier(str[end - 1]))
--end;
if (end == 0)
return std::string();
else
return str.substr(0, end);
}
/**
* Return the substring of 'str' without all trailing spaces.
*/
inline std::string rstrip(const std::string& str)
{
return rstrip(str, ::isspace);
}
/**
* Return the substring of 'str' without all leading and trailing characters
* for which 'classifier' returns true.
*/
template<typename FUN>
inline std::string strip(const std::string& str, const FUN& classifier)
{
if (str.empty())
return str;
size_t beg = 0;
size_t end = str.size() - 1;
while (beg < end && classifier(str[beg]))
++beg;
while (end >= beg && classifier(str[end]))
--end;
return str.substr(beg, end-beg+1);
}
/**
* Return the substring of 'str' without all leading and trailing spaces.
*/
inline std::string strip(const std::string& str)
{
return strip(str, ::isspace);
}
/// Return an uppercased copy of 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;
}
/// Return a lowercased copy of str
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;
}
/// Given a pathname, return the file name without its path
std::string basename(const std::string& pathname);
/// Given a pathname, return the directory name without the file name
std::string dirname(const std::string& pathname);
/// Append path2 to path1, adding slashes when appropriate
void appendpath(std::string& dest, const char* path2);
/// Append path2 to path1, adding slashes when appropriate
void appendpath(std::string& dest, const std::string& path2);
/// Append an arbitrary number of path components to \a dest
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...);
}
/// Join two or more paths, adding slashes when appropriate
template<typename... Args>
std::string joinpath(Args... components)
{
std::string res;
appendpath(res, components...);
return res;
}
/**
* Normalise a pathname.
*
* For example, A//B, A/./B and A/foo/../B all become A/B.
*/
std::string normpath(const std::string& pathname);
/**
* Split a string where a given substring is found
*
* This does a similar work to the split functions of perl, python and ruby.
*
* Example code:
* \code
* str::Split splitter(my_string, "/");
* vector<string> split;
* std::copy(splitter.begin(), splitter.end(), back_inserter(split));
* \endcode
*/
struct Split
{
/// String to split
std::string str;
/// Separator
std::string sep;
/**
* If true, skip empty tokens, effectively grouping consecutive separators
* as if they were a single one
*/
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 : public std::iterator<std::input_iterator_tag, std::string>
{
protected:
const Split* split = nullptr;
/// Current token
std::string cur;
/// Position of the first character of the next token
size_t end = 0;
/// Move end past all the consecutive separators that start at its position
void skip_separators();
public:
/// Begin iterator
const_iterator(const Split& split);
/// End iterator
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;
};
/// Return the begin iterator to split a string on instances of sep
const_iterator begin() { return const_iterator(*this); }
/// Return the end iterator to string split
const_iterator end() { return const_iterator(); }
};
/**
* Escape the string so it can safely used as a C string inside double quotes
*/
std::string encode_cstring(const std::string& str);
/**
* Unescape a C string, stopping at the first double quotes or at the end of
* the string.
*
* lenParsed is set to the number of characters that were pased (which can be
* greather than the size of the resulting string in case escapes were found)
*/
std::string decode_cstring(const std::string& str, size_t& lenParsed);
/// Urlencode a string
std::string encode_url(const std::string& str);
/// Decode an urlencoded string
std::string decode_url(const std::string& str);
/// Encode a string in Base64
std::string encode_base64(const std::string& str);
/// Decode a string encoded in Base64
std::string decode_base64(const std::string& str);
}
}
#endif
|