0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-20 08:51:18 +01:00

ircd::js: Improve string comparison suite; add transparent less.

This commit is contained in:
Jason Volk 2016-10-26 01:29:44 -07:00
parent 4c62795aeb
commit 04a919820a

View file

@ -39,6 +39,18 @@ struct string
explicit operator std::string() const; explicit operator std::string() const;
operator JS::Value() const; operator JS::Value() const;
friend int cmp(const string &, const string &);
friend int cmp(const string &, const char *const &);
friend int cmp(const char *const &, const string &);
friend int cmp(const string &, const std::string &);
friend int cmp(const std::string &, const string &);
struct less
{
using is_transparent = std::true_type;
template<class A, class B> bool operator()(const A &, const B &) const;
};
string(const char *const &, const size_t &len); string(const char *const &, const size_t &len);
explicit string(const std::string &); explicit string(const std::string &);
string(const char *const &); string(const char *const &);
@ -49,14 +61,6 @@ struct string
string(string &&) noexcept; string(string &&) noexcept;
string(const string &) = delete; string(const string &) = delete;
friend int cmp(const string &, const string &);
friend bool operator==(const string &, const string &);
friend bool operator==(const string &, const char *const &);
friend bool operator==(const char *const &, const string &);
friend bool operator==(const string &, const std::string &);
friend bool operator==(const std::string &, const string &);
friend std::ostream &operator<<(std::ostream &, const string &); friend std::ostream &operator<<(std::ostream &, const string &);
}; };
@ -161,16 +165,53 @@ std::ostream &operator<<(std::ostream &os, const string &s)
return os; return os;
} }
inline bool template<class A,
operator==(const string &a, const std::string &b) class B>
constexpr bool
string_comparison()
{ {
return operator==(a, b.c_str()); return std::is_base_of<string, A>() || std::is_base_of<string, B>();
} }
inline bool template<class A,
operator==(const std::string &a, const string &b) class B>
bool
string::less::operator()(const A &a, const B &b)
const
{ {
return operator==(a.c_str(), b); return cmp(a, b) < 0;
}
template<class A,
class B>
typename std::enable_if<string_comparison<A, B>(), bool>::type
operator>(const A &a, const B &b)
{
return cmp(a, b) > 0;
}
template<class A,
class B>
typename std::enable_if<string_comparison<A, B>(), bool>::type
operator<(const A &a, const B &b)
{
return cmp(a, b) < 0;
}
template<class A,
class B>
typename std::enable_if<string_comparison<A, B>(), bool>::type
operator>=(const A &a, const B &b)
{
return cmp(a, b) >= 0;
}
template<class A,
class B>
typename std::enable_if<string_comparison<A, B>(), bool>::type
operator<=(const A &a, const B &b)
{
return cmp(a, b) <= 0;
} }
inline bool inline bool
@ -193,12 +234,46 @@ operator==(const char *const &a, const string &b)
return ret; return ret;
} }
inline bool template<class A,
operator==(const string &a, const string &b) class B>
typename std::enable_if<string_comparison<A, B>(), bool>::type
operator==(const A &a, const B &b)
{ {
return cmp(a, b) == 0; return cmp(a, b) == 0;
} }
template<class A,
class B>
typename std::enable_if<string_comparison<A, B>(), bool>::type
operator!=(const A &a, const B &b)
{
return !(operator==(a, b));
}
inline int
cmp(const string &a, const std::string &b)
{
return cmp(a, b.c_str());
}
inline int
cmp(const std::string &a, const string &b)
{
return cmp(a.c_str(), b);
}
inline int
cmp(const string &a, const char *const &b)
{
return cmp(a, string(b));
}
inline int
cmp(const char *const &a, const string &b)
{
return cmp(string(a), b);
}
inline int inline int
cmp(const string &a, const string &b) cmp(const string &a, const string &b)
{ {