string: add less comparator with transform

This commit is contained in:
Danny Robson 2021-11-22 11:15:53 +10:00
parent e75b1f571c
commit ce826e664f

View File

@ -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