1 #ifndef OOKII_STRING_HELPER_H_
4 #define OOKII_STRING_HELPER_H_
13 #include <string_view>
34 : _case_sensitive{case_sensitive},
45 template<
typename Range1,
typename Range2>
53 return std::ranges::lexicographical_compare(left, right, std::less<>{});
57 return std::ranges::lexicographical_compare(left, right, [
this](
auto left,
auto right)
59 return std::toupper(left, _locale) < std::toupper(right, _locale);
76 template<
typename CharType,
typename Traits>
77 bool string_equal_case_insensitive(std::basic_string_view<CharType, Traits> string1, std::basic_string_view<CharType, Traits> string2,
const std::locale &locale = {})
79 return std::ranges::equal(string1, string2, [&locale](
auto left,
auto right)
81 return std::toupper(left, locale) == std::toupper(right, locale);
97 template<
typename CharType,
size_t Length>
98 constexpr
const std::array<CharType, Length>
literal_cast(
const char (&value)[Length])
100 std::array<CharType, Length> result{};
101 std::ranges::copy(value, result.begin());
115 template<
typename CharType,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
122 static std::basic_string<CharType, Traits, Alloc>
from_bytes(std::string_view value,
const std::locale &loc = {})
124 std::basic_string<CharType, Traits, Alloc> result;
125 auto &facet = std::use_facet<std::ctype<CharType>>(loc);
126 result.reserve(value.length());
127 std::ranges::transform(value, std::back_inserter(result), [&facet](
auto c)
129 return facet.widen(c);
139 template<
typename Traits,
typename Alloc>
145 static std::basic_string<char, Traits, Alloc>
from_bytes(std::string_view value,
const std::locale & = {})
147 return std::basic_string<char, Traits, Alloc>{value};
161 template<
typename T,
typename CharType,
typename Traits = std::
char_traits<CharType>,
typename Alloc = std::allocator<CharType>>
168 static std::optional<T>
from_string(std::basic_string_view<CharType, Traits> value,
const std::locale &loc = {})
170 std::basic_istringstream<CharType> stream{std::basic_string<CharType, Traits, Alloc>{value}};
174 stream.unsetf(std::ios::dec);
175 stream.unsetf(std::ios::oct);
176 stream.unsetf(std::ios::hex);
179 if (!stream || !stream.eof())
194 template<
typename CharType,
typename Traits,
typename Alloc>
195 struct lexical_convert<std::basic_string<CharType, Traits, Alloc>, CharType, Traits, Alloc>
200 static std::optional<std::basic_string<CharType, Traits, Alloc>>
from_string(std::basic_string_view<CharType, Traits> value,
const std::locale &)
202 return {std::basic_string<CharType, Traits, Alloc>{value}};
213 template<
typename CharType,
typename Traits = std::
char_traits<CharType>>
219 using iterator_concept = std::forward_iterator_tag;
220 using value_type = std::basic_string_view<CharType, Traits>;
221 using difference_type = std::ptrdiff_t;
222 using pointer = value_type*;
223 using reference = value_type&;
225 iterator() noexcept =
default;
227 iterator(value_type value, CharType separator) noexcept
229 _separator{separator}
234 iterator &operator++() noexcept
240 iterator operator++(
int) noexcept
242 iterator temp = *
this;
247 value_type operator*()
const noexcept
252 bool operator==(
const iterator &other)
const noexcept
254 return _value == other._value && _remaining == other._remaining;
258 void next_value() noexcept
260 if (_remaining.length() != 0)
262 auto index = _remaining.find_first_of(_separator);
263 _value = _remaining.substr(0, index);
264 if (index == std::string_view::npos)
270 _remaining = _remaining.substr(index + 1);
280 value_type _remaining;
288 tokenize(
typename iterator::value_type value, CharType separator) noexcept
290 _separator{separator}
297 return iterator{_value, _separator};
302 iterator
end() const noexcept
308 typename iterator::value_type _value;
A pseudo-range for string tokenization.
Definition: string_helper.h:215
iterator end() const noexcept
Gets an iterator that will compare equal once the iterator returned by begin() no longer has any toke...
Definition: string_helper.h:302
tokenize(typename iterator::value_type value, CharType separator) noexcept
Initializes a new instance of the tokenize class.
Definition: string_helper.h:288
iterator begin() const noexcept
Gets a forward iterator to the first token.
Definition: string_helper.h:295
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
constexpr const std::array< CharType, Length > literal_cast(const char(&value)[Length])
Converts a simple ASCII string literal to the specified character type at compile time.
Definition: string_helper.h:98
bool string_equal_case_insensitive(std::basic_string_view< CharType, Traits > string1, std::basic_string_view< CharType, Traits > string2, const std::locale &locale={})
Compares two strings, ignoring their case.
Definition: string_helper.h:77
static std::optional< std::basic_string< CharType, Traits, Alloc > > from_string(std::basic_string_view< CharType, Traits > value, const std::locale &)
Convert a string to the specified type.
Definition: string_helper.h:200
Template class used to convert strings to strongly typed argument values.
Definition: string_helper.h:163
static std::optional< T > from_string(std::basic_string_view< CharType, Traits > value, const std::locale &loc={})
Convert a string to the specified type.
Definition: string_helper.h:168
static std::basic_string< char, Traits, Alloc > from_bytes(std::string_view value, const std::locale &={})
Converts a string to the specified character type.
Definition: string_helper.h:145
Performs a simple conversion of a narrow character string to a specified character type.
Definition: string_helper.h:117
static std::basic_string< CharType, Traits, Alloc > from_bytes(std::string_view value, const std::locale &loc={})
Converts a string to the specified character type.
Definition: string_helper.h:122
A version of the std::less predicate for strings that supports case insensitive comparison.
Definition: string_helper.h:23
int is_transparent
Indicates that this function accepts any type and uses perfect forwarding.
Definition: string_helper.h:26
string_less(bool case_sensitive=true, std::locale loc={})
Initializes a new instance of the StringLess class.
Definition: string_helper.h:33
bool operator()(Range1 &&left, Range2 &&right) const
Compares two strings.
Definition: string_helper.h:46