diff --git a/include/boost/container/string.hpp b/include/boost/container/string.hpp index 1e47aba..db65bf3 100644 --- a/include/boost/container/string.hpp +++ b/include/boost/container/string.hpp @@ -58,6 +58,7 @@ #include #include #include +#include namespace boost { namespace container { @@ -2059,6 +2060,12 @@ class basic_string const CharT* data() const BOOST_NOEXCEPT_OR_NOTHROW { return container_detail::to_raw_pointer(this->priv_addr()); } + //! Returns: a string_view to the characters in the string. + //! + //! Complexity: constant time. + operator boost::basic_string_view() const BOOST_NOEXCEPT_OR_NOTHROW + { return boost::basic_string_view(data(), size()); } + ////////////////////////////////////////////// // // string operations @@ -2463,6 +2470,16 @@ class basic_string } + //! Throws: Nothing + //! + //! Returns: compare(basic_string(sv)). + int compare(boost::basic_string_view sv) const + { + const pointer addr = this->priv_addr(); + return s_compare(addr, addr + this->priv_size(), + sv.data(), sv.data() + sv.size()); + } + //! Requires: pos1 > size() and s points to an array of at least n2 elements of CharT. //! //! Throws: out_of_range if pos1 > size() @@ -2776,6 +2793,25 @@ operator==(const basic_string& x, const CharT* s) return x.size() == n && Traits::compare(x.data(), s, n) == 0; } +template +inline bool +operator==( boost::basic_string_view x, + const basic_string& y) +{ + return x.size() == y.size() && + Traits::compare(x.data(), y.data(), x.size()) == 0; +} + +template +inline bool +operator==( const basic_string& x, + boost::basic_string_view y) +{ + return x.size() == y.size() && + Traits::compare(x.data(), y.data(), x.size()) == 0; +} + + template inline bool operator!=(const basic_string& x, @@ -2793,6 +2829,19 @@ operator!=(const basic_string& x, const CharT* s) { return !(x == s); } +template +inline bool +operator!=( boost::basic_string_view x, + const basic_string& y) + { return !(x == y); } + +template +inline bool +operator!=( const basic_string& x, + boost::basic_string_view y) + { return !(x == y); } + + // Operator< (and also >, <=, and >=). template @@ -2825,6 +2874,18 @@ operator<(const basic_string& x, // ::s_compare(x.begin(), x.end(), s, s + n) < 0; } +template +inline bool +operator<( boost::basic_string_view x, + const basic_string& y) + { return x.compare(y) < 0; } + +template +inline bool +operator<( const basic_string& x, + boost::basic_string_view y) + { return y.compare(x) > 0; } + template inline bool operator>(const basic_string& x, @@ -2845,6 +2906,19 @@ operator>(const basic_string& x, const CharT* s) return s < x; } +template +inline bool +operator>( boost::basic_string_view x, + const basic_string& y) + { return y < x; } + +template +inline bool +operator>( const basic_string& x, + boost::basic_string_view y) + { return y < x; } + + template inline bool operator<=(const basic_string& x, @@ -2863,6 +2937,19 @@ inline bool operator<=(const basic_string& x, const CharT* s) { return !(s < x); } + +template +inline bool +operator<=( boost::basic_string_view x, + const basic_string& y) + { return !(y < x); } + +template +inline bool +operator<=( const basic_string& x, + boost::basic_string_view y) + { return !(y < x); } + template inline bool operator>=(const basic_string& x, @@ -2879,6 +2966,19 @@ inline bool operator>=(const basic_string& x, const CharT* s) { return !(x < s); } +template +inline bool +operator>=( boost::basic_string_view x, + const basic_string& y) + { return !(x < y); } + +template +inline bool +operator>=( const basic_string& x, + boost::basic_string_view y) + { return !(x < y); } + + // Swap. template inline void swap(basic_string& x, basic_string& y)