diff --git a/tstl/include/string.hpp b/tstl/include/string.hpp index 81ab27db..2df00b4d 100644 --- a/tstl/include/string.hpp +++ b/tstl/include/string.hpp @@ -476,6 +476,23 @@ public: return const_iterator(data_ptr() + size()); } + /*! + * \brief Lexicographically compare strings + */ + int compare(basic_string& rhs){ + return base_compare(rhs); + } + + /*! + * \brief Lexicographically compare with string_view rhs + */ + template + int compare(const T& rhs){ + basic_string_view sv = rhs; + + return base_compare(sv); + } + private: void ensure_capacity(size_t new_capacity){ if(new_capacity > 0 && (capacity() < new_capacity)){ @@ -499,6 +516,29 @@ private: } } } + + template + int base_compare(const T& rhs){ + for (size_t i = 0; i < rhs.size() && i < size(); ++i) { + if ((*this)[i] < rhs[i]) { + return -1; + } + + if ((*this)[i] > rhs[i]) { + return 1; + } + } + + if (size() == rhs.size()) { + return 0; + } + + if (size() < rhs.size()) { + return -1; + } + + return 1; + } }; template @@ -532,17 +572,7 @@ basic_string operator+(const basic_string& lhs, const C* rhs){ */ template bool operator==(const basic_string& x, const basic_string& y){ - if (x.size() != y.size()) { - return false; - } - - for (size_t i = 0; i < x.size(); ++i) { - if (x[i] != y[i]) { - return false; - } - } - - return true; + return x.compare(y) == 0; } /*! @@ -550,17 +580,7 @@ bool operator==(const basic_string& x, const basic_string& y){ */ template bool operator==(const basic_string& x, const CharT* y){ - if (x.size() != str_len(y)) { - return false; - } - - for (size_t i = 0; i < x.size(); ++i) { - if (x[i] != y[i]) { - return false; - } - } - - return true; + return x.compare(y) == 0; } /*! @@ -568,17 +588,7 @@ bool operator==(const basic_string& x, const CharT* y){ */ template bool operator==(const CharT* x, const basic_string& y){ - if (str_len(x) != y.size()) { - return false; - } - - for (size_t i = 0; i < x.size(); ++i) { - if (x[i] != y[i]) { - return false; - } - } - - return true; + return y.compare(x) == 0; } /*! @@ -586,7 +596,7 @@ bool operator==(const CharT* x, const basic_string& y){ */ template bool operator!=(const basic_string& x, const basic_string& y) { - return !(x == y); + return x.compare(y) != 0; } /*! @@ -594,7 +604,7 @@ bool operator!=(const basic_string& x, const basic_string& y) { */ template bool operator!=(const basic_string& x, const CharT* y) { - return !(x == y); + return x.compare(y) != 0; } /*! @@ -602,7 +612,67 @@ bool operator!=(const basic_string& x, const CharT* y) { */ template bool operator!=(const CharT* x, const basic_string& y) { - return !(x == y); + return y.compare(x) != 0; +} + +template +bool operator<(const basic_string& x, const basic_string& y){ + return x.compare(y) < 0; +} + +template +bool operator<(const basic_string& x, const CharT* y){ + return x.compare(y) < 0; +} + +template +bool operator<(const CharT* x, const basic_string& y){ + return y.compare(x) > 0; +} + +template +bool operator>(const basic_string& x, const basic_string& y){ + return x.compare(y) > 0; +} + +template +bool operator>(const basic_string& x, const CharT* y){ + return x.compare(y) > 0; +} + +template +bool operator>(const CharT* x, const basic_string& y){ + return y.compare(x) < 0; +} + +template +bool operator<=(const basic_string& x, const basic_string& y){ + return x.compare(y) <= 0; +} + +template +bool operator<=(const basic_string& x, const CharT* y){ + return x.compare(y) <= 0; +} + +template +bool operator<=(const CharT* x, const basic_string& y){ + return y.compare(x) >= 0; +} + +template +bool operator>=(const basic_string& x, const basic_string& y){ + return x.compare(y) >= 0; +} + +template +bool operator>=(const basic_string& x, const CharT* y){ + return x.compare(y) >= 0; +} + +template +bool operator>=(const CharT* x, const basic_string& y){ + return y.compare(x) <= 0; } typedef basic_string string;