From c2883b0fed3d60a68b40269e929efd75337703f8 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Thu, 5 Apr 2018 11:14:43 +0200 Subject: [PATCH] Move operators out of std::string --- tstl/include/string.hpp | 131 ++++++++++++++++++++++++---------------- 1 file changed, 80 insertions(+), 51 deletions(-) diff --git a/tstl/include/string.hpp b/tstl/include/string.hpp index cd81553b..81ab27db 100644 --- a/tstl/include/string.hpp +++ b/tstl/include/string.hpp @@ -446,56 +446,6 @@ public: return {data_ptr(), size()}; } - //Operators - - /*! - * \brief Test if this string is equal to the given raw string - */ - bool operator==(const CharT* s) const { - if(size() != str_len(s)){ - return false; - } - - for(size_t i = 0; i < size(); ++i){ - if((*this)[i] != s[i]){ - return false; - } - } - - return true; - } - - /*! - * \brief Test if this string is not equal to the given raw string - */ - bool operator!=(const CharT* s) const { - return !(*this == s); - } - - /*! - * \brief Test if this string is equal to the given string - */ - bool operator==(const basic_string& rhs) const { - if(size() != rhs.size()){ - return false; - } - - for(size_t i = 0; i < size(); ++i){ - if((*this)[i] != rhs[i]){ - return false; - } - } - - return true; - } - - /*! - * \brief Test if this string is not equal to the given string - */ - bool operator!=(const basic_string& rhs) const { - return !(*this == rhs); - } - //Iterators /*! @@ -575,6 +525,86 @@ basic_string operator+(const basic_string& lhs, const C* rhs){ return std::move(result); } +//Operators + +/*! + * \brief Test if two std::basic_string are equal to each other + */ +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; +} + +/*! + * \brief Test if one std::basic_string and one raw string are equal to each other + */ +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; +} + +/*! + * \brief Test if one std::basic_string and one raw string are equal to each other + */ +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; +} + +/*! + * \brief Test if two std::basic_string are not equal to each other + */ +template +bool operator!=(const basic_string& x, const basic_string& y) { + return !(x == y); +} + +/*! + * \brief Test if one std::basic_string and one raw string are not equal to each other + */ +template +bool operator!=(const basic_string& x, const CharT* y) { + return !(x == y); +} + +/*! + * \brief Test if one std::basic_string and one raw string are not equal to each other + */ +template +bool operator!=(const CharT* x, const basic_string& y) { + return !(x == y); +} + typedef basic_string string; static_assert(sizeof(string) == 24, "The size of a string must always be 24 bytes"); @@ -840,7 +870,6 @@ inline uint64_t atoui(const std::string& s){ return value; } - } //end of namespace std #endif