string: add less comparator with transform
This commit is contained in:
parent
e75b1f571c
commit
ce826e664f
49
string.hpp
49
string.hpp
@ -302,6 +302,55 @@ namespace cruft {
|
||||
}
|
||||
|
||||
|
||||
namespace cruft::string {
|
||||
template <char (*TransformV)(char) noexcept>
|
||||
struct less {
|
||||
bool operator() (char const *a, char const *b) const noexcept
|
||||
{
|
||||
for (; a && b; ++a, ++b) {
|
||||
auto const &a_val = TransformV (*a);
|
||||
auto const &b_val = TransformV (*b);
|
||||
|
||||
if (a_val < b_val)
|
||||
return true;
|
||||
if (a_val > b_val)
|
||||
return false;
|
||||
}
|
||||
|
||||
return a == nullptr && b != nullptr;
|
||||
}
|
||||
|
||||
bool operator() (std::string const &a, std::string const & b) const noexcept
|
||||
{
|
||||
return (*this) (std::string_view (a), std::string_view (b));
|
||||
}
|
||||
|
||||
bool operator() (std::string_view a, std::string_view b) const noexcept
|
||||
{
|
||||
auto a_cursor = a.begin ();
|
||||
auto b_cursor = b.begin ();
|
||||
|
||||
for ( ; a_cursor != a.end () && b_cursor != b.end (); ++a_cursor, ++b_cursor) {
|
||||
auto const &a_val = TransformV (*a_cursor);
|
||||
auto const &b_val = TransformV (*b_cursor);
|
||||
if (a_val < b_val)
|
||||
return true;
|
||||
if (a_val > b_val)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (a_cursor == a.end () && b_cursor != b.end ())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
using less_lower = less<ascii::try_lower>;
|
||||
}
|
||||
|
||||
|
||||
namespace cruft::string::equality {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// A case comparator that tests equality on a string after a
|
||||
|
Loading…
Reference in New Issue
Block a user