/usr/include/libfilezilla/string.hpp is in libfilezilla-dev 0.11.0-1.
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | #ifndef LIBFILEZILLA_STRING_HEADER
#define LIBFILEZILLA_STRING_HEADER
#include "libfilezilla.hpp"
#include <string>
#include <vector>
/** \file
* \brief String types and assorted functions.
*
* Defines the \ref fz::native_string type and offers various functions to convert between
* different string types.
*/
namespace fz {
/** \typedef native_string
*
* \brief A string in the system's native character type and encoding.\n Note: This typedef changes depending on platform!
*
* On Windows, the system's native encoding is UTF-16, so native_string is typedef'ed to std::wstring.
*
* On all other platform, native_string is a typedef for std::string.
*
* Always using native_string has the benefit that no conversion needs to be performed which is especially useful
* if dealing with filenames.
*/
#ifdef FZ_WINDOWS
typedef std::wstring native_string;
#endif
#if defined(FZ_UNIX) || defined(FZ_MAC)
typedef std::string native_string;
#endif
/** \brief Converts std::string to native_string.
*
* \return the converted string on success. On failure an empty string is returned.
*/
native_string FZ_PUBLIC_SYMBOL to_native(std::string const& in);
/** \brief Convert std::wstring to native_string.
*
* \return the converted string on success. On failure an empty string is returned.
*/
native_string FZ_PUBLIC_SYMBOL to_native(std::wstring const& in);
/** \brief Locale-sensitive stricmp
*
* Like std::string::strcmp but case-insensitive, respecting locale.
*
* \note does not handle embedded null
*/
int FZ_PUBLIC_SYMBOL stricmp(std::string const& a, std::string const& b);
int FZ_PUBLIC_SYMBOL stricmp(std::wstring const& a, std::wstring const& b);
/** \brief Converts ASCII uppercase characters to lowercase as if C-locale is used.
Under some locales there is a different case-relationship
between the letters a-z and A-Z as one expects from ASCII under the C locale.
In Turkish for example there are different variations of the letter i,
namely dotted and dotless. What we see as 'i' is the lowercase dotted i and
'I' is the uppercase dotless i. Since std::tolower is locale-aware, I would
become the dotless lowercase i.
This is not always what we want. FTP commands for example are case-insensitive
ASCII strings, LIST and list are the same.
tolower_ascii instead converts all types of 'i's to the ASCII i as well.
\return A-Z becomes a-z.\n In addition dotless lowercase i and dotted uppercase i also become the standard i.
*/
template<typename Char>
Char tolower_ascii(Char c) {
if (c >= 'A' && c <= 'Z') {
return c + ('a' - 'A');
}
return c;
}
template<>
std::wstring::value_type FZ_PUBLIC_SYMBOL tolower_ascii(std::wstring::value_type c);
/// \brief Converts ASCII lowercase characters to uppercase as if C-locale is used.
template<typename Char>
Char toupper_ascii(Char c) {
if (c >= 'a' && c <= 'z') {
return c + ('A' - 'a');
}
return c;
}
template<>
std::wstring::value_type FZ_PUBLIC_SYMBOL toupper_ascii(std::wstring::value_type c);
/** \brief tr_tolower_ascii does for strings what tolower_ascii does for individual characters
*/
// Note: For UTF-8 strings it works on individual octets!
template<typename String>
String str_tolower_ascii(String const& s)
{
String ret = s;
for (auto& c : ret) {
c = tolower_ascii(c);
}
return ret;
}
template<typename String>
String str_toupper_ascii(String const& s)
{
String ret = s;
for (auto& c : ret) {
c = toupper_ascii(c);
}
return ret;
}
/** \brief Comparator to be used for std::map for case-insentitive keys
*
* Comparision is done locale-agnostic.
* Useful for key-value pairs in protocols, e.g. HTTP headers.
*/
struct FZ_PUBLIC_SYMBOL less_insensitive_ascii final
{
template<typename T>
bool operator()(T const& lhs, T const& rhs) const {
return fz::str_tolower_ascii(lhs) < fz::str_tolower_ascii(rhs);
}
};
/** \brief Converts from std::string in system encoding into std::wstring
*
* \return the converted string on success. On failure an empty string is returned.
*
* \note Does not handle embedded nulls
*/
std::wstring FZ_PUBLIC_SYMBOL to_wstring(std::string const& in);
/// Returns identity, that way to_wstring can be called with native_string.
inline std::wstring FZ_PUBLIC_SYMBOL to_wstring(std::wstring const& in) { return in; }
/// Converts from arithmetic type to std::wstring
template<typename Arg>
inline typename std::enable_if<std::is_arithmetic<std::decay_t<Arg>>::value, std::wstring>::type to_wstring(Arg && arg)
{
return std::to_wstring(std::forward<Arg>(arg));
}
/** \brief Converts from std::string in UTF-8 into std::wstring
*
* \return the converted string on success. On failure an empty string is returned.
*/
std::wstring FZ_PUBLIC_SYMBOL to_wstring_from_utf8(std::string const& in);
std::wstring FZ_PUBLIC_SYMBOL to_wstring_from_utf8(char const* s, size_t len);
/** \brief Converts from std::wstring into std::string in system encoding
*
* \return the converted string on success. On failure an empty string is returned.
*
* \note Does not handle embedded nulls
*/
std::string FZ_PUBLIC_SYMBOL to_string(std::wstring const& in);
/// Returns identity, that way to_string can be called with native_string.
inline std::string FZ_PUBLIC_SYMBOL to_string(std::string const& in) { return in; }
/// Converts from arithmetic type to std::string
template<typename Arg>
inline typename std::enable_if<std::is_arithmetic<std::decay_t<Arg>>::value, std::string>::type to_string(Arg && arg)
{
return std::to_string(std::forward<Arg>(arg));
}
/// Returns length of 0-terminated character sequence. Works with both narrow and wide-characters.
template<typename Char>
size_t strlen(Char const* str) {
return std::char_traits<Char>::length(str);
}
/** \brief Converts from std::string in native encoding into std::string in UTF-8
*
* \return the converted string on success. On failure an empty string is returned.
*
* \note Does not handle embedded nulls
*/
std::string FZ_PUBLIC_SYMBOL to_utf8(std::string const& in);
/** \brief Converts from std::wstring in native encoding into std::string in UTF-8
*
* \return the converted string on success. On failure an empty string is returned.
*
* \note Does not handle embedded nulls
*/
std::string FZ_PUBLIC_SYMBOL to_utf8(std::wstring const& in);
/// Calls either fz::to_string or fz::to_wstring depending on the passed template argument
template<typename String, typename Arg>
inline auto toString(Arg&& arg) -> typename std::enable_if<std::is_same<String, std::string>::value, decltype(to_string(std::forward<Arg>(arg)))>::type
{
return to_string(std::forward<Arg>(arg));
}
template<typename String, typename Arg>
inline auto toString(Arg&& arg) -> typename std::enable_if<std::is_same<String, std::wstring>::value, decltype(to_wstring(std::forward<Arg>(arg)))>::type
{
return to_wstring(std::forward<Arg>(arg));
}
#if !defined(fzT) || defined(DOXYGEN)
#ifdef FZ_WINDOWS
/** \brief Macro for a string literal in system-native character type.\n Note: Macro definition changes depending on platform!
*
* Example: \c fzT("this string is wide on Windows and narrow elsewhere")
*/
#define fzT(x) L ## x
#else
/** \brief Macro for a string literal in system-native character type.\n Note: Macro definition changes depending on platform!
*
* Example: \c fzT("this string is wide on Windows and narrow elsewhere")
*/
#define fzT(x) x
#endif
#endif
/// Returns the function argument of the type matching the template argument. \sa fzS
template<typename Char>
Char const* choose_string(char const* c, wchar_t const* w);
template<> inline char const* choose_string(char const* c, wchar_t const*) { return c; }
template<> inline wchar_t const* choose_string(char const*, wchar_t const* w) { return w; }
#if !defined(fzS) || defined(DOXYGEN)
/** \brief Macro to get const pointer to a string of the corresponding type
*
* Useful when using string literals in templates where the type of string
* is a template argument:
* \code
* template<typename String>
* String append_foo(String const& s) {
* s += fzS(String::value_type, "foo");
* }
* \endcode
*/
#define fzS(Char, s) fz::choose_string<Char>(s, L ## s)
#endif
/// Returns \c in with all occurrences of \c find in the input string replaced with \c replacement
std::string FZ_PUBLIC_SYMBOL replaced_substrings(std::string const& in, std::string const& find, std::string const& replacement);
std::wstring FZ_PUBLIC_SYMBOL replaced_substrings(std::wstring const& in, std::wstring const& find, std::wstring const& replacement);
/// Modifies \c in, replacing all occurrences of \c find with \c replacement
void FZ_PUBLIC_SYMBOL replace_substrings(std::string& in, std::string const& find, std::string const& replacement);
void FZ_PUBLIC_SYMBOL replace_substrings(std::wstring& in, std::wstring const& find, std::wstring const& replacement);
/// Tokenizes string. Returns all non-empty substrings
template<typename String, typename Delim, typename Container = std::vector<String>>
Container strtok(String const& s, Delim const& delims)
{
Container ret;
typename String::size_type start{}, pos{};
do {
pos = s.find_first_of(delims, start);
// Not found, we're at ends;
if (pos == String::npos) {
if (start < s.size()) {
ret.emplace_back(s.substr(start));
}
}
else if (pos > start) {
// Non-empty substring
ret.emplace_back(s.substr(start, pos - start));
}
start = pos + 1;
} while (pos != String::npos);
return ret;
}
// Converts string to integral type T. If string is not convertible, T() is returned.
template<typename T, typename String>
T to_integral(String const& s, T const errorval = T())
{
T ret{};
auto it = s.cbegin();
if (it != s.cend() && (*it == '-' || *it == '+')) {
++it;
}
if (it == s.cend()) {
return errorval;
}
for (; it != s.cend(); ++it) {
auto const& c = *it;
if (c < '0' || c > '9') {
return errorval;
}
ret *= 10;
ret += c - '0';
}
if (!s.empty() && s.front() == '-') {
return ret *= static_cast<T>(-1);
}
else {
return ret;
}
}
/// \brief Returns true iff the string only has characters in the 7-bit ASCII range
template<typename String>
bool str_is_ascii(String const& s) {
for (auto const& c : s) {
if (static_cast<std::make_unsigned_t<typename String::value_type>>(c) > 127) {
return false;
}
}
return true;
}
/// \brief Return passed string with all leading and trailing whitespace removed
template<typename String>
String trimmed(String const& s, String const& chars = fzS(typename String::value_type, " \r\n\t"), bool fromLeft = true, bool fromRight = true) {
size_t const first = fromLeft ? s.find_first_not_of(chars) : 0;
if (first == String::npos) {
return String();
}
size_t const last = fromRight ? s.find_last_not_of(chars) : s.size();
if (last == String::npos) {
return String();
}
return s.substr(first, last - first + 1);
}
template<typename String>
String ltrimmed(String const& s, String const& chars = fzS(typename String::value_type, " \r\n\t"), bool fromLeft = true, bool fromRight = true) {
return trimmed(s, chars, true, false);
}
template<typename String>
String rtrimmed(String const& s, String const& chars = fzS(typename String::value_type, " \r\n\t"), bool fromLeft = true, bool fromRight = true) {
return trimmed(s, chars, false, true);
}
/// \brief Remove all leading and trailing whitespace from string
template<typename String>
void trim(String & s, String const& chars = fzS(typename String::value_type, " \r\n\t")) {
s = trimmed(s, chars);
}
template<typename String>
void ltrim(String & s, String const& chars = fzS(typename String::value_type, " \r\n\t")) {
s = trimmed(s, chars, true, false);
}
template<typename String>
void rtrim(String & s, String const& chars = fzS(typename String::value_type, " \r\n\t")) {
s = trimmed(s, chars, false, true);
}
}
#endif
|