1#ifndef LIBFILEZILLA_STRING_HEADER
2#define LIBFILEZILLA_STRING_HEADER
35typedef std::wstring_view native_string_view;
37#if defined(FZ_UNIX) || defined(FZ_MAC)
39typedef std::string_view native_string_view;
55template<
typename T,
typename std::enable_if_t<std::is_same_v<native_
string,
typename std::decay_t<T>>,
int> = 0>
86template<
typename Char>
88 if (
c >=
'A' &&
c <=
'Z') {
89 return c + (
'a' -
'A');
98template<
typename Char>
100 if (
c >=
'a' &&
c <=
'z') {
101 return c + (
'A' -
'a');
126 bool operator()(
T const&
lhs,
T const&
rhs)
const {
127 return std::lexicographical_compare(
lhs.cbegin(),
lhs.cend(),
rhs.cbegin(),
rhs.cend(),
128 [](
typename T::value_type
const&
a,
typename T::value_type
const&
b) {
129 return tolower_ascii(a) < tolower_ascii(b);
141 return std::equal(
a.cbegin(),
a.cend(),
b.cbegin(),
b.cend(),
142 [](
auto const&
a,
auto const&
b) {
143 return tolower_ascii(a) == tolower_ascii(b);
149 return std::equal(
a.cbegin(),
a.cend(),
b.cbegin(),
b.cend(),
150 [](
auto const&
a,
auto const&
b) {
151 return tolower_ascii(a) == tolower_ascii(b);
169 return std::wstring(std::forward<T>(
in));
173template<
typename Arg>
174inline typename std::enable_if<std::is_arithmetic_v<std::decay_t<Arg>>, std::wstring>::type
to_wstring(
Arg &&
arg)
176 return std::to_wstring(std::forward<Arg>(
arg));
203 return std::string(std::forward<T>(
in));
208template<
typename Arg>
209inline typename std::enable_if<std::is_arithmetic_v<std::decay_t<Arg>>, std::string>::type
to_string(
Arg &&
arg)
211 return std::to_string(std::forward<Arg>(
arg));
216template<
typename Char>
218 return std::char_traits<Char>::length(
str);
239template<
typename String,
typename Arg>
240inline auto toString(
Arg&&
arg) ->
typename std::enable_if<std::is_same_v<String, std::string>,
decltype(
to_string(std::forward<Arg>(
arg)))>::type
245template<
typename String,
typename Arg>
246inline auto toString(
Arg&&
arg) ->
typename std::enable_if<std::is_same_v<String, std::wstring>,
decltype(
to_wstring(std::forward<Arg>(
arg)))>::type
251#if !defined(fzT) || defined(DOXYGEN)
268template<
typename Char>
271template<>
constexpr inline char const*
choose_string(
char const*
c,
wchar_t const*) {
return c; }
272template<>
constexpr inline wchar_t const*
choose_string(
char const*,
wchar_t const*
w) {
return w; }
274#if !defined(fzS) || defined(DOXYGEN)
286#define fzS(Char, s) fz::choose_string<Char>(s, L ## s)
337template <
typename String,
typename Delims>
340 using view_type = std::basic_string_view<std::decay_t<decltype(std::declval<String>()[0])>>;
355 using value_type =
const view_type;
356 using pointer = value_type*;
357 using reference = value_type&;
358 using size_type = std::size_t;
359 using difference_type = std::ptrdiff_t;
365 using iterator_category = std::input_iterator_tag;
366 using difference_type = strtokenizer::difference_type;
367 using value_type = strtokenizer::value_type;
368 using pointer = strtokenizer::pointer;
369 using reference = strtokenizer::reference;
371 constexpr bool operator !=(
sentinel)
const
376 constexpr bool operator ==(
sentinel)
const
381 constexpr bool operator ==(
iterator const&
op)
const
383 return s_.size() ==
op.s_.size();
386 constexpr bool operator !=(
iterator const&
op)
const
388 return s_.size() !=
op.s_.size();
391 constexpr value_type operator*()
const
393 return s_.substr(0, pos_);
399 if (pos_ != s_.size()) {
403 s_.remove_prefix(pos_);
405 pos_ = s_.find_first_of(t_->delims_);
407 if (pos_ == view_type::npos) {
412 if (pos_ != 0 || !t_->ignore_empty_) {
425 , s_(view_type(t_->string_))
426 , pos_(view_type::npos)
436 using const_value_type = value_type;
437 using const_pointer = pointer;
438 using const_reference = reference;
446 constexpr sentinel end()
const
451 constexpr const_iterator cbegin()
const
456 constexpr sentinel cend()
const
473template <
typename String,
typename Delims>
509template<
typename T,
typename String>
512 if constexpr (std::is_same_v<T, bool>) {
515 else if constexpr (std::is_enum_v<T>) {
520 auto it =
s.cbegin();
521 if (
it !=
s.cend() && (*
it ==
'-' || *
it ==
'+')) {
525 if (
it ==
s.cend()) {
529 for (;
it !=
s.cend(); ++
it) {
531 if (
c <
'0' ||
c >
'9') {
538 if (!
s.empty() &&
s.front() ==
'-') {
539 ret *=
static_cast<T>(-1);
556template<
typename T,
typename StringType>
563template<
typename String>
565 for (
auto const&
c :
s) {
566 if (
static_cast<std::make_unsigned_t<typename String::value_type>
>(
c) > 127) {
575template<
typename String,
typename Chars>
578 if (
first == String::npos) {
584 if (
last == String::npos) {
597 return std::string(
s);
603 return std::wstring(
s);
606inline std::string
FZ_PUBLIC_SYMBOL ltrimmed(std::string_view
s, std::string_view
const&
chars =
" \r\n\t")
608 trim_impl(
s,
chars,
true,
false);
609 return std::string(
s);
614 trim_impl(
s,
chars,
true,
false);
615 return std::wstring(
s);
618inline std::string
FZ_PUBLIC_SYMBOL rtrimmed(std::string_view
s, std::string_view
const&
chars =
" \r\n\t")
620 trim_impl(
s,
chars,
false,
true);
621 return std::string(
s);
626 trim_impl(
s,
chars,
false,
true);
627 return std::wstring(
s);
632template<
typename String,
typename std::enable_if_t<std::is_same_v<
typename String::value_type,
char>,
int> = 0>
638template<
typename String,
typename std::enable_if_t<std::is_same_v<
typename String::value_type,
wchar_t>,
int> = 0>
644template<
typename String,
typename std::enable_if_t<std::is_same_v<
typename String::value_type,
char>,
int> = 0>
645inline void ltrim(
String&
s, std::string_view
const&
chars =
" \r\n\t")
647 trim_impl(
s,
chars,
true,
false);
650template<
typename String,
typename std::enable_if_t<std::is_same_v<
typename String::value_type,
wchar_t>,
int> = 0>
651inline void ltrim(
String&
s, std::wstring_view
const&
chars =
L" \r\n\t")
653 trim_impl(
s,
chars,
true,
false);
656template<
typename String,
typename std::enable_if_t<std::is_same_v<
typename String::value_type,
char>,
int> = 0>
657inline void rtrim(
String&
s, std::string_view
const&
chars =
" \r\n\t")
659 trim_impl(
s,
chars,
false,
true);
662template<
typename String,
typename std::enable_if_t<std::is_same_v<
typename String::value_type,
wchar_t>,
int> = 0>
663inline void rtrim(
String &
s, std::wstring_view
const&
chars =
L" \r\n\t")
665 trim_impl(
s,
chars,
false,
true);
672template<
bool insensitive_ascii = false,
typename String>
679 return std::equal(
beginning.begin(),
beginning.end(),
s.begin(), [](
typename String::value_type
const&
a,
typename String::value_type
const&
b) {
680 return tolower_ascii(a) == tolower_ascii(b);
692template<
bool insensitive_ascii = false,
typename String>
695 if (
ending.size() >
s.size()) {
700 return std::equal(
ending.rbegin(),
ending.rend(),
s.rbegin(), [](
typename String::value_type
const&
a,
typename String::value_type
const&
b) {
701 return tolower_ascii(a) == tolower_ascii(b);
705 return std::equal(
ending.rbegin(),
ending.rend(),
s.rbegin());
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:27
Small class to return filesystem errors.
Definition fsresult.hpp:26
Container-like class that can be used to iterate over tokens in a string.
Definition string.hpp:339
constexpr strtokenizer(String &&string, Delims &&delims, bool ignore_empty)
strtokenizer class constructor.
Definition string.hpp:349
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
size_t strlen(Char const *str)
Returns length of 0-terminated character sequence. Works with both narrow and wide-characters.
Definition string.hpp:217
Char toupper_ascii(Char c)
Converts ASCII lowercase characters to uppercase as if C-locale is used.
Definition string.hpp:99
bool utf16le_to_utf8_append(std::string &result, std::string_view data, uint32_t &state)
Just as utf16be_to_utf8_append but for little-endian UTF-16.
std::vector< std::string_view > strtok_view(std::string_view const &tokens, std::string_view const &delims, bool const ignore_empty=true)
Tokenizes string.
Char tolower_ascii(Char c)
Converts ASCII uppercase characters to lowercase as if C-locale is used.
Definition string.hpp:87
bool str_is_ascii(String const &s)
Returns true iff the string only has characters in the 7-bit ASCII range.
Definition string.hpp:564
strtokenizer(String &&string, Delims &&delims, bool ignore_empty) -> strtokenizer< String, Delims >
strtokenizer class construction-guide.
auto toString(Arg &&arg) -> typename std::enable_if< std::is_same_v< String, std::string >, 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:240
constexpr Char const * choose_string(char const *c, wchar_t const *w)
Returns the function argument of the type matching the template argument.
void trim(String &s, std::string_view const &chars=" \r\n\t", bool fromLeft=true, bool fromRight=true)
Remove all leading and trailing whitespace from string.
Definition string.hpp:633
bool is_valid_utf8(std::string_view s)
Verifies that the input data is valid UTF-8.
std::string trimmed(std::string_view s, std::string_view const &chars=" \r\n\t", bool fromLeft=true, bool fromRight=true)
Return passed string with all leading and trailing whitespace removed.
Definition string.hpp:594
std::wstring to_wstring_from_utf8(std::string_view const &in)
Converts from std::string in UTF-8 into std::wstring.
std::string normalize_hyphens(std::string_view const &in)
std::wstring native_string
A string in the system's native character type and encoding. Note: This typedef changes depending on...
Definition string.hpp:34
bool utf16be_to_utf8_append(std::string &result, std::string_view data, uint32_t &state)
Converts from UTF-16-BE and appends it to the passed string.
std::string to_utf8(std::string_view const &in)
Converts from std::string in native encoding into std::string in UTF-8.
bool ends_with(String const &s, String const &ending)
Tests whether the first string ends with the second string.
Definition string.hpp:693
bool equal_insensitive_ascii(std::string_view a, std::string_view b)
Locale-insensitive stricmp.
Definition string.hpp:139
std::vector< std::string > strtok(std::string_view const &tokens, std::string_view const &delims, bool const ignore_empty=true)
Tokenizes string.
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition event_handler.hpp:199
std::string to_string(std::wstring_view const &in)
Converts from std::wstring into std::string in system encoding.
std::wstring to_wstring(std::string_view const &in)
Converts from std::string in system encoding into std::wstring.
std::string replaced_substrings(std::string_view const &in, std::string_view const &find, std::string_view const &replacement)
Returns in with all occurrences of find in the input string replaced with replacement.
bool starts_with(String const &s, String const &beginning)
Tests whether the first string starts with the second string.
Definition string.hpp:673
bool replace_substrings(std::string &in, std::string_view const &find, std::string_view const &replacement)
Modifies in, replacing all occurrences of find with replacement.
int stricmp(std::string_view const &a, std::string_view const &b)
Locale-sensitive stricmp.
std::string str_tolower_ascii(std::string_view const &s)
tr_tolower_ascii does for strings what tolower_ascii does for individual characters
void unicode_codepoint_to_utf8_append(std::string &result, uint32_t codepoint)
Encodes a valid Unicode codepoint as UTF-8 and appends it to the passed string.
native_string to_native(std::string_view const &in)
Converts std::string to native_string.
T to_integral(std::string_view const &s, T const errorval=T())
Converts string to integral type T. If string is not convertible, errorval is returned.
Definition string.hpp:547
Comparator to be used for std::map for case-insensitive keys.
Definition string.hpp:124
Definition string.hpp:364
Definition string.hpp:361