1 #ifndef LIBFILEZILLA_STRING_HEADER 2 #define LIBFILEZILLA_STRING_HEADER 34 #if defined(FZ_UNIX) || defined(FZ_MAC) 42 native_string FZ_PUBLIC_SYMBOL
to_native(std::string
const& in);
48 native_string FZ_PUBLIC_SYMBOL
to_native(std::wstring
const& in);
56 int FZ_PUBLIC_SYMBOL
stricmp(std::string
const& a, std::string
const& b);
57 int FZ_PUBLIC_SYMBOL
stricmp(std::wstring
const& a, std::wstring
const& b);
76 template<
typename Char>
78 if (c >=
'A' && c <=
'Z') {
79 return c + (
'a' -
'A');
85 std::wstring::value_type FZ_PUBLIC_SYMBOL
tolower_ascii(std::wstring::value_type c);
88 template<
typename Char>
90 if (c >=
'a' && c <=
'z') {
91 return c + (
'A' -
'a');
97 std::wstring::value_type FZ_PUBLIC_SYMBOL
toupper_ascii(std::wstring::value_type c);
102 template<
typename String>
106 for (
auto& c : ret) {
112 template<
typename String>
113 String str_toupper_ascii(String
const& s)
116 for (
auto& c : ret) {
130 bool operator()(T
const& lhs, T
const& rhs)
const {
141 std::wstring FZ_PUBLIC_SYMBOL
to_wstring(std::string
const& in);
144 inline std::wstring FZ_PUBLIC_SYMBOL
to_wstring(std::wstring
const& in) {
return in; }
147 template<
typename Arg>
148 inline typename std::enable_if<std::is_arithmetic<std::decay_t<Arg>>::value, std::wstring>::type
to_wstring(Arg && arg)
167 std::string FZ_PUBLIC_SYMBOL
to_string(std::wstring
const& in);
170 inline std::string FZ_PUBLIC_SYMBOL
to_string(std::string
const& in) {
return in; }
173 template<
typename Arg>
174 inline typename std::enable_if<std::is_arithmetic<std::decay_t<Arg>>::value, std::string>::type
to_string(Arg && arg)
181 template<
typename Char>
183 return std::char_traits<Char>::length(str);
193 std::string FZ_PUBLIC_SYMBOL
to_utf8(std::string
const& in);
201 std::string FZ_PUBLIC_SYMBOL
to_utf8(std::wstring
const& in);
204 template<
typename String,
typename Arg>
205 inline auto toString(Arg&& arg) ->
typename std::enable_if<std::is_same<String, std::string>::value, decltype(
to_string(std::forward<Arg>(arg)))>::type
207 return to_string(std::forward<Arg>(arg));
210 template<
typename String,
typename Arg>
211 inline auto toString(Arg&& arg) ->
typename std::enable_if<std::is_same<String, std::wstring>::value, decltype(
to_wstring(std::forward<Arg>(arg)))>::type
216 #if !defined(fzT) || defined(DOXYGEN) 222 #define fzT(x) L ## x 233 template<
typename Char>
236 template<>
inline char const*
choose_string(
char const* c,
wchar_t const*) {
return c; }
237 template<>
inline wchar_t const*
choose_string(
char const*,
wchar_t const* w) {
return w; }
239 #if !defined(fzS) || defined(DOXYGEN) 251 #define fzS(Char, s) fz::choose_string<Char>(s, L ## s) 255 std::string FZ_PUBLIC_SYMBOL
replaced_substrings(std::string
const& in, std::string
const& find, std::string
const& replacement);
256 std::wstring FZ_PUBLIC_SYMBOL
replaced_substrings(std::wstring
const& in, std::wstring
const& find, std::wstring
const& replacement);
259 void FZ_PUBLIC_SYMBOL
replace_substrings(std::string& in, std::string
const& find, std::string
const& replacement);
260 void FZ_PUBLIC_SYMBOL
replace_substrings(std::wstring& in, std::wstring
const& find, std::wstring
const& replacement);
263 template<
typename String,
typename Delim,
typename Container = std::vector<String>>
264 Container
strtok(String
const& s, Delim
const& delims)
268 typename String::size_type start{}, pos{};
270 pos = s.find_first_of(delims, start);
273 if (pos == String::npos) {
274 if (start < s.size()) {
275 ret.emplace_back(s.substr(start));
278 else if (pos > start) {
280 ret.emplace_back(s.substr(start, pos - start));
283 }
while (pos != String::npos);
289 template<
typename T,
typename String>
290 T to_integral(String
const& s, T
const errorval = T())
294 auto it = s.cbegin();
295 if (it != s.cend() && (*it ==
'-' || *it ==
'+')) {
299 if (it == s.cend()) {
303 for (; it != s.cend(); ++it) {
305 if (c < '0' || c >
'9') {
312 if (!s.empty() && s.front() ==
'-') {
313 return ret *=
static_cast<T
>(-1);
321 template<
typename String>
323 for (
auto const& c : s) {
324 if (
static_cast<std::make_unsigned_t<typename String::value_type>
>(c) > 127) {
333 template<
typename String>
334 String
trimmed(String
const& s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t"),
bool fromLeft =
true,
bool fromRight =
true) {
335 size_t const first = fromLeft ? s.find_first_not_of(chars) : 0;
336 if (first == String::npos) {
340 size_t const last = fromRight ? s.find_last_not_of(chars) : s.size();
341 if (last == String::npos) {
344 return s.substr(first, last - first + 1);
347 template<
typename String>
348 String ltrimmed(String
const& s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
349 return trimmed(s, chars,
true,
false);
352 template<
typename String>
353 String rtrimmed(String
const& s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
354 return trimmed(s, chars,
false,
true);
358 template<
typename String>
359 void trim(String & s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
363 template<
typename String>
364 void ltrim(String & s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
365 s =
trimmed(s, chars,
true,
false);
368 template<
typename String>
369 void rtrim(String & s, String
const& chars =
fzS(
typename String::value_type,
" \r\n\t")) {
370 s =
trimmed(s, chars,
false,
true);
374 template<
typename String>
375 bool starts_with(String
const& s, String
const& beginning)
377 if (beginning.size() > s.size()) {
380 return std::equal(beginning.begin(), beginning.end(), s.begin());
384 template<
typename String>
385 bool ends_with(String
const& s, String
const& ending)
387 if (ending.size() > s.size()) {
390 return std::equal(ending.rbegin(), ending.rend(), s.rbegin());
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:127
Char toupper_ascii(Char c)
Converts ASCII lowercase characters to uppercase as if C-locale is used.
Definition: string.hpp:89
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:148
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:174
#define fzS(Char, s)
Macro to get const pointer to a string of the corresponding type.
Definition: string.hpp:251
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:77
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:182
bool str_is_ascii(String const &s)
Returns true iff the string only has characters in the 7-bit ASCII range.
Definition: string.hpp:322
Container strtok(String const &s, Delim const &delims)
Tokenizes string. Returns all non-empty substrings.
Definition: string.hpp:264
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition: string.hpp:32
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:236
void trim(String &s, String const &chars=fz::choose_string< typename String::value_type >(" \\, L" \\\"))
Remove all leading and trailing whitespace from string.
Definition: string.hpp:359
String str_tolower_ascii(String const &s)
tr_tolower_ascii does for strings what tolower_ascii does for individual characters ...
Definition: string.hpp:103
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:205
String trimmed(String const &s, String const &chars=fz::choose_string< typename String::value_type >(" \\, L" \\\"), bool fromLeft=true, bool fromRight=true)
Return passed string with all leading and trailing whitespace removed.
Definition: string.hpp:334
std::wstring to_wstring(std::string const &in)
Converts from std::string in system encoding into std::wstring.