1 #ifndef LIBFILEZILLA_STRING_HEADER 2 #define LIBFILEZILLA_STRING_HEADER 33 #if defined(FZ_UNIX) || defined(FZ_MAC) 41 native_string FZ_PUBLIC_SYMBOL
to_native(std::string
const& in);
47 native_string FZ_PUBLIC_SYMBOL
to_native(std::wstring
const& in);
55 int FZ_PUBLIC_SYMBOL
stricmp(std::string
const& a, std::string
const& b);
56 int FZ_PUBLIC_SYMBOL
stricmp(std::wstring
const& a, std::wstring
const& b);
75 template<
typename Char>
77 if (c >=
'A' && c <=
'Z') {
78 return c + (
'a' -
'A');
84 std::wstring::value_type FZ_PUBLIC_SYMBOL
tolower_ascii(std::wstring::value_type c);
87 template<
typename Char>
89 if (c >=
'a' && c <=
'z') {
90 return c + (
'A' -
'a');
96 std::wstring::value_type FZ_PUBLIC_SYMBOL
toupper_ascii(std::wstring::value_type c);
101 template<
typename String>
105 for (
auto& c : ret) {
111 template<
typename String>
112 String str_toupper_ascii(String
const& s)
115 for (
auto& c : ret) {
129 bool operator()(T
const& lhs, T
const& rhs)
const {
140 std::wstring FZ_PUBLIC_SYMBOL
to_wstring(std::string
const& in);
143 inline std::wstring FZ_PUBLIC_SYMBOL
to_wstring(std::wstring
const& in) {
return in; }
146 template<
typename Arg>
147 inline typename std::enable_if<std::is_arithmetic<std::decay_t<Arg>>::value, std::wstring>::type
to_wstring(Arg && arg)
166 std::string FZ_PUBLIC_SYMBOL
to_string(std::wstring
const& in);
169 inline std::string FZ_PUBLIC_SYMBOL
to_string(std::string
const& in) {
return in; }
172 template<
typename Arg>
173 inline typename std::enable_if<std::is_arithmetic<std::decay_t<Arg>>::value, std::string>::type
to_string(Arg && arg)
180 template<
typename Char>
182 return std::char_traits<Char>::length(str);
192 std::string FZ_PUBLIC_SYMBOL
to_utf8(std::string
const& in);
200 std::string FZ_PUBLIC_SYMBOL
to_utf8(std::wstring
const& in);
203 template<
typename String,
typename Arg>
204 inline auto toString(Arg&& arg) ->
typename std::enable_if<std::is_same<String, std::string>::value, decltype(
to_string(std::forward<Arg>(arg)))>::type
206 return to_string(std::forward<Arg>(arg));
209 template<
typename String,
typename Arg>
210 inline auto toString(Arg&& arg) ->
typename std::enable_if<std::is_same<String, std::wstring>::value, decltype(
to_wstring(std::forward<Arg>(arg)))>::type
215 #if !defined(fzT) || defined(DOXYGEN) 221 #define fzT(x) L ## x 232 template<
typename Char>
235 template<>
inline char const*
choose_string(
char const* c,
wchar_t const*) {
return c; }
236 template<>
inline wchar_t const*
choose_string(
char const*,
wchar_t const* w) {
return w; }
238 #if !defined(fzS) || defined(DOXYGEN) 250 #define fzS(Char, s) fz::choose_string<Char>(s, L ## s) 254 std::string FZ_PUBLIC_SYMBOL
replaced_substrings(std::string
const& in, std::string
const& find, std::string
const& replacement);
255 std::wstring FZ_PUBLIC_SYMBOL
replaced_substrings(std::wstring
const& in, std::wstring
const& find, std::wstring
const& replacement);
258 void FZ_PUBLIC_SYMBOL
replace_substrings(std::string& in, std::string
const& find, std::string
const& replacement);
259 void FZ_PUBLIC_SYMBOL
replace_substrings(std::wstring& in, std::wstring
const& find, std::wstring
const& replacement);
262 template<
typename String,
typename Delim,
typename Container = std::vector<String>>
263 Container
strtok(String
const& s, Delim
const& delims)
267 typename String::size_type start{}, pos{};
269 pos = s.find_first_of(delims, start);
272 if (pos == String::npos) {
273 if (start < s.size()) {
274 ret.emplace_back(s.substr(start));
277 else if (pos > start) {
279 ret.emplace_back(s.substr(start, pos - start));
282 }
while (pos != String::npos);
288 template<
typename T,
typename String>
289 T to_integral(String
const& s, T
const errorval = T())
293 auto it = s.cbegin();
294 if (it != s.cend() && (*it ==
'-' || *it ==
'+')) {
298 if (it == s.cend()) {
302 for (; it != s.cend(); ++it) {
304 if (c < '0' || c >
'9') {
311 if (!s.empty() && s.front() ==
'-') {
312 return ret *=
static_cast<T
>(-1);
320 template<
typename String>
322 for (
auto const& c : s) {
323 if (
static_cast<std::make_unsigned_t<typename String::value_type>
>(c) > 127) {
332 template<
typename String>
333 String
trimmed(String
const& s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t"),
bool fromLeft =
true,
bool fromRight =
true) {
334 size_t const first = fromLeft ? s.find_first_not_of(chars) : 0;
335 if (first == String::npos) {
339 size_t const last = fromRight ? s.find_last_not_of(chars) : s.size();
340 if (last == String::npos) {
343 return s.substr(first, last - first + 1);
346 template<
typename String>
347 String ltrimmed(String
const& s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t"),
bool fromLeft =
true,
bool fromRight =
true) {
348 return trimmed(s, chars,
true,
false);
351 template<
typename String>
352 String rtrimmed(String
const& s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t"),
bool fromLeft =
true,
bool fromRight =
true) {
353 return trimmed(s, chars,
false,
true);
357 template<
typename String>
358 void trim(String & s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
362 template<
typename String>
363 void ltrim(String & s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
364 s =
trimmed(s, chars,
true,
false);
367 template<
typename String>
368 void rtrim(String & s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
369 s =
trimmed(s, chars,
false,
true);
std::wstring to_wstring_from_utf8(std::string const &in)
Converts from std::string in UTF-8 into std::wstring.
Comparator to be used for std::map for case-insentitive keys.
Definition: string.hpp:126
Char toupper_ascii(Char c)
Converts ASCII lowercase characters to uppercase as if C-locale is used.
Definition: string.hpp:88
std::enable_if< std::is_arithmetic< std::decay_t< Arg > >::value, std::wstring >::type to_wstring(Arg &&arg)
Converts from arithmetic type to std::wstring.
Definition: string.hpp:147
std::enable_if< std::is_arithmetic< std::decay_t< Arg > >::value, std::string >::type to_string(Arg &&arg)
Converts from arithmetic type to std::string.
Definition: string.hpp:173
#define fzS(Char, s)
Macro to get const pointer to a string of the corresponding type.
Definition: string.hpp:250
int stricmp(std::string const &a, std::string const &b)
Locale-sensitive stricmp.
Char tolower_ascii(Char c)
Converts ASCII uppercase characters to lowercase as if C-locale is used.
Definition: string.hpp:76
std::string replaced_substrings(std::string const &in, std::string const &find, std::string const &replacement)
Returns in with all occurrences of find in the input string replaced with replacement.
std::string to_utf8(std::string const &in)
Converts from std::string in native encoding into std::string in UTF-8.
void replace_substrings(std::string &in, std::string const &find, std::string const &replacement)
Modifies in, replacing all occurrences of find with replacement.
size_t strlen(Char const *str)
Returns length of 0-terminated character sequence. Works with both narrow and wide-characters.
Definition: string.hpp:181
bool str_is_ascii(String const &s)
Returns true iff the string only has characters in the 7-bit ASCII range.
Definition: string.hpp:321
Container strtok(String const &s, Delim const &delims)
Tokenizes string. Returns all non-empty substrings.
Definition: string.hpp:263
String trimmed(String const &s, String const &chars=fz::choose_string< typename String::value_type >(" \r\n\t", L" \r\n\t"), bool fromLeft=true, bool fromRight=true)
Return passed string with all leading and trailing whitespace removed.
Definition: string.hpp:333
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:31
std::string to_string(std::wstring const &in)
Converts from std::wstring into std::string in system encoding.
The namespace used by libfilezilla.
Definition: apply.hpp:16
Char const * choose_string(char const *c, wchar_t const *w)
Returns the function argument of the type matching the template argument.
Definition: string.hpp:235
void trim(String &s, String const &chars=fz::choose_string< typename String::value_type >(" \r\n\t", L" \r\n\t"))
Remove all leading and trailing whitespace from string.
Definition: string.hpp:358
String str_tolower_ascii(String const &s)
tr_tolower_ascii does for strings what tolower_ascii does for individual characters ...
Definition: string.hpp:102
Sets some global macros and further includes string.hpp.
native_string to_native(std::string const &in)
Converts std::string to native_string.
auto toString(Arg &&arg) -> typename std::enable_if< std::is_same< String, std::string >::value, decltype(to_string(std::forward< Arg >(arg)))>::type
Calls either fz::to_string or fz::to_wstring depending on the passed template argument.
Definition: string.hpp:204
std::wstring to_wstring(std::string const &in)
Converts from std::string in system encoding into std::wstring.