From 669c12418aa43740ab13822731cb1715f5c2e45b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 3 Apr 2017 23:49:21 -0700 Subject: [PATCH] ircd: Add functors for iless/iequals/igreater. --- include/ircd/lexical.h | 165 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 159 insertions(+), 6 deletions(-) diff --git a/include/ircd/lexical.h b/include/ircd/lexical.h index b4a2eb2f6..f7d4e5e9c 100644 --- a/include/ircd/lexical.h +++ b/include/ircd/lexical.h @@ -151,8 +151,9 @@ string_view tokens_after(const string_view &str, const char *const &sep, const s // // Simple case insensitive comparison convenience utils -bool iequals(const string_view &a, const string_view &b); -bool iless(const string_view &a, const string_view &b); +struct iless; +struct igreater; +struct iequals; // Vintage size_t strlcpy(char *const &dest, const char *const &src, const size_t &bufmax); @@ -537,9 +538,56 @@ ircd::strlcat(char *const &dest, } #endif +struct ircd::iless +{ + using is_transparent = std::true_type; + + bool s; + + operator const bool &() const { return s; } + + bool operator()(const string_view &a, const string_view &b) const; + bool operator()(const string_view &a, const std::string &b) const; + bool operator()(const std::string &a, const string_view &b) const; + bool operator()(const std::string &a, const std::string &b) const; + + template + iless(A&& a, B&& b) + :s{operator()(std::forward(a), std::forward(b))} + {} + + iless() = default; +}; + inline bool -ircd::iless(const string_view &a, - const string_view &b) +ircd::iless::operator()(const std::string &a, + const std::string &b) +const +{ + return operator()(string_view{a}, string_view{b}); +} + +inline bool +ircd::iless::operator()(const string_view &a, + const std::string &b) +const +{ + return operator()(a, string_view{b}); +} + +inline bool +ircd::iless::operator()(const std::string &a, + const string_view &b) +const +{ + return operator()(string_view{a}, b); +} + +inline bool +ircd::iless::operator()(const string_view &a, + const string_view &b) +const { return std::lexicographical_compare(begin(a), end(a), begin(b), end(b), [] (const char &a, const char &b) @@ -548,9 +596,56 @@ ircd::iless(const string_view &a, }); } +struct ircd::iequals +{ + using is_transparent = std::true_type; + + bool s; + + operator const bool &() const { return s; } + + bool operator()(const string_view &a, const string_view &b) const; + bool operator()(const string_view &a, const std::string &b) const; + bool operator()(const std::string &a, const string_view &b) const; + bool operator()(const std::string &a, const std::string &b) const; + + template + iequals(A&& a, B&& b) + :s{operator()(std::forward(a), std::forward(b))} + {} + + iequals() = default; +}; + inline bool -ircd::iequals(const string_view &a, - const string_view &b) +ircd::iequals::operator()(const std::string &a, + const std::string &b) +const +{ + return operator()(string_view{a}, string_view{b}); +} + +inline bool +ircd::iequals::operator()(const string_view &a, + const std::string &b) +const +{ + return operator()(a, string_view{b}); +} + +inline bool +ircd::iequals::operator()(const std::string &a, + const string_view &b) +const +{ + return operator()(string_view{a}, b); +} + +inline bool +ircd::iequals::operator()(const string_view &a, + const string_view &b) +const { return std::equal(begin(a), end(a), begin(b), end(b), [] (const char &a, const char &b) @@ -559,6 +654,64 @@ ircd::iequals(const string_view &a, }); } +struct ircd::igreater +{ + using is_transparent = std::true_type; + + bool s; + + operator const bool &() const { return s; } + + bool operator()(const string_view &a, const string_view &b) const; + bool operator()(const string_view &a, const std::string &b) const; + bool operator()(const std::string &a, const string_view &b) const; + bool operator()(const std::string &a, const std::string &b) const; + + template + igreater(A&& a, B&& b) + :s{operator()(std::forward(a), std::forward(b))} + {} + + igreater() = default; +}; + +inline bool +ircd::igreater::operator()(const std::string &a, + const std::string &b) +const +{ + return operator()(string_view{a}, string_view{b}); +} + +inline bool +ircd::igreater::operator()(const string_view &a, + const std::string &b) +const +{ + return operator()(a, string_view{b}); +} + +inline bool +ircd::igreater::operator()(const std::string &a, + const string_view &b) +const +{ + return operator()(string_view{a}, b); +} + +inline bool +ircd::igreater::operator()(const string_view &a, + const string_view &b) +const +{ + return std::lexicographical_compare(begin(a), end(a), begin(b), end(b), [] + (const char &a, const char &b) + { + return tolower(a) > tolower(b); + }); +} + template ircd::string_view ircd::lex_cast(const T &t)