From 8ab8e36dcfaa88837e3cf0bccaf01c458d3c1fb2 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 17 Feb 2016 11:32:16 -0800 Subject: [PATCH 01/12] Remove the 'basic_string_ref template; use 'basic_string_view' instead. Keep the string_ref, etc typedefs around, though --- include/boost/utility/string_ref.hpp | 516 +--------------------- include/boost/utility/string_ref_fwd.hpp | 12 +- include/boost/utility/string_view_fwd.hpp | 2 +- test/Jamfile.v2 | 3 - test/string_ref_test1.cpp | 109 ----- test/string_ref_test2.cpp | 318 ------------- test/string_ref_test_io.cpp | 179 -------- 7 files changed, 10 insertions(+), 1129 deletions(-) delete mode 100644 test/string_ref_test1.cpp delete mode 100644 test/string_ref_test2.cpp delete mode 100644 test/string_ref_test_io.cpp diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 8707157..df5dcbe 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -18,519 +18,9 @@ #include #include #include -#include +#include -#include -#include -#include -#include -#include -#include - -namespace boost { - - namespace detail { - // A helper functor because sometimes we don't have lambdas - template - class string_ref_traits_eq { - public: - string_ref_traits_eq ( charT ch ) : ch_(ch) {} - bool operator () ( charT val ) const { return traits::eq ( ch_, val ); } - charT ch_; - }; - } - - template - class basic_string_ref { - public: - // types - typedef charT value_type; - typedef const charT* pointer; - typedef const charT& reference; - typedef const charT& const_reference; - typedef pointer const_iterator; // impl-defined - typedef const_iterator iterator; - typedef std::reverse_iterator const_reverse_iterator; - typedef const_reverse_iterator reverse_iterator; - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1); - - // construct/copy - BOOST_CONSTEXPR basic_string_ref () - : ptr_(NULL), len_(0) {} - - BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) - : ptr_(rhs.ptr_), len_(rhs.len_) {} - - basic_string_ref& operator=(const basic_string_ref &rhs) { - ptr_ = rhs.ptr_; - len_ = rhs.len_; - return *this; - } - - basic_string_ref(const charT* str) - : ptr_(str), len_(traits::length(str)) {} - - template - basic_string_ref(const std::basic_string& str) - : ptr_(str.data()), len_(str.length()) {} - - BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) - : ptr_(str), len_(len) {} - -#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS - template - explicit operator std::basic_string() const { - return std::basic_string ( begin(), end()); - } -#endif - - std::basic_string to_string () const { - return std::basic_string ( begin(), end()); - } - - // iterators - BOOST_CONSTEXPR const_iterator begin() const { return ptr_; } - BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; } - BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; } - BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; } - const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); } - const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); } - const_reverse_iterator rend() const { return const_reverse_iterator (begin()); } - const_reverse_iterator crend() const { return const_reverse_iterator (begin()); } - - // capacity - BOOST_CONSTEXPR size_type size() const { return len_; } - BOOST_CONSTEXPR size_type length() const { return len_; } - BOOST_CONSTEXPR size_type max_size() const { return len_; } - BOOST_CONSTEXPR bool empty() const { return len_ == 0; } - - // element access - BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; } - - const charT& at(size_t pos) const { - if ( pos >= len_ ) - BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) ); - return ptr_[pos]; - } - - BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; } - BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; } - BOOST_CONSTEXPR const charT* data() const { return ptr_; } - - // modifiers - void clear() { len_ = 0; } - void remove_prefix(size_type n) { - if ( n > len_ ) - n = len_; - ptr_ += n; - len_ -= n; - } - - void remove_suffix(size_type n) { - if ( n > len_ ) - n = len_; - len_ -= n; - } - - - // basic_string_ref string operations - basic_string_ref substr(size_type pos, size_type n=npos) const { - if ( pos > size()) - BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); - if ( n == npos || pos + n > size()) - n = size () - pos; - return basic_string_ref ( data() + pos, n ); - } - - int compare(basic_string_ref x) const { - const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_)); - return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 ); - } - - bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); } - bool starts_with(basic_string_ref x) const { - return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0; - } - - bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); } - bool ends_with(basic_string_ref x) const { - return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0; - } - - size_type find(basic_string_ref s) const { - const_iterator iter = std::search ( this->cbegin (), this->cend (), - s.cbegin (), s.cend (), traits::eq ); - return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); - } - - size_type find(charT c) const { - const_iterator iter = std::find_if ( this->cbegin (), this->cend (), - detail::string_ref_traits_eq ( c )); - return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); - } - - size_type rfind(basic_string_ref s) const { - const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (), - s.crbegin (), s.crend (), traits::eq ); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); - } - - size_type rfind(charT c) const { - const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (), - detail::string_ref_traits_eq ( c )); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); - } - - size_type find_first_of(charT c) const { return find (c); } - size_type find_last_of (charT c) const { return rfind (c); } - - size_type find_first_of(basic_string_ref s) const { - const_iterator iter = std::find_first_of - ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq ); - return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); - } - - size_type find_last_of(basic_string_ref s) const { - const_reverse_iterator iter = std::find_first_of - ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); - } - - size_type find_first_not_of(basic_string_ref s) const { - const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s ); - return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); - } - - size_type find_first_not_of(charT c) const { - for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter ) - if ( !traits::eq ( c, *iter )) - return std::distance ( this->cbegin (), iter ); - return npos; - } - - size_type find_last_not_of(basic_string_ref s) const { - const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s ); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); - } - - size_type find_last_not_of(charT c) const { - for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter ) - if ( !traits::eq ( c, *iter )) - return reverse_distance ( this->crbegin (), iter ); - return npos; - } - - private: - template - size_type reverse_distance ( r_iter first, r_iter last ) const { - return len_ - 1 - std::distance ( first, last ); - } - - template - Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const { - for ( ; first != last ; ++first ) - if ( 0 == traits::find ( s.ptr_, s.len_, *first )) - return first; - return last; - } - - - - const charT *ptr_; - std::size_t len_; - }; - - -// Comparison operators -// Equality - template - inline bool operator==(basic_string_ref x, basic_string_ref y) { - if ( x.size () != y.size ()) return false; - return x.compare(y) == 0; - } - - template - inline bool operator==(basic_string_ref x, const std::basic_string & y) { - return x == basic_string_ref(y); - } - - template - inline bool operator==(const std::basic_string & x, basic_string_ref y) { - return basic_string_ref(x) == y; - } - - template - inline bool operator==(basic_string_ref x, const charT * y) { - return x == basic_string_ref(y); - } - - template - inline bool operator==(const charT * x, basic_string_ref y) { - return basic_string_ref(x) == y; - } - -// Inequality - template - inline bool operator!=(basic_string_ref x, basic_string_ref y) { - if ( x.size () != y.size ()) return true; - return x.compare(y) != 0; - } - - template - inline bool operator!=(basic_string_ref x, const std::basic_string & y) { - return x != basic_string_ref(y); - } - - template - inline bool operator!=(const std::basic_string & x, basic_string_ref y) { - return basic_string_ref(x) != y; - } - - template - inline bool operator!=(basic_string_ref x, const charT * y) { - return x != basic_string_ref(y); - } - - template - inline bool operator!=(const charT * x, basic_string_ref y) { - return basic_string_ref(x) != y; - } - -// Less than - template - inline bool operator<(basic_string_ref x, basic_string_ref y) { - return x.compare(y) < 0; - } - - template - inline bool operator<(basic_string_ref x, const std::basic_string & y) { - return x < basic_string_ref(y); - } - - template - inline bool operator<(const std::basic_string & x, basic_string_ref y) { - return basic_string_ref(x) < y; - } - - template - inline bool operator<(basic_string_ref x, const charT * y) { - return x < basic_string_ref(y); - } - - template - inline bool operator<(const charT * x, basic_string_ref y) { - return basic_string_ref(x) < y; - } - -// Greater than - template - inline bool operator>(basic_string_ref x, basic_string_ref y) { - return x.compare(y) > 0; - } - - template - inline bool operator>(basic_string_ref x, const std::basic_string & y) { - return x > basic_string_ref(y); - } - - template - inline bool operator>(const std::basic_string & x, basic_string_ref y) { - return basic_string_ref(x) > y; - } - - template - inline bool operator>(basic_string_ref x, const charT * y) { - return x > basic_string_ref(y); - } - - template - inline bool operator>(const charT * x, basic_string_ref y) { - return basic_string_ref(x) > y; - } - -// Less than or equal to - template - inline bool operator<=(basic_string_ref x, basic_string_ref y) { - return x.compare(y) <= 0; - } - - template - inline bool operator<=(basic_string_ref x, const std::basic_string & y) { - return x <= basic_string_ref(y); - } - - template - inline bool operator<=(const std::basic_string & x, basic_string_ref y) { - return basic_string_ref(x) <= y; - } - - template - inline bool operator<=(basic_string_ref x, const charT * y) { - return x <= basic_string_ref(y); - } - - template - inline bool operator<=(const charT * x, basic_string_ref y) { - return basic_string_ref(x) <= y; - } - -// Greater than or equal to - template - inline bool operator>=(basic_string_ref x, basic_string_ref y) { - return x.compare(y) >= 0; - } - - template - inline bool operator>=(basic_string_ref x, const std::basic_string & y) { - return x >= basic_string_ref(y); - } - - template - inline bool operator>=(const std::basic_string & x, basic_string_ref y) { - return basic_string_ref(x) >= y; - } - - template - inline bool operator>=(basic_string_ref x, const charT * y) { - return x >= basic_string_ref(y); - } - - template - inline bool operator>=(const charT * x, basic_string_ref y) { - return basic_string_ref(x) >= y; - } - - namespace detail { - - template - inline void insert_fill_chars(std::basic_ostream& os, std::size_t n) { - enum { chunk_size = 8 }; - charT fill_chars[chunk_size]; - std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); - for (; n >= chunk_size && os.good(); n -= chunk_size) - os.write(fill_chars, static_cast< std::size_t >(chunk_size)); - if (n > 0 && os.good()) - os.write(fill_chars, n); - } - - template - void insert_aligned(std::basic_ostream& os, const basic_string_ref& str) { - const std::size_t size = str.size(); - const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; - const bool align_left = (os.flags() & std::basic_ostream::adjustfield) == std::basic_ostream::left; - if (!align_left) { - detail::insert_fill_chars(os, alignment_size); - if (os.good()) - os.write(str.data(), size); - } - else { - os.write(str.data(), size); - if (os.good()) - detail::insert_fill_chars(os, alignment_size); - } - } - - } // namespace detail - - // Inserter - template - inline std::basic_ostream& - operator<<(std::basic_ostream& os, const basic_string_ref& str) { - if (os.good()) { - const std::size_t size = str.size(); - const std::size_t w = static_cast< std::size_t >(os.width()); - if (w <= size) - os.write(str.data(), size); - else - detail::insert_aligned(os, str); - os.width(0); - } - return os; - } - -#if 0 - // numeric conversions - // - // These are short-term implementations. - // In a production environment, I would rather avoid the copying. - // - inline int stoi (string_ref str, size_t* idx=0, int base=10) { - return std::stoi ( std::string(str), idx, base ); - } - - inline long stol (string_ref str, size_t* idx=0, int base=10) { - return std::stol ( std::string(str), idx, base ); - } - - inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) { - return std::stoul ( std::string(str), idx, base ); - } - - inline long long stoll (string_ref str, size_t* idx=0, int base=10) { - return std::stoll ( std::string(str), idx, base ); - } - - inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) { - return std::stoull ( std::string(str), idx, base ); - } - - inline float stof (string_ref str, size_t* idx=0) { - return std::stof ( std::string(str), idx ); - } - - inline double stod (string_ref str, size_t* idx=0) { - return std::stod ( std::string(str), idx ); - } - - inline long double stold (string_ref str, size_t* idx=0) { - return std::stold ( std::string(str), idx ); - } - - inline int stoi (wstring_ref str, size_t* idx=0, int base=10) { - return std::stoi ( std::wstring(str), idx, base ); - } - - inline long stol (wstring_ref str, size_t* idx=0, int base=10) { - return std::stol ( std::wstring(str), idx, base ); - } - - inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) { - return std::stoul ( std::wstring(str), idx, base ); - } - - inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) { - return std::stoll ( std::wstring(str), idx, base ); - } - - inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) { - return std::stoull ( std::wstring(str), idx, base ); - } - - inline float stof (wstring_ref str, size_t* idx=0) { - return std::stof ( std::wstring(str), idx ); - } - - inline double stod (wstring_ref str, size_t* idx=0) { - return std::stod ( std::wstring(str), idx ); - } - - inline long double stold (wstring_ref str, size_t* idx=0) { - return std::stold ( std::wstring(str), idx ); - } -#endif - -} - -#if 0 -namespace std { - // Hashing - template<> struct hash; - template<> struct hash; - template<> struct hash; - template<> struct hash; -} -#endif +// The primary template has been changed to basic_string_view +// The convenience typedefs string_ref still exist. #endif diff --git a/include/boost/utility/string_ref_fwd.hpp b/include/boost/utility/string_ref_fwd.hpp index f58b98c..ead7b13 100644 --- a/include/boost/utility/string_ref_fwd.hpp +++ b/include/boost/utility/string_ref_fwd.hpp @@ -16,20 +16,20 @@ #define BOOST_STRING_REF_FWD_HPP #include -#include +#include +#include // for std::char_traits namespace boost { - template > class basic_string_ref; - typedef basic_string_ref > string_ref; - typedef basic_string_ref > wstring_ref; + typedef basic_string_view > string_ref; + typedef basic_string_view > wstring_ref; #ifndef BOOST_NO_CXX11_CHAR16_T - typedef basic_string_ref > u16string_ref; + typedef basic_string_view > u16string_ref; #endif #ifndef BOOST_NO_CXX11_CHAR32_T - typedef basic_string_ref > u32string_ref; + typedef basic_string_view > u32string_ref; #endif } diff --git a/include/boost/utility/string_view_fwd.hpp b/include/boost/utility/string_view_fwd.hpp index 3d2317a..3764f99 100644 --- a/include/boost/utility/string_view_fwd.hpp +++ b/include/boost/utility/string_view_fwd.hpp @@ -18,7 +18,7 @@ #define BOOST_STRING_VIEW_FWD_HPP #include -#include +#include // for std::char_traits namespace boost { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a2d5e4b..6e39ef6 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,9 +27,6 @@ test-suite utility [ run ../operators_test.cpp ../../test/build//boost_test_exec_monitor/static ] [ compile result_of_test.cpp ] [ run ../shared_iterator_test.cpp ] - [ run string_ref_test1.cpp unit_test_framework ] - [ run string_ref_test2.cpp unit_test_framework ] - [ run string_ref_test_io.cpp unit_test_framework ] [ run string_view_test1.cpp unit_test_framework ] [ run string_view_test2.cpp unit_test_framework ] [ run string_view_test_io.cpp unit_test_framework ] diff --git a/test/string_ref_test1.cpp b/test/string_ref_test1.cpp deleted file mode 100644 index f1d4df2..0000000 --- a/test/string_ref_test1.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - Copyright (c) Marshall Clow 2012-2012. - - Distributed under the Boost Software License, Version 1.0. (See accompanying - file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - - For more information, see http://www.boost.org -*/ - -#include -#include -#include - -#include - -#define BOOST_TEST_MAIN -#include - -typedef boost::string_ref string_ref; - -// Should be equal -void interop ( const std::string &str, string_ref ref ) { -// BOOST_CHECK ( str == ref ); - BOOST_CHECK ( str.size () == ref.size ()); - BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ())); - BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ())); - } - -void null_tests ( const char *p ) { -// All zero-length string-refs should be equal - string_ref sr1; // NULL, 0 - string_ref sr2 ( NULL, 0 ); - string_ref sr3 ( p, 0 ); - string_ref sr4 ( p ); - sr4.clear (); - - BOOST_CHECK ( sr1 == sr2 ); - BOOST_CHECK ( sr1 == sr3 ); - BOOST_CHECK ( sr2 == sr3 ); - BOOST_CHECK ( sr1 == sr4 ); - } - -// make sure that substrings work just like strings -void test_substr ( const std::string &str ) { - const size_t sz = str.size (); - string_ref ref ( str ); - -// Substrings at the end - for ( size_t i = 0; i <= sz; ++ i ) - interop ( str.substr ( i ), ref.substr ( i )); - -// Substrings at the beginning - for ( size_t i = 0; i <= sz; ++ i ) - interop ( str.substr ( 0, i ), ref.substr ( 0, i )); - -// All possible substrings - for ( size_t i = 0; i < sz; ++i ) - for ( size_t j = i; j < sz; ++j ) - interop ( str.substr ( i, j ), ref.substr ( i, j )); - } - -// make sure that removing prefixes and suffixes work just like strings -void test_remove ( const std::string &str ) { - const size_t sz = str.size (); - std::string work; - string_ref ref; - - for ( size_t i = 1; i <= sz; ++i ) { - work = str; - ref = str; - while ( ref.size () >= i ) { - interop ( work, ref ); - work.erase ( 0, i ); - ref.remove_prefix (i); - } - } - - for ( size_t i = 1; i < sz; ++ i ) { - work = str; - ref = str; - while ( ref.size () >= i ) { - interop ( work, ref ); - work.erase ( work.size () - i, i ); - ref.remove_suffix (i); - } - } - } - -const char *test_strings [] = { - "", - "1", - "ABCDEFGHIJKLMNOPQRSTUVWXYZ", - "0123456789", - NULL - }; - -BOOST_AUTO_TEST_CASE( test_main ) -{ - const char **p = &test_strings[0]; - - while ( *p != NULL ) { - interop ( *p, *p ); - test_substr ( *p ); - test_remove ( *p ); - null_tests ( *p ); - - p++; - } -} diff --git a/test/string_ref_test2.cpp b/test/string_ref_test2.cpp deleted file mode 100644 index 9559c67..0000000 --- a/test/string_ref_test2.cpp +++ /dev/null @@ -1,318 +0,0 @@ -/* - Copyright (c) Marshall Clow 2012-2012. - - Distributed under the Boost Software License, Version 1.0. (See accompanying - file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - - For more information, see http://www.boost.org -*/ - -#include -#include // for std::strchr - -#include - -#define BOOST_TEST_MAIN -#include - -typedef boost::string_ref string_ref; - -void ends_with ( const char *arg ) { - const size_t sz = std::strlen ( arg ); - string_ref sr ( arg ); - string_ref sr2 ( arg ); - const char *p = arg; - - while ( *p ) { - BOOST_CHECK ( sr.ends_with ( p )); - ++p; - } - - while ( !sr2.empty ()) { - BOOST_CHECK ( sr.ends_with ( sr2 )); - sr2.remove_prefix (1); - } - - sr2 = arg; - while ( !sr2.empty ()) { - BOOST_CHECK ( sr.ends_with ( sr2 )); - sr2.remove_prefix (1); - } - - char ch = sz == 0 ? '\0' : arg [ sz - 1 ]; - sr2 = arg; - if ( sz > 0 ) - BOOST_CHECK ( sr2.ends_with ( ch )); - BOOST_CHECK ( !sr2.ends_with ( ++ch )); - BOOST_CHECK ( sr2.ends_with ( string_ref ())); - } - -void starts_with ( const char *arg ) { - const size_t sz = std::strlen ( arg ); - string_ref sr ( arg ); - string_ref sr2 ( arg ); - const char *p = arg + std::strlen ( arg ) - 1; - while ( p >= arg ) { - std::string foo ( arg, p + 1 ); - BOOST_CHECK ( sr.starts_with ( foo )); - --p; - } - - while ( !sr2.empty ()) { - BOOST_CHECK ( sr.starts_with ( sr2 )); - sr2.remove_suffix (1); - } - - char ch = *arg; - sr2 = arg; - if ( sz > 0 ) - BOOST_CHECK ( sr2.starts_with ( ch )); - BOOST_CHECK ( !sr2.starts_with ( ++ch )); - BOOST_CHECK ( sr2.starts_with ( string_ref ())); - } - -void reverse ( const char *arg ) { -// Round trip - string_ref sr1 ( arg ); - std::string string1 ( sr1.rbegin (), sr1.rend ()); - string_ref sr2 ( string1 ); - std::string string2 ( sr2.rbegin (), sr2.rend ()); - - BOOST_CHECK ( std::equal ( sr2.rbegin (), sr2.rend (), arg )); - BOOST_CHECK ( string2 == arg ); - BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ())); - } - -// This helper function eliminates signed vs. unsigned warnings -string_ref::size_type ptr_diff ( const char *res, const char *base ) { - BOOST_CHECK ( res >= base ); - return static_cast ( res - base ); - } - -void find ( const char *arg ) { - string_ref sr1; - string_ref sr2; - const char *p; - -// Look for each character in the string(searching from the start) - p = arg; - sr1 = arg; - while ( *p ) { - string_ref::size_type pos = sr1.find(*p); - BOOST_CHECK ( pos != string_ref::npos && ( pos <= ptr_diff ( p, arg ))); - ++p; - } - -// Look for each character in the string (searching from the end) - p = arg; - sr1 = arg; - while ( *p ) { - string_ref::size_type pos = sr1.rfind(*p); - BOOST_CHECK ( pos != string_ref::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg ))); - ++p; - } - -// Look for pairs on characters (searching from the start) - sr1 = arg; - p = arg; - while ( *p && *(p+1)) { - string_ref sr3 ( p, 2 ); - string_ref::size_type pos = sr1.find ( sr3 ); - BOOST_CHECK ( pos != string_ref::npos && pos <= static_cast( p - arg )); - p++; - } - - sr1 = arg; - p = arg; -// for all possible chars, see if we find them in the right place. -// Note that strchr will/might do the _wrong_ thing if we search for NULL - for ( int ch = 1; ch < 256; ++ch ) { - string_ref::size_type pos = sr1.find(ch); - const char *strp = std::strchr ( arg, ch ); - BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); - if ( strp != NULL ) - BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); - } - - sr1 = arg; - p = arg; -// for all possible chars, see if we find them in the right place. -// Note that strchr will/might do the _wrong_ thing if we search for NULL - for ( int ch = 1; ch < 256; ++ch ) { - string_ref::size_type pos = sr1.rfind(ch); - const char *strp = std::strrchr ( arg, ch ); - BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); - if ( strp != NULL ) - BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); - } - - -// Find everything at the start - p = arg; - sr1 = arg; - while ( !sr1.empty ()) { - string_ref::size_type pos = sr1.find(*p); - BOOST_CHECK ( pos == 0 ); - sr1.remove_prefix (1); - ++p; - } - -// Find everything at the end - sr1 = arg; - p = arg + std::strlen ( arg ) - 1; - while ( !sr1.empty ()) { - string_ref::size_type pos = sr1.rfind(*p); - BOOST_CHECK ( pos == sr1.size () - 1 ); - sr1.remove_suffix (1); - --p; - } - -// Find everything at the start - sr1 = arg; - p = arg; - while ( !sr1.empty ()) { - string_ref::size_type pos = sr1.find_first_of(*p); - BOOST_CHECK ( pos == 0 ); - sr1.remove_prefix (1); - ++p; - } - - -// Find everything at the end - sr1 = arg; - p = arg + std::strlen ( arg ) - 1; - while ( !sr1.empty ()) { - string_ref::size_type pos = sr1.find_last_of(*p); - BOOST_CHECK ( pos == sr1.size () - 1 ); - sr1.remove_suffix (1); - --p; - } - -// Basic sanity checking for "find_first_of / find_first_not_of" - sr1 = arg; - sr2 = arg; - while ( !sr1.empty() ) { - BOOST_CHECK ( sr1.find_first_of ( sr2 ) == 0 ); - BOOST_CHECK ( sr1.find_first_not_of ( sr2 ) == string_ref::npos ); - sr1.remove_prefix ( 1 ); - } - - p = arg; - sr1 = arg; - while ( *p ) { - string_ref::size_type pos1 = sr1.find_first_of(*p); - string_ref::size_type pos2 = sr1.find_first_not_of(*p); - BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg )); - if ( pos2 != string_ref::npos ) { - for ( size_t i = 0 ; i < pos2; ++i ) - BOOST_CHECK ( sr1[i] == *p ); - BOOST_CHECK ( sr1 [ pos2 ] != *p ); - } - - BOOST_CHECK ( pos2 != pos1 ); - ++p; - } - -// Basic sanity checking for "find_last_of / find_last_not_of" - sr1 = arg; - sr2 = arg; - while ( !sr1.empty() ) { - BOOST_CHECK ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 )); - BOOST_CHECK ( sr1.find_last_not_of ( sr2 ) == string_ref::npos ); - sr1.remove_suffix ( 1 ); - } - - p = arg; - sr1 = arg; - while ( *p ) { - string_ref::size_type pos1 = sr1.find_last_of(*p); - string_ref::size_type pos2 = sr1.find_last_not_of(*p); - BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg )); - BOOST_CHECK ( pos2 == string_ref::npos || pos1 < sr1.size ()); - if ( pos2 != string_ref::npos ) { - for ( size_t i = sr1.size () -1 ; i > pos2; --i ) - BOOST_CHECK ( sr1[i] == *p ); - BOOST_CHECK ( sr1 [ pos2 ] != *p ); - } - - BOOST_CHECK ( pos2 != pos1 ); - ++p; - } - - } - - -void to_string ( const char *arg ) { - string_ref sr1; - std::string str1; - std::string str2; - - str1.assign ( arg ); - sr1 = arg; -// str2 = sr1.to_string > (); - str2 = sr1.to_string (); - BOOST_CHECK ( str1 == str2 ); - -#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS - std::string str3 = static_cast ( sr1 ); - BOOST_CHECK ( str1 == str3 ); -#endif - } - -void compare ( const char *arg ) { - string_ref sr1; - std::string str1; - std::string str2 = str1; - - str1.assign ( arg ); - sr1 = arg; - BOOST_CHECK ( sr1 == sr1); // compare string_ref and string_ref - BOOST_CHECK ( sr1 == str1); // compare string and string_ref - BOOST_CHECK ( str1 == sr1 ); // compare string_ref and string - BOOST_CHECK ( sr1 == arg ); // compare string_ref and pointer - BOOST_CHECK ( arg == sr1 ); // compare pointer and string_ref - - if ( sr1.size () > 0 ) { - (*str1.rbegin())++; - BOOST_CHECK ( sr1 != str1 ); - BOOST_CHECK ( str1 != sr1 ); - BOOST_CHECK ( sr1 < str1 ); - BOOST_CHECK ( sr1 <= str1 ); - BOOST_CHECK ( str1 > sr1 ); - BOOST_CHECK ( str1 >= sr1 ); - - (*str1.rbegin()) -= 2; - BOOST_CHECK ( sr1 != str1 ); - BOOST_CHECK ( str1 != sr1 ); - BOOST_CHECK ( sr1 > str1 ); - BOOST_CHECK ( sr1 >= str1 ); - BOOST_CHECK ( str1 < sr1 ); - BOOST_CHECK ( str1 <= sr1 ); - } - } - -const char *test_strings [] = { - "", - "0", - "abc", - "AAA", // all the same - "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf", - "abc\0asdfadsfasf", - NULL - }; - -BOOST_AUTO_TEST_CASE( test_main ) -{ - const char **p = &test_strings[0]; - - while ( *p != NULL ) { - starts_with ( *p ); - ends_with ( *p ); - reverse ( *p ); - find ( *p ); - to_string ( *p ); - compare ( *p ); - - p++; - } -} diff --git a/test/string_ref_test_io.cpp b/test/string_ref_test_io.cpp deleted file mode 100644 index 72f5837..0000000 --- a/test/string_ref_test_io.cpp +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright Andrey Semashev 2013. - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ -/*! - * \file string_ref_test_io.cpp - * \author Andrey Semashev - * \date 26.05.2013 - * - * \brief This header contains tests for stream operations of \c basic_string_ref. - */ - -#define BOOST_TEST_MODULE string_ref_test_io - -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -typedef boost::mpl::vector< - char -#if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_STD_WSTREAMBUF) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) - , wchar_t -#endif -/* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available. -#if !defined(BOOST_NO_CXX11_CHAR16_T) - , char16_t -#endif -#if !defined(BOOST_NO_CXX11_CHAR32_T) - , char32_t -#endif -*/ ->::type char_types; - -static const char* test_strings[] = -{ - "begin", - "abcd", - "end" -}; - -//! The context with test data for particular character type -template< typename CharT > -struct context -{ - typedef CharT char_type; - typedef std::basic_string< char_type > string_type; - typedef std::basic_ostringstream< char_type > ostream_type; - - string_type begin, abcd, end; - - context() - { - boost::string_ref str = test_strings[0]; - std::copy(str.begin(), str.end(), std::back_inserter(begin)); - - str = test_strings[1]; - std::copy(str.begin(), str.end(), std::back_inserter(abcd)); - - str = test_strings[2]; - std::copy(str.begin(), str.end(), std::back_inserter(end)); - } -}; - -// Test regular output -BOOST_AUTO_TEST_CASE_TEMPLATE(string_ref_output, CharT, char_types) -{ - typedef CharT char_type; - typedef std::basic_ostringstream< char_type > ostream_type; - typedef boost::basic_string_ref< char_type > string_ref_type; - - context< char_type > ctx; - - ostream_type strm; - strm << string_ref_type(ctx.abcd); - BOOST_CHECK(strm.str() == ctx.abcd); -} - -// Test support for padding -BOOST_AUTO_TEST_CASE_TEMPLATE(padding, CharT, char_types) -{ - typedef CharT char_type; - typedef std::basic_ostringstream< char_type > ostream_type; - typedef boost::basic_string_ref< char_type > string_ref_type; - - context< char_type > ctx; - - // Test for padding - { - ostream_type strm_ref; - strm_ref << ctx.begin << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; - - ostream_type strm_correct; - strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end; - - BOOST_CHECK(strm_ref.str() == strm_correct.str()); - } - - // Test for long padding - { - ostream_type strm_ref; - strm_ref << ctx.begin << std::setw(100) << string_ref_type(ctx.abcd) << ctx.end; - - ostream_type strm_correct; - strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end; - - BOOST_CHECK(strm_ref.str() == strm_correct.str()); - } - - // Test that short width does not truncate the string - { - ostream_type strm_ref; - strm_ref << ctx.begin << std::setw(1) << string_ref_type(ctx.abcd) << ctx.end; - - ostream_type strm_correct; - strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end; - - BOOST_CHECK(strm_ref.str() == strm_correct.str()); - } -} - -// Test support for padding fill -BOOST_AUTO_TEST_CASE_TEMPLATE(padding_fill, CharT, char_types) -{ - typedef CharT char_type; - typedef std::basic_ostringstream< char_type > ostream_type; - typedef boost::basic_string_ref< char_type > string_ref_type; - - context< char_type > ctx; - - ostream_type strm_ref; - strm_ref << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; - - ostream_type strm_correct; - strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end; - - BOOST_CHECK(strm_ref.str() == strm_correct.str()); -} - -// Test support for alignment -BOOST_AUTO_TEST_CASE_TEMPLATE(alignment, CharT, char_types) -{ - typedef CharT char_type; - typedef std::basic_ostringstream< char_type > ostream_type; - typedef boost::basic_string_ref< char_type > string_ref_type; - - context< char_type > ctx; - - // Left alignment - { - ostream_type strm_ref; - strm_ref << ctx.begin << std::left << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; - - ostream_type strm_correct; - strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end; - - BOOST_CHECK(strm_ref.str() == strm_correct.str()); - } - - // Right alignment - { - ostream_type strm_ref; - strm_ref << ctx.begin << std::right << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; - - ostream_type strm_correct; - strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end; - - BOOST_CHECK(strm_ref.str() == strm_correct.str()); - } -} From 08a1b7da61a6bb18932e115405e31d1b79a78100 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Mon, 22 Feb 2016 20:39:16 +0100 Subject: [PATCH 02/12] Added ADL protector --- include/boost/operators.hpp | 490 ++++++++--------- include/boost/operators_v1.hpp | 951 +++++++++++++++++++++++++++++++++ 2 files changed, 1167 insertions(+), 274 deletions(-) create mode 100644 include/boost/operators_v1.hpp diff --git a/include/boost/operators.hpp b/include/boost/operators.hpp index ab20330..d8a1038 100644 --- a/include/boost/operators.hpp +++ b/include/boost/operators.hpp @@ -1,6 +1,7 @@ // Boost operators.hpp header file ----------------------------------------// // (C) Copyright David Abrahams, Jeremy Siek, Daryle Walker 1999-2001. +// (C) Copyright Daniel Frey 2002-2016. // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -8,6 +9,8 @@ // See http://www.boost.org/libs/utility/operators.htm for documentation. // Revision History +// 22 Feb 16 Added ADL protection, preserve old work-arounds in +// operators_v1.hpp and clean up this file. (Daniel Frey) // 16 Dec 10 Limit warning suppression for 4284 to older versions of VC++ // (Matthew Bradbury, fixes #4432) // 07 Aug 08 Added "euclidean" spelling. (Daniel Frey) @@ -24,8 +27,8 @@ // additional classes for groups of related operators added; // workaround for empty base class optimization // bug of GCC 3.0 (Helmut Zeisel) -// 25 Jun 01 output_iterator_helper changes: removed default template -// parameters, added support for self-proxying, additional +// 25 Jun 01 output_iterator_helper changes: removed default template +// parameters, added support for self-proxying, additional // documentation and tests (Aleksey Gurtovoy) // 29 May 01 Added operator classes for << and >>. Added input and output // iterator helper classes. Added classes to connect equality and @@ -38,18 +41,18 @@ // 04 Jul 00 Fixed NO_OPERATORS_IN_NAMESPACE bugs, major cleanup and // refactoring of compiler workarounds, additional documentation // (Alexy Gurtovoy and Mark Rodgers with some help and prompting from -// Dave Abrahams) +// Dave Abrahams) // 28 Jun 00 General cleanup and integration of bugfixes from Mark Rodgers and // Jeremy Siek (Dave Abrahams) // 20 Jun 00 Changes to accommodate Borland C++Builder 4 and Borland C++ 5.5 // (Mark Rodgers) // 20 Jun 00 Minor fixes to the prior revision (Aleksey Gurtovoy) // 10 Jun 00 Support for the base class chaining technique was added -// (Aleksey Gurtovoy). See documentation and the comments below -// for the details. +// (Aleksey Gurtovoy). See documentation and the comments below +// for the details. // 12 Dec 99 Initial version with iterator operators (Jeremy Siek) // 18 Nov 99 Change name "divideable" to "dividable", remove unnecessary -// specializations of dividable, subtractable, modable (Ed Brey) +// specializations of dividable, subtractable, modable (Ed Brey) // 17 Nov 99 Add comments (Beman Dawes) // Remove unnecessary specialization of operators<> (Ed Brey) // 15 Nov 99 Fix less_than_comparable second operand type for first two @@ -60,8 +63,8 @@ // 10 Nov 99 Initial version // 10 Jun 00: -// An additional optional template parameter was added to most of -// operator templates to support the base class chaining technique (see +// An additional optional template parameter was added to most of +// operator templates to support the base class chaining technique (see // documentation for the details). Unfortunately, a straightforward // implementation of this change would have broken compatibility with the // previous version of the library by making it impossible to use the same @@ -72,16 +75,22 @@ // One of the complications is an existence of special auxiliary class template // 'is_chained_base<>' (see 'detail' namespace below), which is used // to determine whether its template parameter is a library's operator template -// or not. You have to specialize 'is_chained_base<>' for each new +// or not. You have to specialize 'is_chained_base<>' for each new // operator template you add to the library. // -// However, most of the non-trivial implementation details are hidden behind +// However, most of the non-trivial implementation details are hidden behind // several local macros defined below, and as soon as you understand them, -// you understand the whole library implementation. +// you understand the whole library implementation. #ifndef BOOST_OPERATORS_HPP #define BOOST_OPERATORS_HPP +// If old work-arounds are needed, refer to the preserved version without +// ADL protection. +#if defined(BOOST_NO_OPERATORS_IN_NAMESPACE) || defined(BOOST_USE_OPERATORS_V1) +#include "operators_v1.hpp" +#else + #include #include @@ -93,35 +102,30 @@ #endif #if BOOST_WORKAROUND(BOOST_MSVC, < 1600) -# pragma warning( disable : 4284 ) // complaint about return type of +# pragma warning( disable : 4284 ) // complaint about return type of #endif // operator-> not begin a UDT -namespace boost { -namespace detail { +// In this section we supply the xxxx1 and xxxx2 forms of the operator +// templates, which are explicitly targeted at the 1-type-argument and +// 2-type-argument operator forms, respectively. + +namespace boost +{ +namespace operators_impl +{ +namespace detail +{ template class empty_base {}; } // namespace detail -} // namespace boost - -// In this section we supply the xxxx1 and xxxx2 forms of the operator -// templates, which are explicitly targeted at the 1-type-argument and -// 2-type-argument operator forms, respectively. Some compilers get confused -// when inline friend functions are overloaded in namespaces other than the -// global namespace. When BOOST_NO_OPERATORS_IN_NAMESPACE is defined, all of -// these templates must go in the global namespace. - -#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE -namespace boost -{ -#endif // Basic operator classes (contributed by Dave Abrahams) ------------------// // Note that friend functions defined in a class are implicitly inline. // See the C++ std, 11.4 [class.friend] paragraph 5 -template > +template > struct less_than_comparable2 : B { friend bool operator<=(const T& x, const U& y) { return !static_cast(x > y); } @@ -132,7 +136,7 @@ struct less_than_comparable2 : B friend bool operator>=(const U& x, const T& y) { return !static_cast(y > x); } }; -template > +template > struct less_than_comparable1 : B { friend bool operator>(const T& x, const T& y) { return y < x; } @@ -140,7 +144,7 @@ struct less_than_comparable1 : B friend bool operator>=(const T& x, const T& y) { return !static_cast(x < y); } }; -template > +template > struct equality_comparable2 : B { friend bool operator==(const U& y, const T& x) { return x == y; } @@ -148,7 +152,7 @@ struct equality_comparable2 : B friend bool operator!=(const T& y, const U& x) { return !static_cast(y == x); } }; -template > +template > struct equality_comparable1 : B { friend bool operator!=(const T& x, const T& y) { return !static_cast(x == y); } @@ -166,43 +170,43 @@ struct equality_comparable1 : B // If the compiler has no NRVO, this is the best symmetric // implementation available. -#define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( const T& lhs, const U& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ - friend T operator OP( const U& lhs, const T& rhs ) \ - { T nrv( rhs ); nrv OP##= lhs; return nrv; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( const T& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +#define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { T nrv( rhs ); nrv OP##= lhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ }; -#define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( const T& lhs, const U& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ -}; \ - \ -template > \ -struct BOOST_OPERATOR2_LEFT(NAME) : B \ -{ \ - friend T operator OP( const U& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( const T& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +#define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct BOOST_OPERATOR2_LEFT(NAME) : B \ +{ \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ }; #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) @@ -213,34 +217,34 @@ struct NAME##1 : B \ // optimization opportunities to the compiler :) #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ -template > \ +template > \ struct NAME##2 : B \ { \ friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \ }; \ \ -template > \ +template > \ struct NAME##1 : B \ { \ friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ }; #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ -template > \ +template > \ struct NAME##2 : B \ { \ friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ }; \ \ -template > \ +template > \ struct BOOST_OPERATOR2_LEFT(NAME) : B \ { \ friend T operator OP( const U& lhs, const T& rhs ) \ { return T( lhs ) OP##= rhs; } \ }; \ \ -template > \ +template > \ struct NAME##1 : B \ { \ friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ @@ -263,7 +267,7 @@ BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | ) // incrementable and decrementable contributed by Jeremy Siek -template > +template > struct incrementable : B { friend T operator++(T& x, int) @@ -276,7 +280,7 @@ private: // The use of this typedef works around a Borland bug typedef T incrementable_type; }; -template > +template > struct decrementable : B { friend T operator--(T& x, int) @@ -291,16 +295,16 @@ private: // The use of this typedef works around a Borland bug // Iterator operator classes (contributed by Jeremy Siek) ------------------// -template > +template > struct dereferenceable : B { P operator->() const - { - return &*static_cast(*this); + { + return &*static_cast(*this); } }; -template > +template > struct indexable : B { R operator[](I n) const @@ -314,34 +318,34 @@ struct indexable : B #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) -#define BOOST_BINARY_OPERATOR( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( const T& lhs, const U& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( const T& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +#define BOOST_BINARY_OPERATOR( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ }; #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) -#define BOOST_BINARY_OPERATOR( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ +#define BOOST_BINARY_OPERATOR( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ }; #endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) @@ -351,7 +355,7 @@ BOOST_BINARY_OPERATOR( right_shiftable, >> ) #undef BOOST_BINARY_OPERATOR -template > +template > struct equivalent2 : B { friend bool operator==(const T& x, const U& y) @@ -360,7 +364,7 @@ struct equivalent2 : B } }; -template > +template > struct equivalent1 : B { friend bool operator==(const T&x, const T&y) @@ -369,7 +373,7 @@ struct equivalent1 : B } }; -template > +template > struct partially_ordered2 : B { friend bool operator<=(const T& x, const U& y) @@ -386,7 +390,7 @@ struct partially_ordered2 : B { return static_cast(y < x) || static_cast(y == x); } }; -template > +template > struct partially_ordered1 : B { friend bool operator>(const T& x, const T& y) @@ -399,161 +403,161 @@ struct partially_ordered1 : B // Combined operator classes (contributed by Daryle Walker) ----------------// -template > +template > struct totally_ordered2 : less_than_comparable2 > {}; -template > +template > struct totally_ordered1 : less_than_comparable1 > {}; -template > +template > struct additive2 : addable2 > {}; -template > +template > struct additive1 : addable1 > {}; -template > +template > struct multiplicative2 : multipliable2 > {}; -template > +template > struct multiplicative1 : multipliable1 > {}; -template > +template > struct integer_multiplicative2 : multiplicative2 > {}; -template > +template > struct integer_multiplicative1 : multiplicative1 > {}; -template > +template > struct arithmetic2 : additive2 > {}; -template > +template > struct arithmetic1 : additive1 > {}; -template > +template > struct integer_arithmetic2 : additive2 > {}; -template > +template > struct integer_arithmetic1 : additive1 > {}; -template > +template > struct bitwise2 : xorable2 > > {}; -template > +template > struct bitwise1 : xorable1 > > {}; -template > +template > struct unit_steppable : incrementable > {}; -template > +template > struct shiftable2 : left_shiftable2 > {}; -template > +template > struct shiftable1 : left_shiftable1 > {}; -template > +template > struct ring_operators2 : additive2 > > {}; -template > +template > struct ring_operators1 : additive1 > {}; -template > +template > struct ordered_ring_operators2 : ring_operators2 > {}; -template > +template > struct ordered_ring_operators1 : ring_operators1 > {}; -template > +template > struct field_operators2 : ring_operators2 > > {}; -template > +template > struct field_operators1 : ring_operators1 > {}; -template > +template > struct ordered_field_operators2 : field_operators2 > {}; -template > +template > struct ordered_field_operators1 : field_operators1 > {}; -template > +template > struct euclidian_ring_operators2 : ring_operators2 > > > > {}; -template > +template > struct euclidian_ring_operators1 : ring_operators1 > > {}; -template > +template > struct ordered_euclidian_ring_operators2 : totally_ordered2 > {}; -template > +template > struct ordered_euclidian_ring_operators1 : totally_ordered1 > {}; -template > +template > struct euclidean_ring_operators2 : ring_operators2 > > > > {}; -template > +template > struct euclidean_ring_operators1 : ring_operators1 > > {}; -template > +template > struct ordered_euclidean_ring_operators2 : totally_ordered2 > {}; -template > +template > struct ordered_euclidean_ring_operators1 : totally_ordered1 > {}; -template > +template > struct input_iteratable : equality_comparable1 > > {}; -template > +template > struct output_iteratable : incrementable {}; -template > +template > struct forward_iteratable : input_iteratable {}; -template > +template > struct bidirectional_iteratable : forward_iteratable > +template > struct random_access_iteratable : bidirectional_iteratable > > > {}; -#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE -} // namespace boost -#endif // BOOST_NO_OPERATORS_IN_NAMESPACE - - -// BOOST_IMPORT_TEMPLATE1 .. BOOST_IMPORT_TEMPLATE4 - -// -// When BOOST_NO_OPERATORS_IN_NAMESPACE is defined we need a way to import an -// operator template into the boost namespace. BOOST_IMPORT_TEMPLATE1 is used -// for one-argument forms of operator templates; BOOST_IMPORT_TEMPLATE2 for -// two-argument forms. Note that these macros expect to be invoked from within -// boost. - -#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE - - // The template is already in boost so we have nothing to do. -# define BOOST_IMPORT_TEMPLATE4(template_name) -# define BOOST_IMPORT_TEMPLATE3(template_name) -# define BOOST_IMPORT_TEMPLATE2(template_name) -# define BOOST_IMPORT_TEMPLATE1(template_name) - -#else // BOOST_NO_OPERATORS_IN_NAMESPACE - -# ifndef BOOST_NO_USING_TEMPLATE - - // Bring the names in with a using-declaration - // to avoid stressing the compiler. -# define BOOST_IMPORT_TEMPLATE4(template_name) using ::template_name; -# define BOOST_IMPORT_TEMPLATE3(template_name) using ::template_name; -# define BOOST_IMPORT_TEMPLATE2(template_name) using ::template_name; -# define BOOST_IMPORT_TEMPLATE1(template_name) using ::template_name; - -# else - - // Otherwise, because a Borland C++ 5.5 bug prevents a using declaration - // from working, we are forced to use inheritance for that compiler. -# define BOOST_IMPORT_TEMPLATE4(template_name) \ - template > \ - struct template_name : ::template_name {}; - -# define BOOST_IMPORT_TEMPLATE3(template_name) \ - template > \ - struct template_name : ::template_name {}; - -# define BOOST_IMPORT_TEMPLATE2(template_name) \ - template > \ - struct template_name : ::template_name {}; - -# define BOOST_IMPORT_TEMPLATE1(template_name) \ - template > \ - struct template_name : ::template_name {}; - -# endif // BOOST_NO_USING_TEMPLATE - -#endif // BOOST_NO_OPERATORS_IN_NAMESPACE // -// Here's where we put it all together, defining the xxxx forms of the templates -// in namespace boost. We also define specializations of is_chained_base<> for -// the xxxx, xxxx1, and xxxx2 templates, importing them into boost:: as -// necessary. +// Here's where we put it all together, defining the xxxx forms of the templates. +// We also define specializations of is_chained_base<> for +// the xxxx, xxxx1, and xxxx2 templates. // +namespace detail +{ + +// A type parameter is used instead of a plain bool because Borland's compiler +// didn't cope well with the more obvious non-type template parameter. +struct true_t {}; +struct false_t {}; + +} // namespace detail + // is_chained_base<> - a traits class used to distinguish whether an operator // template argument is being used for base class chaining, or is specifying a // 2nd argument type. -namespace boost { -// A type parameter is used instead of a plain bool because Borland's compiler -// didn't cope well with the more obvious non-type template parameter. -namespace detail { - struct true_t {}; - struct false_t {}; -} // namespace detail - // Unspecialized version assumes that most types are not being used for base // class chaining. We specialize for the operator templates defined in this // library. template struct is_chained_base { - typedef ::boost::detail::false_t value; + typedef detail::false_t value; }; -} // namespace boost - -// Import a 4-type-argument operator template into boost (if necessary) and -// provide a specialization of 'is_chained_base<>' for it. -# define BOOST_OPERATOR_TEMPLATE4(template_name4) \ - BOOST_IMPORT_TEMPLATE4(template_name4) \ - template \ - struct is_chained_base< ::boost::template_name4 > { \ - typedef ::boost::detail::true_t value; \ +// Provide a specialization of 'is_chained_base<>' +// for a 4-type-argument operator template. +# define BOOST_OPERATOR_TEMPLATE4(template_name4) \ + template \ + struct is_chained_base< template_name4 > { \ + typedef detail::true_t value; \ }; -// Import a 3-type-argument operator template into boost (if necessary) and -// provide a specialization of 'is_chained_base<>' for it. -# define BOOST_OPERATOR_TEMPLATE3(template_name3) \ - BOOST_IMPORT_TEMPLATE3(template_name3) \ - template \ - struct is_chained_base< ::boost::template_name3 > { \ - typedef ::boost::detail::true_t value; \ +// Provide a specialization of 'is_chained_base<>' +// for a 3-type-argument operator template. +# define BOOST_OPERATOR_TEMPLATE3(template_name3) \ + template \ + struct is_chained_base< template_name3 > { \ + typedef detail::true_t value; \ }; -// Import a 2-type-argument operator template into boost (if necessary) and -// provide a specialization of 'is_chained_base<>' for it. -# define BOOST_OPERATOR_TEMPLATE2(template_name2) \ - BOOST_IMPORT_TEMPLATE2(template_name2) \ - template \ - struct is_chained_base< ::boost::template_name2 > { \ - typedef ::boost::detail::true_t value; \ +// Provide a specialization of 'is_chained_base<>' +// for a 2-type-argument operator template. +# define BOOST_OPERATOR_TEMPLATE2(template_name2) \ + template \ + struct is_chained_base< template_name2 > { \ + typedef detail::true_t value; \ }; -// Import a 1-type-argument operator template into boost (if necessary) and -// provide a specialization of 'is_chained_base<>' for it. -# define BOOST_OPERATOR_TEMPLATE1(template_name1) \ - BOOST_IMPORT_TEMPLATE1(template_name1) \ - template \ - struct is_chained_base< ::boost::template_name1 > { \ - typedef ::boost::detail::true_t value; \ +// Provide a specialization of 'is_chained_base<>' +// for a 1-type-argument operator template. +# define BOOST_OPERATOR_TEMPLATE1(template_name1) \ + template \ + struct is_chained_base< template_name1 > { \ + typedef detail::true_t value; \ }; // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it @@ -778,34 +722,34 @@ template struct is_chained_base { // implementation in terms of either '1' or '2'. // -# define BOOST_OPERATOR_TEMPLATE(template_name) \ -template \ - ,class O = typename is_chained_base::value \ - > \ -struct template_name : template_name##2 {}; \ - \ -template \ -struct template_name \ - : template_name##1 {}; \ - \ -template \ -struct template_name \ - : template_name##1 {}; \ - \ -template \ -struct is_chained_base< ::boost::template_name > { \ - typedef ::boost::detail::true_t value; \ -}; \ - \ -BOOST_OPERATOR_TEMPLATE2(template_name##2) \ +# define BOOST_OPERATOR_TEMPLATE(template_name) \ +template \ + ,class O = typename is_chained_base::value \ + > \ +struct template_name; \ + \ +template \ +struct template_name \ + : template_name##2 {}; \ + \ +template \ +struct template_name, detail::true_t> \ + : template_name##1 {}; \ + \ +template \ +struct template_name \ + : template_name##1 {}; \ + \ +template \ +struct is_chained_base< template_name > { \ + typedef detail::true_t value; \ +}; \ + \ +BOOST_OPERATOR_TEMPLATE2(template_name##2) \ BOOST_OPERATOR_TEMPLATE1(template_name##1) - - -namespace boost { - BOOST_OPERATOR_TEMPLATE(less_than_comparable) BOOST_OPERATOR_TEMPLATE(equality_comparable) BOOST_OPERATOR_TEMPLATE(multipliable) @@ -859,13 +803,7 @@ BOOST_OPERATOR_TEMPLATE4(random_access_iteratable) #undef BOOST_OPERATOR_TEMPLATE3 #undef BOOST_OPERATOR_TEMPLATE2 #undef BOOST_OPERATOR_TEMPLATE1 -#undef BOOST_IMPORT_TEMPLATE1 -#undef BOOST_IMPORT_TEMPLATE2 -#undef BOOST_IMPORT_TEMPLATE3 -#undef BOOST_IMPORT_TEMPLATE4 -// The following 'operators' classes can only be used portably if the derived class -// declares ALL of the required member operators. template struct operators2 : totally_ordered2 > {}; template @@ -941,10 +879,14 @@ struct random_access_iterator_helper } }; // random_access_iterator_helper +} // namespace operators_impl +using namespace operators_impl; + } // namespace boost #if defined(__sgi) && !defined(__GNUC__) #pragma reset woff 1234 #endif +#endif // BOOST_NO_OPERATORS_IN_NAMESPACE #endif // BOOST_OPERATORS_HPP diff --git a/include/boost/operators_v1.hpp b/include/boost/operators_v1.hpp new file mode 100644 index 0000000..e1c53e8 --- /dev/null +++ b/include/boost/operators_v1.hpp @@ -0,0 +1,951 @@ +// Boost operators.hpp header file ----------------------------------------// + +// (C) Copyright David Abrahams, Jeremy Siek, Daryle Walker 1999-2001. +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/utility/operators.htm for documentation. + +// Revision History +// 22 Feb 16 Preserve old work-arounds. (Daniel Frey) +// 16 Dec 10 Limit warning suppression for 4284 to older versions of VC++ +// (Matthew Bradbury, fixes #4432) +// 07 Aug 08 Added "euclidean" spelling. (Daniel Frey) +// 03 Apr 08 Make sure "convertible to bool" is sufficient +// for T::operator<, etc. (Daniel Frey) +// 24 May 07 Changed empty_base to depend on T, see +// http://svn.boost.org/trac/boost/ticket/979 +// 21 Oct 02 Modified implementation of operators to allow compilers with a +// correct named return value optimization (NRVO) to produce optimal +// code. (Daniel Frey) +// 02 Dec 01 Bug fixed in random_access_iteratable. (Helmut Zeisel) +// 28 Sep 01 Factored out iterator operator groups. (Daryle Walker) +// 27 Aug 01 'left' form for non commutative operators added; +// additional classes for groups of related operators added; +// workaround for empty base class optimization +// bug of GCC 3.0 (Helmut Zeisel) +// 25 Jun 01 output_iterator_helper changes: removed default template +// parameters, added support for self-proxying, additional +// documentation and tests (Aleksey Gurtovoy) +// 29 May 01 Added operator classes for << and >>. Added input and output +// iterator helper classes. Added classes to connect equality and +// relational operators. Added classes for groups of related +// operators. Reimplemented example operator and iterator helper +// classes in terms of the new groups. (Daryle Walker, with help +// from Alexy Gurtovoy) +// 11 Feb 01 Fixed bugs in the iterator helpers which prevented explicitly +// supplied arguments from actually being used (Dave Abrahams) +// 04 Jul 00 Fixed NO_OPERATORS_IN_NAMESPACE bugs, major cleanup and +// refactoring of compiler workarounds, additional documentation +// (Alexy Gurtovoy and Mark Rodgers with some help and prompting from +// Dave Abrahams) +// 28 Jun 00 General cleanup and integration of bugfixes from Mark Rodgers and +// Jeremy Siek (Dave Abrahams) +// 20 Jun 00 Changes to accommodate Borland C++Builder 4 and Borland C++ 5.5 +// (Mark Rodgers) +// 20 Jun 00 Minor fixes to the prior revision (Aleksey Gurtovoy) +// 10 Jun 00 Support for the base class chaining technique was added +// (Aleksey Gurtovoy). See documentation and the comments below +// for the details. +// 12 Dec 99 Initial version with iterator operators (Jeremy Siek) +// 18 Nov 99 Change name "divideable" to "dividable", remove unnecessary +// specializations of dividable, subtractable, modable (Ed Brey) +// 17 Nov 99 Add comments (Beman Dawes) +// Remove unnecessary specialization of operators<> (Ed Brey) +// 15 Nov 99 Fix less_than_comparable second operand type for first two +// operators.(Beman Dawes) +// 12 Nov 99 Add operators templates (Ed Brey) +// 11 Nov 99 Add single template parameter version for compilers without +// partial specialization (Beman Dawes) +// 10 Nov 99 Initial version + +// 10 Jun 00: +// An additional optional template parameter was added to most of +// operator templates to support the base class chaining technique (see +// documentation for the details). Unfortunately, a straightforward +// implementation of this change would have broken compatibility with the +// previous version of the library by making it impossible to use the same +// template name (e.g. 'addable') for both the 1- and 2-argument versions of +// an operator template. This implementation solves the backward-compatibility +// issue at the cost of some simplicity. +// +// One of the complications is an existence of special auxiliary class template +// 'is_chained_base<>' (see 'detail' namespace below), which is used +// to determine whether its template parameter is a library's operator template +// or not. You have to specialize 'is_chained_base<>' for each new +// operator template you add to the library. +// +// However, most of the non-trivial implementation details are hidden behind +// several local macros defined below, and as soon as you understand them, +// you understand the whole library implementation. + +#ifndef BOOST_OPERATORS_V1_HPP +#define BOOST_OPERATORS_V1_HPP + +#include +#include + +#include +#include + +#if defined(__sgi) && !defined(__GNUC__) +# pragma set woff 1234 +#endif + +#if BOOST_WORKAROUND(BOOST_MSVC, < 1600) +# pragma warning( disable : 4284 ) // complaint about return type of +#endif // operator-> not begin a UDT + +namespace boost { +namespace detail { + +template class empty_base {}; + +} // namespace detail +} // namespace boost + +// In this section we supply the xxxx1 and xxxx2 forms of the operator +// templates, which are explicitly targeted at the 1-type-argument and +// 2-type-argument operator forms, respectively. Some compilers get confused +// when inline friend functions are overloaded in namespaces other than the +// global namespace. When BOOST_NO_OPERATORS_IN_NAMESPACE is defined, all of +// these templates must go in the global namespace. + +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE +namespace boost +{ +#endif + +// Basic operator classes (contributed by Dave Abrahams) ------------------// + +// Note that friend functions defined in a class are implicitly inline. +// See the C++ std, 11.4 [class.friend] paragraph 5 + +template > +struct less_than_comparable2 : B +{ + friend bool operator<=(const T& x, const U& y) { return !static_cast(x > y); } + friend bool operator>=(const T& x, const U& y) { return !static_cast(x < y); } + friend bool operator>(const U& x, const T& y) { return y < x; } + friend bool operator<(const U& x, const T& y) { return y > x; } + friend bool operator<=(const U& x, const T& y) { return !static_cast(y < x); } + friend bool operator>=(const U& x, const T& y) { return !static_cast(y > x); } +}; + +template > +struct less_than_comparable1 : B +{ + friend bool operator>(const T& x, const T& y) { return y < x; } + friend bool operator<=(const T& x, const T& y) { return !static_cast(y < x); } + friend bool operator>=(const T& x, const T& y) { return !static_cast(x < y); } +}; + +template > +struct equality_comparable2 : B +{ + friend bool operator==(const U& y, const T& x) { return x == y; } + friend bool operator!=(const U& y, const T& x) { return !static_cast(x == y); } + friend bool operator!=(const T& y, const U& x) { return !static_cast(y == x); } +}; + +template > +struct equality_comparable1 : B +{ + friend bool operator!=(const T& x, const T& y) { return !static_cast(x == y); } +}; + +// A macro which produces "name_2left" from "name". +#define BOOST_OPERATOR2_LEFT(name) name##2##_##left + +// NRVO-friendly implementation (contributed by Daniel Frey) ---------------// + +#if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) + +// This is the optimal implementation for ISO/ANSI C++, +// but it requires the compiler to implement the NRVO. +// If the compiler has no NRVO, this is the best symmetric +// implementation available. + +#define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { T nrv( rhs ); nrv OP##= lhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; + +#define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct BOOST_OPERATOR2_LEFT(NAME) : B \ +{ \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; + +#else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) + +// For compilers without NRVO the following code is optimal, but not +// symmetric! Note that the implementation of +// BOOST_OPERATOR2_LEFT(NAME) only looks cool, but doesn't provide +// optimization opportunities to the compiler :) + +#define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ + friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ +}; + +#define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ +}; \ + \ +template > \ +struct BOOST_OPERATOR2_LEFT(NAME) : B \ +{ \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { return T( lhs ) OP##= rhs; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ +}; + +#endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) + +BOOST_BINARY_OPERATOR_COMMUTATIVE( multipliable, * ) +BOOST_BINARY_OPERATOR_COMMUTATIVE( addable, + ) +BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( subtractable, - ) +BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( dividable, / ) +BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( modable, % ) +BOOST_BINARY_OPERATOR_COMMUTATIVE( xorable, ^ ) +BOOST_BINARY_OPERATOR_COMMUTATIVE( andable, & ) +BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | ) + +#undef BOOST_BINARY_OPERATOR_COMMUTATIVE +#undef BOOST_BINARY_OPERATOR_NON_COMMUTATIVE +#undef BOOST_OPERATOR2_LEFT + +// incrementable and decrementable contributed by Jeremy Siek + +template > +struct incrementable : B +{ + friend T operator++(T& x, int) + { + incrementable_type nrv(x); + ++x; + return nrv; + } +private: // The use of this typedef works around a Borland bug + typedef T incrementable_type; +}; + +template > +struct decrementable : B +{ + friend T operator--(T& x, int) + { + decrementable_type nrv(x); + --x; + return nrv; + } +private: // The use of this typedef works around a Borland bug + typedef T decrementable_type; +}; + +// Iterator operator classes (contributed by Jeremy Siek) ------------------// + +template > +struct dereferenceable : B +{ + P operator->() const + { + return &*static_cast(*this); + } +}; + +template > +struct indexable : B +{ + R operator[](I n) const + { + return *(static_cast(*this) + n); + } +}; + +// More operator classes (contributed by Daryle Walker) --------------------// +// (NRVO-friendly implementation contributed by Daniel Frey) ---------------// + +#if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) + +#define BOOST_BINARY_OPERATOR( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; + +#else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) + +#define BOOST_BINARY_OPERATOR( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ +}; + +#endif // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) + +BOOST_BINARY_OPERATOR( left_shiftable, << ) +BOOST_BINARY_OPERATOR( right_shiftable, >> ) + +#undef BOOST_BINARY_OPERATOR + +template > +struct equivalent2 : B +{ + friend bool operator==(const T& x, const U& y) + { + return !static_cast(x < y) && !static_cast(x > y); + } +}; + +template > +struct equivalent1 : B +{ + friend bool operator==(const T&x, const T&y) + { + return !static_cast(x < y) && !static_cast(y < x); + } +}; + +template > +struct partially_ordered2 : B +{ + friend bool operator<=(const T& x, const U& y) + { return static_cast(x < y) || static_cast(x == y); } + friend bool operator>=(const T& x, const U& y) + { return static_cast(x > y) || static_cast(x == y); } + friend bool operator>(const U& x, const T& y) + { return y < x; } + friend bool operator<(const U& x, const T& y) + { return y > x; } + friend bool operator<=(const U& x, const T& y) + { return static_cast(y > x) || static_cast(y == x); } + friend bool operator>=(const U& x, const T& y) + { return static_cast(y < x) || static_cast(y == x); } +}; + +template > +struct partially_ordered1 : B +{ + friend bool operator>(const T& x, const T& y) + { return y < x; } + friend bool operator<=(const T& x, const T& y) + { return static_cast(x < y) || static_cast(x == y); } + friend bool operator>=(const T& x, const T& y) + { return static_cast(y < x) || static_cast(x == y); } +}; + +// Combined operator classes (contributed by Daryle Walker) ----------------// + +template > +struct totally_ordered2 + : less_than_comparable2 > {}; + +template > +struct totally_ordered1 + : less_than_comparable1 > {}; + +template > +struct additive2 + : addable2 > {}; + +template > +struct additive1 + : addable1 > {}; + +template > +struct multiplicative2 + : multipliable2 > {}; + +template > +struct multiplicative1 + : multipliable1 > {}; + +template > +struct integer_multiplicative2 + : multiplicative2 > {}; + +template > +struct integer_multiplicative1 + : multiplicative1 > {}; + +template > +struct arithmetic2 + : additive2 > {}; + +template > +struct arithmetic1 + : additive1 > {}; + +template > +struct integer_arithmetic2 + : additive2 > {}; + +template > +struct integer_arithmetic1 + : additive1 > {}; + +template > +struct bitwise2 + : xorable2 > > {}; + +template > +struct bitwise1 + : xorable1 > > {}; + +template > +struct unit_steppable + : incrementable > {}; + +template > +struct shiftable2 + : left_shiftable2 > {}; + +template > +struct shiftable1 + : left_shiftable1 > {}; + +template > +struct ring_operators2 + : additive2 > > {}; + +template > +struct ring_operators1 + : additive1 > {}; + +template > +struct ordered_ring_operators2 + : ring_operators2 > {}; + +template > +struct ordered_ring_operators1 + : ring_operators1 > {}; + +template > +struct field_operators2 + : ring_operators2 > > {}; + +template > +struct field_operators1 + : ring_operators1 > {}; + +template > +struct ordered_field_operators2 + : field_operators2 > {}; + +template > +struct ordered_field_operators1 + : field_operators1 > {}; + +template > +struct euclidian_ring_operators2 + : ring_operators2 > > > > {}; + +template > +struct euclidian_ring_operators1 + : ring_operators1 > > {}; + +template > +struct ordered_euclidian_ring_operators2 + : totally_ordered2 > {}; + +template > +struct ordered_euclidian_ring_operators1 + : totally_ordered1 > {}; + +template > +struct euclidean_ring_operators2 + : ring_operators2 > > > > {}; + +template > +struct euclidean_ring_operators1 + : ring_operators1 > > {}; + +template > +struct ordered_euclidean_ring_operators2 + : totally_ordered2 > {}; + +template > +struct ordered_euclidean_ring_operators1 + : totally_ordered1 > {}; + +template > +struct input_iteratable + : equality_comparable1 > > {}; + +template > +struct output_iteratable + : incrementable {}; + +template > +struct forward_iteratable + : input_iteratable {}; + +template > +struct bidirectional_iteratable + : forward_iteratable > {}; + +// To avoid repeated derivation from equality_comparable, +// which is an indirect base class of bidirectional_iterable, +// random_access_iteratable must not be derived from totally_ordered1 +// but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001) +template > +struct random_access_iteratable + : bidirectional_iteratable > > > {}; + +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE +} // namespace boost +#endif // BOOST_NO_OPERATORS_IN_NAMESPACE + + +// BOOST_IMPORT_TEMPLATE1 .. BOOST_IMPORT_TEMPLATE4 - +// +// When BOOST_NO_OPERATORS_IN_NAMESPACE is defined we need a way to import an +// operator template into the boost namespace. BOOST_IMPORT_TEMPLATE1 is used +// for one-argument forms of operator templates; BOOST_IMPORT_TEMPLATE2 for +// two-argument forms. Note that these macros expect to be invoked from within +// boost. + +#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE + + // The template is already in boost so we have nothing to do. +# define BOOST_IMPORT_TEMPLATE4(template_name) +# define BOOST_IMPORT_TEMPLATE3(template_name) +# define BOOST_IMPORT_TEMPLATE2(template_name) +# define BOOST_IMPORT_TEMPLATE1(template_name) + +#else // BOOST_NO_OPERATORS_IN_NAMESPACE + +# ifndef BOOST_NO_USING_TEMPLATE + + // Bring the names in with a using-declaration + // to avoid stressing the compiler. +# define BOOST_IMPORT_TEMPLATE4(template_name) using ::template_name; +# define BOOST_IMPORT_TEMPLATE3(template_name) using ::template_name; +# define BOOST_IMPORT_TEMPLATE2(template_name) using ::template_name; +# define BOOST_IMPORT_TEMPLATE1(template_name) using ::template_name; + +# else + + // Otherwise, because a Borland C++ 5.5 bug prevents a using declaration + // from working, we are forced to use inheritance for that compiler. +# define BOOST_IMPORT_TEMPLATE4(template_name) \ + template > \ + struct template_name : ::template_name {}; + +# define BOOST_IMPORT_TEMPLATE3(template_name) \ + template > \ + struct template_name : ::template_name {}; + +# define BOOST_IMPORT_TEMPLATE2(template_name) \ + template > \ + struct template_name : ::template_name {}; + +# define BOOST_IMPORT_TEMPLATE1(template_name) \ + template > \ + struct template_name : ::template_name {}; + +# endif // BOOST_NO_USING_TEMPLATE + +#endif // BOOST_NO_OPERATORS_IN_NAMESPACE + +// +// Here's where we put it all together, defining the xxxx forms of the templates +// in namespace boost. We also define specializations of is_chained_base<> for +// the xxxx, xxxx1, and xxxx2 templates, importing them into boost:: as +// necessary. +// + +// is_chained_base<> - a traits class used to distinguish whether an operator +// template argument is being used for base class chaining, or is specifying a +// 2nd argument type. + +namespace boost { +// A type parameter is used instead of a plain bool because Borland's compiler +// didn't cope well with the more obvious non-type template parameter. +namespace detail { + struct true_t {}; + struct false_t {}; +} // namespace detail + +// Unspecialized version assumes that most types are not being used for base +// class chaining. We specialize for the operator templates defined in this +// library. +template struct is_chained_base { + typedef ::boost::detail::false_t value; +}; + +} // namespace boost + +// Import a 4-type-argument operator template into boost (if necessary) and +// provide a specialization of 'is_chained_base<>' for it. +# define BOOST_OPERATOR_TEMPLATE4(template_name4) \ + BOOST_IMPORT_TEMPLATE4(template_name4) \ + template \ + struct is_chained_base< ::boost::template_name4 > { \ + typedef ::boost::detail::true_t value; \ + }; + +// Import a 3-type-argument operator template into boost (if necessary) and +// provide a specialization of 'is_chained_base<>' for it. +# define BOOST_OPERATOR_TEMPLATE3(template_name3) \ + BOOST_IMPORT_TEMPLATE3(template_name3) \ + template \ + struct is_chained_base< ::boost::template_name3 > { \ + typedef ::boost::detail::true_t value; \ + }; + +// Import a 2-type-argument operator template into boost (if necessary) and +// provide a specialization of 'is_chained_base<>' for it. +# define BOOST_OPERATOR_TEMPLATE2(template_name2) \ + BOOST_IMPORT_TEMPLATE2(template_name2) \ + template \ + struct is_chained_base< ::boost::template_name2 > { \ + typedef ::boost::detail::true_t value; \ + }; + +// Import a 1-type-argument operator template into boost (if necessary) and +// provide a specialization of 'is_chained_base<>' for it. +# define BOOST_OPERATOR_TEMPLATE1(template_name1) \ + BOOST_IMPORT_TEMPLATE1(template_name1) \ + template \ + struct is_chained_base< ::boost::template_name1 > { \ + typedef ::boost::detail::true_t value; \ + }; + +// BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it +// can be used for specifying both 1-argument and 2-argument forms. Requires the +// existence of two previously defined class templates named '1' +// and '2' which must implement the corresponding 1- and 2- +// argument forms. +// +// The template type parameter O == is_chained_base::value is used to +// distinguish whether the 2nd argument to is being used for +// base class chaining from another boost operator template or is describing a +// 2nd operand type. O == true_t only when U is actually an another operator +// template from the library. Partial specialization is used to select an +// implementation in terms of either '1' or '2'. +// + +# define BOOST_OPERATOR_TEMPLATE(template_name) \ +template \ + ,class O = typename is_chained_base::value \ + > \ +struct template_name : template_name##2 {}; \ + \ +template \ +struct template_name \ + : template_name##1 {}; \ + \ +template \ +struct template_name \ + : template_name##1 {}; \ + \ +template \ +struct is_chained_base< ::boost::template_name > { \ + typedef ::boost::detail::true_t value; \ +}; \ + \ +BOOST_OPERATOR_TEMPLATE2(template_name##2) \ +BOOST_OPERATOR_TEMPLATE1(template_name##1) + + + +namespace boost { + +BOOST_OPERATOR_TEMPLATE(less_than_comparable) +BOOST_OPERATOR_TEMPLATE(equality_comparable) +BOOST_OPERATOR_TEMPLATE(multipliable) +BOOST_OPERATOR_TEMPLATE(addable) +BOOST_OPERATOR_TEMPLATE(subtractable) +BOOST_OPERATOR_TEMPLATE2(subtractable2_left) +BOOST_OPERATOR_TEMPLATE(dividable) +BOOST_OPERATOR_TEMPLATE2(dividable2_left) +BOOST_OPERATOR_TEMPLATE(modable) +BOOST_OPERATOR_TEMPLATE2(modable2_left) +BOOST_OPERATOR_TEMPLATE(xorable) +BOOST_OPERATOR_TEMPLATE(andable) +BOOST_OPERATOR_TEMPLATE(orable) + +BOOST_OPERATOR_TEMPLATE1(incrementable) +BOOST_OPERATOR_TEMPLATE1(decrementable) + +BOOST_OPERATOR_TEMPLATE2(dereferenceable) +BOOST_OPERATOR_TEMPLATE3(indexable) + +BOOST_OPERATOR_TEMPLATE(left_shiftable) +BOOST_OPERATOR_TEMPLATE(right_shiftable) +BOOST_OPERATOR_TEMPLATE(equivalent) +BOOST_OPERATOR_TEMPLATE(partially_ordered) + +BOOST_OPERATOR_TEMPLATE(totally_ordered) +BOOST_OPERATOR_TEMPLATE(additive) +BOOST_OPERATOR_TEMPLATE(multiplicative) +BOOST_OPERATOR_TEMPLATE(integer_multiplicative) +BOOST_OPERATOR_TEMPLATE(arithmetic) +BOOST_OPERATOR_TEMPLATE(integer_arithmetic) +BOOST_OPERATOR_TEMPLATE(bitwise) +BOOST_OPERATOR_TEMPLATE1(unit_steppable) +BOOST_OPERATOR_TEMPLATE(shiftable) +BOOST_OPERATOR_TEMPLATE(ring_operators) +BOOST_OPERATOR_TEMPLATE(ordered_ring_operators) +BOOST_OPERATOR_TEMPLATE(field_operators) +BOOST_OPERATOR_TEMPLATE(ordered_field_operators) +BOOST_OPERATOR_TEMPLATE(euclidian_ring_operators) +BOOST_OPERATOR_TEMPLATE(ordered_euclidian_ring_operators) +BOOST_OPERATOR_TEMPLATE(euclidean_ring_operators) +BOOST_OPERATOR_TEMPLATE(ordered_euclidean_ring_operators) +BOOST_OPERATOR_TEMPLATE2(input_iteratable) +BOOST_OPERATOR_TEMPLATE1(output_iteratable) +BOOST_OPERATOR_TEMPLATE2(forward_iteratable) +BOOST_OPERATOR_TEMPLATE2(bidirectional_iteratable) +BOOST_OPERATOR_TEMPLATE4(random_access_iteratable) + +#undef BOOST_OPERATOR_TEMPLATE +#undef BOOST_OPERATOR_TEMPLATE4 +#undef BOOST_OPERATOR_TEMPLATE3 +#undef BOOST_OPERATOR_TEMPLATE2 +#undef BOOST_OPERATOR_TEMPLATE1 +#undef BOOST_IMPORT_TEMPLATE1 +#undef BOOST_IMPORT_TEMPLATE2 +#undef BOOST_IMPORT_TEMPLATE3 +#undef BOOST_IMPORT_TEMPLATE4 + +// The following 'operators' classes can only be used portably if the derived class +// declares ALL of the required member operators. +template +struct operators2 + : totally_ordered2 > > {}; + +template +struct operators : operators2 {}; + +template struct operators + : totally_ordered > > > {}; + +// Iterator helper classes (contributed by Jeremy Siek) -------------------// +// (Input and output iterator helpers contributed by Daryle Walker) -------// +// (Changed to use combined operator classes by Daryle Walker) ------------// +template +struct input_iterator_helper + : input_iteratable > {}; + +template +struct output_iterator_helper + : output_iteratable > +{ + T& operator*() { return static_cast(*this); } + T& operator++() { return static_cast(*this); } +}; + +template +struct forward_iterator_helper + : forward_iteratable > {}; + +template +struct bidirectional_iterator_helper + : bidirectional_iteratable > {}; + +template +struct random_access_iterator_helper + : random_access_iteratable > +{ + friend D requires_difference_operator(const T& x, const T& y) { + return x - y; + } +}; // random_access_iterator_helper + +} // namespace boost + +#if defined(__sgi) && !defined(__GNUC__) +#pragma reset woff 1234 +#endif + +#endif // BOOST_OPERATORS_V1_HPP From a25ac4550b1c855901b5c5210c03f46edc12d8f4 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Mon, 22 Feb 2016 20:46:58 +0100 Subject: [PATCH 03/12] Removed unused overloads --- operators_test.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/operators_test.cpp b/operators_test.cpp index 5b71f3a..f445945 100644 --- a/operators_test.cpp +++ b/operators_test.cpp @@ -38,11 +38,9 @@ namespace int true_value(int x) { return x; } long true_value(long x) { return x; } signed char true_value(signed char x) { return x; } - short true_value(short x) { return x; } unsigned int true_value(unsigned int x) { return x; } unsigned long true_value(unsigned long x) { return x; } unsigned char true_value(unsigned char x) { return x; } - unsigned short true_value(unsigned short x) { return x; } // verify the minimum requirements for some operators class convertible_to_bool From 1dfacff7ec008bddfe55e11ece50ad21a24583eb Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Tue, 23 Feb 2016 20:30:16 +0100 Subject: [PATCH 04/12] Renamed namespace detail to operators_detail --- include/boost/operators.hpp | 282 ++++++++++++++++++------------------ 1 file changed, 141 insertions(+), 141 deletions(-) diff --git a/include/boost/operators.hpp b/include/boost/operators.hpp index d8a1038..3fc08d5 100644 --- a/include/boost/operators.hpp +++ b/include/boost/operators.hpp @@ -73,7 +73,7 @@ // issue at the cost of some simplicity. // // One of the complications is an existence of special auxiliary class template -// 'is_chained_base<>' (see 'detail' namespace below), which is used +// 'is_chained_base<>' (see 'operators_detail' namespace below), which is used // to determine whether its template parameter is a library's operator template // or not. You have to specialize 'is_chained_base<>' for each new // operator template you add to the library. @@ -113,19 +113,19 @@ namespace boost { namespace operators_impl { -namespace detail +namespace operators_detail { template class empty_base {}; -} // namespace detail +} // namespace operators_detail // Basic operator classes (contributed by Dave Abrahams) ------------------// // Note that friend functions defined in a class are implicitly inline. // See the C++ std, 11.4 [class.friend] paragraph 5 -template > +template > struct less_than_comparable2 : B { friend bool operator<=(const T& x, const U& y) { return !static_cast(x > y); } @@ -136,7 +136,7 @@ struct less_than_comparable2 : B friend bool operator>=(const U& x, const T& y) { return !static_cast(y > x); } }; -template > +template > struct less_than_comparable1 : B { friend bool operator>(const T& x, const T& y) { return y < x; } @@ -144,7 +144,7 @@ struct less_than_comparable1 : B friend bool operator>=(const T& x, const T& y) { return !static_cast(x < y); } }; -template > +template > struct equality_comparable2 : B { friend bool operator==(const U& y, const T& x) { return x == y; } @@ -152,7 +152,7 @@ struct equality_comparable2 : B friend bool operator!=(const T& y, const U& x) { return !static_cast(y == x); } }; -template > +template > struct equality_comparable1 : B { friend bool operator!=(const T& x, const T& y) { return !static_cast(x == y); } @@ -170,43 +170,43 @@ struct equality_comparable1 : B // If the compiler has no NRVO, this is the best symmetric // implementation available. -#define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( const T& lhs, const U& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ - friend T operator OP( const U& lhs, const T& rhs ) \ - { T nrv( rhs ); nrv OP##= lhs; return nrv; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( const T& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +#define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { T nrv( rhs ); nrv OP##= lhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ }; -#define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( const T& lhs, const U& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ -}; \ - \ -template > \ -struct BOOST_OPERATOR2_LEFT(NAME) : B \ -{ \ - friend T operator OP( const U& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( const T& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +#define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct BOOST_OPERATOR2_LEFT(NAME) : B \ +{ \ + friend T operator OP( const U& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ }; #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) @@ -217,34 +217,34 @@ struct NAME##1 : B \ // optimization opportunities to the compiler :) #define BOOST_BINARY_OPERATOR_COMMUTATIVE( NAME, OP ) \ -template > \ +template > \ struct NAME##2 : B \ { \ friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ friend T operator OP( const U& lhs, T rhs ) { return rhs OP##= lhs; } \ }; \ \ -template > \ +template > \ struct NAME##1 : B \ { \ friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ }; #define BOOST_BINARY_OPERATOR_NON_COMMUTATIVE( NAME, OP ) \ -template > \ +template > \ struct NAME##2 : B \ { \ friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ }; \ \ -template > \ +template > \ struct BOOST_OPERATOR2_LEFT(NAME) : B \ { \ friend T operator OP( const U& lhs, const T& rhs ) \ { return T( lhs ) OP##= rhs; } \ }; \ \ -template > \ +template > \ struct NAME##1 : B \ { \ friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ @@ -267,7 +267,7 @@ BOOST_BINARY_OPERATOR_COMMUTATIVE( orable, | ) // incrementable and decrementable contributed by Jeremy Siek -template > +template > struct incrementable : B { friend T operator++(T& x, int) @@ -280,7 +280,7 @@ private: // The use of this typedef works around a Borland bug typedef T incrementable_type; }; -template > +template > struct decrementable : B { friend T operator--(T& x, int) @@ -295,7 +295,7 @@ private: // The use of this typedef works around a Borland bug // Iterator operator classes (contributed by Jeremy Siek) ------------------// -template > +template > struct dereferenceable : B { P operator->() const @@ -304,7 +304,7 @@ struct dereferenceable : B } }; -template > +template > struct indexable : B { R operator[](I n) const @@ -318,31 +318,31 @@ struct indexable : B #if defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) -#define BOOST_BINARY_OPERATOR( NAME, OP ) \ -template > \ -struct NAME##2 : B \ -{ \ - friend T operator OP( const T& lhs, const U& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ -}; \ - \ -template > \ -struct NAME##1 : B \ -{ \ - friend T operator OP( const T& lhs, const T& rhs ) \ - { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +#define BOOST_BINARY_OPERATOR( NAME, OP ) \ +template > \ +struct NAME##2 : B \ +{ \ + friend T operator OP( const T& lhs, const U& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ +}; \ + \ +template > \ +struct NAME##1 : B \ +{ \ + friend T operator OP( const T& lhs, const T& rhs ) \ + { T nrv( lhs ); nrv OP##= rhs; return nrv; } \ }; #else // defined(BOOST_HAS_NRVO) || defined(BOOST_FORCE_SYMMETRIC_OPERATORS) #define BOOST_BINARY_OPERATOR( NAME, OP ) \ -template > \ +template > \ struct NAME##2 : B \ { \ friend T operator OP( T lhs, const U& rhs ) { return lhs OP##= rhs; } \ }; \ \ -template > \ +template > \ struct NAME##1 : B \ { \ friend T operator OP( T lhs, const T& rhs ) { return lhs OP##= rhs; } \ @@ -355,7 +355,7 @@ BOOST_BINARY_OPERATOR( right_shiftable, >> ) #undef BOOST_BINARY_OPERATOR -template > +template > struct equivalent2 : B { friend bool operator==(const T& x, const U& y) @@ -364,7 +364,7 @@ struct equivalent2 : B } }; -template > +template > struct equivalent1 : B { friend bool operator==(const T&x, const T&y) @@ -373,7 +373,7 @@ struct equivalent1 : B } }; -template > +template > struct partially_ordered2 : B { friend bool operator<=(const T& x, const U& y) @@ -390,7 +390,7 @@ struct partially_ordered2 : B { return static_cast(y < x) || static_cast(y == x); } }; -template > +template > struct partially_ordered1 : B { friend bool operator>(const T& x, const T& y) @@ -403,161 +403,161 @@ struct partially_ordered1 : B // Combined operator classes (contributed by Daryle Walker) ----------------// -template > +template > struct totally_ordered2 : less_than_comparable2 > {}; -template > +template > struct totally_ordered1 : less_than_comparable1 > {}; -template > +template > struct additive2 : addable2 > {}; -template > +template > struct additive1 : addable1 > {}; -template > +template > struct multiplicative2 : multipliable2 > {}; -template > +template > struct multiplicative1 : multipliable1 > {}; -template > +template > struct integer_multiplicative2 : multiplicative2 > {}; -template > +template > struct integer_multiplicative1 : multiplicative1 > {}; -template > +template > struct arithmetic2 : additive2 > {}; -template > +template > struct arithmetic1 : additive1 > {}; -template > +template > struct integer_arithmetic2 : additive2 > {}; -template > +template > struct integer_arithmetic1 : additive1 > {}; -template > +template > struct bitwise2 : xorable2 > > {}; -template > +template > struct bitwise1 : xorable1 > > {}; -template > +template > struct unit_steppable : incrementable > {}; -template > +template > struct shiftable2 : left_shiftable2 > {}; -template > +template > struct shiftable1 : left_shiftable1 > {}; -template > +template > struct ring_operators2 : additive2 > > {}; -template > +template > struct ring_operators1 : additive1 > {}; -template > +template > struct ordered_ring_operators2 : ring_operators2 > {}; -template > +template > struct ordered_ring_operators1 : ring_operators1 > {}; -template > +template > struct field_operators2 : ring_operators2 > > {}; -template > +template > struct field_operators1 : ring_operators1 > {}; -template > +template > struct ordered_field_operators2 : field_operators2 > {}; -template > +template > struct ordered_field_operators1 : field_operators1 > {}; -template > +template > struct euclidian_ring_operators2 : ring_operators2 > > > > {}; -template > +template > struct euclidian_ring_operators1 : ring_operators1 > > {}; -template > +template > struct ordered_euclidian_ring_operators2 : totally_ordered2 > {}; -template > +template > struct ordered_euclidian_ring_operators1 : totally_ordered1 > {}; -template > +template > struct euclidean_ring_operators2 : ring_operators2 > > > > {}; -template > +template > struct euclidean_ring_operators1 : ring_operators1 > > {}; -template > +template > struct ordered_euclidean_ring_operators2 : totally_ordered2 > {}; -template > +template > struct ordered_euclidean_ring_operators1 : totally_ordered1 > {}; -template > +template > struct input_iteratable : equality_comparable1 > > {}; -template > +template > struct output_iteratable : incrementable {}; -template > +template > struct forward_iteratable : input_iteratable {}; -template > +template > struct bidirectional_iteratable : forward_iteratable > +template > struct random_access_iteratable : bidirectional_iteratable - a traits class used to distinguish whether an operator // template argument is being used for base class chaining, or is specifying a @@ -673,7 +673,7 @@ struct false_t {}; // class chaining. We specialize for the operator templates defined in this // library. template struct is_chained_base { - typedef detail::false_t value; + typedef operators_detail::false_t value; }; // Provide a specialization of 'is_chained_base<>' @@ -681,7 +681,7 @@ template struct is_chained_base { # define BOOST_OPERATOR_TEMPLATE4(template_name4) \ template \ struct is_chained_base< template_name4 > { \ - typedef detail::true_t value; \ + typedef operators_detail::true_t value; \ }; // Provide a specialization of 'is_chained_base<>' @@ -689,7 +689,7 @@ template struct is_chained_base { # define BOOST_OPERATOR_TEMPLATE3(template_name3) \ template \ struct is_chained_base< template_name3 > { \ - typedef detail::true_t value; \ + typedef operators_detail::true_t value; \ }; // Provide a specialization of 'is_chained_base<>' @@ -697,7 +697,7 @@ template struct is_chained_base { # define BOOST_OPERATOR_TEMPLATE2(template_name2) \ template \ struct is_chained_base< template_name2 > { \ - typedef detail::true_t value; \ + typedef operators_detail::true_t value; \ }; // Provide a specialization of 'is_chained_base<>' @@ -705,7 +705,7 @@ template struct is_chained_base { # define BOOST_OPERATOR_TEMPLATE1(template_name1) \ template \ struct is_chained_base< template_name1 > { \ - typedef detail::true_t value; \ + typedef operators_detail::true_t value; \ }; // BOOST_OPERATOR_TEMPLATE(template_name) defines template_name<> such that it @@ -722,32 +722,32 @@ template struct is_chained_base { // implementation in terms of either '1' or '2'. // -# define BOOST_OPERATOR_TEMPLATE(template_name) \ -template \ - ,class O = typename is_chained_base::value \ - > \ -struct template_name; \ - \ -template \ -struct template_name \ - : template_name##2 {}; \ - \ -template \ -struct template_name, detail::true_t> \ - : template_name##1 {}; \ - \ -template \ -struct template_name \ - : template_name##1 {}; \ - \ -template \ -struct is_chained_base< template_name > { \ - typedef detail::true_t value; \ -}; \ - \ -BOOST_OPERATOR_TEMPLATE2(template_name##2) \ +# define BOOST_OPERATOR_TEMPLATE(template_name) \ +template \ + ,class O = typename is_chained_base::value \ + > \ +struct template_name; \ + \ +template \ +struct template_name \ + : template_name##2 {}; \ + \ +template \ +struct template_name, operators_detail::true_t> \ + : template_name##1 {}; \ + \ +template \ +struct template_name \ + : template_name##1 {}; \ + \ +template \ +struct is_chained_base< template_name > { \ + typedef operators_detail::true_t value; \ +}; \ + \ +BOOST_OPERATOR_TEMPLATE2(template_name##2) \ BOOST_OPERATOR_TEMPLATE1(template_name##1) BOOST_OPERATOR_TEMPLATE(less_than_comparable) From 4313bfc323d14a18ec95aada288accad0c4f4df4 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 7 Mar 2016 08:45:25 -0800 Subject: [PATCH 05/12] Revert "Remove the 'basic_string_ref template; use 'basic_string_view' instead. Keep the string_ref, etc typedefs around, though" This reverts commit 8ab8e36dcfaa88837e3cf0bccaf01c458d3c1fb2. --- include/boost/utility/string_ref.hpp | 516 +++++++++++++++++++++- include/boost/utility/string_ref_fwd.hpp | 12 +- include/boost/utility/string_view_fwd.hpp | 2 +- test/Jamfile.v2 | 3 + test/string_ref_test1.cpp | 109 +++++ test/string_ref_test2.cpp | 318 +++++++++++++ test/string_ref_test_io.cpp | 179 ++++++++ 7 files changed, 1129 insertions(+), 10 deletions(-) create mode 100644 test/string_ref_test1.cpp create mode 100644 test/string_ref_test2.cpp create mode 100644 test/string_ref_test_io.cpp diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index df5dcbe..8707157 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -18,9 +18,519 @@ #include #include #include -#include +#include -// The primary template has been changed to basic_string_view -// The convenience typedefs string_ref still exist. +#include +#include +#include +#include +#include +#include + +namespace boost { + + namespace detail { + // A helper functor because sometimes we don't have lambdas + template + class string_ref_traits_eq { + public: + string_ref_traits_eq ( charT ch ) : ch_(ch) {} + bool operator () ( charT val ) const { return traits::eq ( ch_, val ); } + charT ch_; + }; + } + + template + class basic_string_ref { + public: + // types + typedef charT value_type; + typedef const charT* pointer; + typedef const charT& reference; + typedef const charT& const_reference; + typedef pointer const_iterator; // impl-defined + typedef const_iterator iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef const_reverse_iterator reverse_iterator; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1); + + // construct/copy + BOOST_CONSTEXPR basic_string_ref () + : ptr_(NULL), len_(0) {} + + BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) + : ptr_(rhs.ptr_), len_(rhs.len_) {} + + basic_string_ref& operator=(const basic_string_ref &rhs) { + ptr_ = rhs.ptr_; + len_ = rhs.len_; + return *this; + } + + basic_string_ref(const charT* str) + : ptr_(str), len_(traits::length(str)) {} + + template + basic_string_ref(const std::basic_string& str) + : ptr_(str.data()), len_(str.length()) {} + + BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) + : ptr_(str), len_(len) {} + +#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS + template + explicit operator std::basic_string() const { + return std::basic_string ( begin(), end()); + } +#endif + + std::basic_string to_string () const { + return std::basic_string ( begin(), end()); + } + + // iterators + BOOST_CONSTEXPR const_iterator begin() const { return ptr_; } + BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; } + BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; } + BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; } + const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); } + const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); } + const_reverse_iterator rend() const { return const_reverse_iterator (begin()); } + const_reverse_iterator crend() const { return const_reverse_iterator (begin()); } + + // capacity + BOOST_CONSTEXPR size_type size() const { return len_; } + BOOST_CONSTEXPR size_type length() const { return len_; } + BOOST_CONSTEXPR size_type max_size() const { return len_; } + BOOST_CONSTEXPR bool empty() const { return len_ == 0; } + + // element access + BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; } + + const charT& at(size_t pos) const { + if ( pos >= len_ ) + BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) ); + return ptr_[pos]; + } + + BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; } + BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; } + BOOST_CONSTEXPR const charT* data() const { return ptr_; } + + // modifiers + void clear() { len_ = 0; } + void remove_prefix(size_type n) { + if ( n > len_ ) + n = len_; + ptr_ += n; + len_ -= n; + } + + void remove_suffix(size_type n) { + if ( n > len_ ) + n = len_; + len_ -= n; + } + + + // basic_string_ref string operations + basic_string_ref substr(size_type pos, size_type n=npos) const { + if ( pos > size()) + BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); + if ( n == npos || pos + n > size()) + n = size () - pos; + return basic_string_ref ( data() + pos, n ); + } + + int compare(basic_string_ref x) const { + const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_)); + return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 ); + } + + bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); } + bool starts_with(basic_string_ref x) const { + return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0; + } + + bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); } + bool ends_with(basic_string_ref x) const { + return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0; + } + + size_type find(basic_string_ref s) const { + const_iterator iter = std::search ( this->cbegin (), this->cend (), + s.cbegin (), s.cend (), traits::eq ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find(charT c) const { + const_iterator iter = std::find_if ( this->cbegin (), this->cend (), + detail::string_ref_traits_eq ( c )); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type rfind(basic_string_ref s) const { + const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (), + s.crbegin (), s.crend (), traits::eq ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type rfind(charT c) const { + const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (), + detail::string_ref_traits_eq ( c )); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type find_first_of(charT c) const { return find (c); } + size_type find_last_of (charT c) const { return rfind (c); } + + size_type find_first_of(basic_string_ref s) const { + const_iterator iter = std::find_first_of + ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find_last_of(basic_string_ref s) const { + const_reverse_iterator iter = std::find_first_of + ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); + } + + size_type find_first_not_of(basic_string_ref s) const { + const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find_first_not_of(charT c) const { + for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter ) + if ( !traits::eq ( c, *iter )) + return std::distance ( this->cbegin (), iter ); + return npos; + } + + size_type find_last_not_of(basic_string_ref s) const { + const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type find_last_not_of(charT c) const { + for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter ) + if ( !traits::eq ( c, *iter )) + return reverse_distance ( this->crbegin (), iter ); + return npos; + } + + private: + template + size_type reverse_distance ( r_iter first, r_iter last ) const { + return len_ - 1 - std::distance ( first, last ); + } + + template + Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const { + for ( ; first != last ; ++first ) + if ( 0 == traits::find ( s.ptr_, s.len_, *first )) + return first; + return last; + } + + + + const charT *ptr_; + std::size_t len_; + }; + + +// Comparison operators +// Equality + template + inline bool operator==(basic_string_ref x, basic_string_ref y) { + if ( x.size () != y.size ()) return false; + return x.compare(y) == 0; + } + + template + inline bool operator==(basic_string_ref x, const std::basic_string & y) { + return x == basic_string_ref(y); + } + + template + inline bool operator==(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) == y; + } + + template + inline bool operator==(basic_string_ref x, const charT * y) { + return x == basic_string_ref(y); + } + + template + inline bool operator==(const charT * x, basic_string_ref y) { + return basic_string_ref(x) == y; + } + +// Inequality + template + inline bool operator!=(basic_string_ref x, basic_string_ref y) { + if ( x.size () != y.size ()) return true; + return x.compare(y) != 0; + } + + template + inline bool operator!=(basic_string_ref x, const std::basic_string & y) { + return x != basic_string_ref(y); + } + + template + inline bool operator!=(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) != y; + } + + template + inline bool operator!=(basic_string_ref x, const charT * y) { + return x != basic_string_ref(y); + } + + template + inline bool operator!=(const charT * x, basic_string_ref y) { + return basic_string_ref(x) != y; + } + +// Less than + template + inline bool operator<(basic_string_ref x, basic_string_ref y) { + return x.compare(y) < 0; + } + + template + inline bool operator<(basic_string_ref x, const std::basic_string & y) { + return x < basic_string_ref(y); + } + + template + inline bool operator<(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) < y; + } + + template + inline bool operator<(basic_string_ref x, const charT * y) { + return x < basic_string_ref(y); + } + + template + inline bool operator<(const charT * x, basic_string_ref y) { + return basic_string_ref(x) < y; + } + +// Greater than + template + inline bool operator>(basic_string_ref x, basic_string_ref y) { + return x.compare(y) > 0; + } + + template + inline bool operator>(basic_string_ref x, const std::basic_string & y) { + return x > basic_string_ref(y); + } + + template + inline bool operator>(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) > y; + } + + template + inline bool operator>(basic_string_ref x, const charT * y) { + return x > basic_string_ref(y); + } + + template + inline bool operator>(const charT * x, basic_string_ref y) { + return basic_string_ref(x) > y; + } + +// Less than or equal to + template + inline bool operator<=(basic_string_ref x, basic_string_ref y) { + return x.compare(y) <= 0; + } + + template + inline bool operator<=(basic_string_ref x, const std::basic_string & y) { + return x <= basic_string_ref(y); + } + + template + inline bool operator<=(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) <= y; + } + + template + inline bool operator<=(basic_string_ref x, const charT * y) { + return x <= basic_string_ref(y); + } + + template + inline bool operator<=(const charT * x, basic_string_ref y) { + return basic_string_ref(x) <= y; + } + +// Greater than or equal to + template + inline bool operator>=(basic_string_ref x, basic_string_ref y) { + return x.compare(y) >= 0; + } + + template + inline bool operator>=(basic_string_ref x, const std::basic_string & y) { + return x >= basic_string_ref(y); + } + + template + inline bool operator>=(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) >= y; + } + + template + inline bool operator>=(basic_string_ref x, const charT * y) { + return x >= basic_string_ref(y); + } + + template + inline bool operator>=(const charT * x, basic_string_ref y) { + return basic_string_ref(x) >= y; + } + + namespace detail { + + template + inline void insert_fill_chars(std::basic_ostream& os, std::size_t n) { + enum { chunk_size = 8 }; + charT fill_chars[chunk_size]; + std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); + for (; n >= chunk_size && os.good(); n -= chunk_size) + os.write(fill_chars, static_cast< std::size_t >(chunk_size)); + if (n > 0 && os.good()) + os.write(fill_chars, n); + } + + template + void insert_aligned(std::basic_ostream& os, const basic_string_ref& str) { + const std::size_t size = str.size(); + const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; + const bool align_left = (os.flags() & std::basic_ostream::adjustfield) == std::basic_ostream::left; + if (!align_left) { + detail::insert_fill_chars(os, alignment_size); + if (os.good()) + os.write(str.data(), size); + } + else { + os.write(str.data(), size); + if (os.good()) + detail::insert_fill_chars(os, alignment_size); + } + } + + } // namespace detail + + // Inserter + template + inline std::basic_ostream& + operator<<(std::basic_ostream& os, const basic_string_ref& str) { + if (os.good()) { + const std::size_t size = str.size(); + const std::size_t w = static_cast< std::size_t >(os.width()); + if (w <= size) + os.write(str.data(), size); + else + detail::insert_aligned(os, str); + os.width(0); + } + return os; + } + +#if 0 + // numeric conversions + // + // These are short-term implementations. + // In a production environment, I would rather avoid the copying. + // + inline int stoi (string_ref str, size_t* idx=0, int base=10) { + return std::stoi ( std::string(str), idx, base ); + } + + inline long stol (string_ref str, size_t* idx=0, int base=10) { + return std::stol ( std::string(str), idx, base ); + } + + inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) { + return std::stoul ( std::string(str), idx, base ); + } + + inline long long stoll (string_ref str, size_t* idx=0, int base=10) { + return std::stoll ( std::string(str), idx, base ); + } + + inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) { + return std::stoull ( std::string(str), idx, base ); + } + + inline float stof (string_ref str, size_t* idx=0) { + return std::stof ( std::string(str), idx ); + } + + inline double stod (string_ref str, size_t* idx=0) { + return std::stod ( std::string(str), idx ); + } + + inline long double stold (string_ref str, size_t* idx=0) { + return std::stold ( std::string(str), idx ); + } + + inline int stoi (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoi ( std::wstring(str), idx, base ); + } + + inline long stol (wstring_ref str, size_t* idx=0, int base=10) { + return std::stol ( std::wstring(str), idx, base ); + } + + inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoul ( std::wstring(str), idx, base ); + } + + inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoll ( std::wstring(str), idx, base ); + } + + inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoull ( std::wstring(str), idx, base ); + } + + inline float stof (wstring_ref str, size_t* idx=0) { + return std::stof ( std::wstring(str), idx ); + } + + inline double stod (wstring_ref str, size_t* idx=0) { + return std::stod ( std::wstring(str), idx ); + } + + inline long double stold (wstring_ref str, size_t* idx=0) { + return std::stold ( std::wstring(str), idx ); + } +#endif + +} + +#if 0 +namespace std { + // Hashing + template<> struct hash; + template<> struct hash; + template<> struct hash; + template<> struct hash; +} +#endif #endif diff --git a/include/boost/utility/string_ref_fwd.hpp b/include/boost/utility/string_ref_fwd.hpp index ead7b13..f58b98c 100644 --- a/include/boost/utility/string_ref_fwd.hpp +++ b/include/boost/utility/string_ref_fwd.hpp @@ -16,20 +16,20 @@ #define BOOST_STRING_REF_FWD_HPP #include -#include -#include // for std::char_traits +#include namespace boost { - typedef basic_string_view > string_ref; - typedef basic_string_view > wstring_ref; + template > class basic_string_ref; + typedef basic_string_ref > string_ref; + typedef basic_string_ref > wstring_ref; #ifndef BOOST_NO_CXX11_CHAR16_T - typedef basic_string_view > u16string_ref; + typedef basic_string_ref > u16string_ref; #endif #ifndef BOOST_NO_CXX11_CHAR32_T - typedef basic_string_view > u32string_ref; + typedef basic_string_ref > u32string_ref; #endif } diff --git a/include/boost/utility/string_view_fwd.hpp b/include/boost/utility/string_view_fwd.hpp index 3764f99..3d2317a 100644 --- a/include/boost/utility/string_view_fwd.hpp +++ b/include/boost/utility/string_view_fwd.hpp @@ -18,7 +18,7 @@ #define BOOST_STRING_VIEW_FWD_HPP #include -#include // for std::char_traits +#include namespace boost { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 6e39ef6..a2d5e4b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,6 +27,9 @@ test-suite utility [ run ../operators_test.cpp ../../test/build//boost_test_exec_monitor/static ] [ compile result_of_test.cpp ] [ run ../shared_iterator_test.cpp ] + [ run string_ref_test1.cpp unit_test_framework ] + [ run string_ref_test2.cpp unit_test_framework ] + [ run string_ref_test_io.cpp unit_test_framework ] [ run string_view_test1.cpp unit_test_framework ] [ run string_view_test2.cpp unit_test_framework ] [ run string_view_test_io.cpp unit_test_framework ] diff --git a/test/string_ref_test1.cpp b/test/string_ref_test1.cpp new file mode 100644 index 0000000..f1d4df2 --- /dev/null +++ b/test/string_ref_test1.cpp @@ -0,0 +1,109 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include + +#define BOOST_TEST_MAIN +#include + +typedef boost::string_ref string_ref; + +// Should be equal +void interop ( const std::string &str, string_ref ref ) { +// BOOST_CHECK ( str == ref ); + BOOST_CHECK ( str.size () == ref.size ()); + BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ())); + BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ())); + } + +void null_tests ( const char *p ) { +// All zero-length string-refs should be equal + string_ref sr1; // NULL, 0 + string_ref sr2 ( NULL, 0 ); + string_ref sr3 ( p, 0 ); + string_ref sr4 ( p ); + sr4.clear (); + + BOOST_CHECK ( sr1 == sr2 ); + BOOST_CHECK ( sr1 == sr3 ); + BOOST_CHECK ( sr2 == sr3 ); + BOOST_CHECK ( sr1 == sr4 ); + } + +// make sure that substrings work just like strings +void test_substr ( const std::string &str ) { + const size_t sz = str.size (); + string_ref ref ( str ); + +// Substrings at the end + for ( size_t i = 0; i <= sz; ++ i ) + interop ( str.substr ( i ), ref.substr ( i )); + +// Substrings at the beginning + for ( size_t i = 0; i <= sz; ++ i ) + interop ( str.substr ( 0, i ), ref.substr ( 0, i )); + +// All possible substrings + for ( size_t i = 0; i < sz; ++i ) + for ( size_t j = i; j < sz; ++j ) + interop ( str.substr ( i, j ), ref.substr ( i, j )); + } + +// make sure that removing prefixes and suffixes work just like strings +void test_remove ( const std::string &str ) { + const size_t sz = str.size (); + std::string work; + string_ref ref; + + for ( size_t i = 1; i <= sz; ++i ) { + work = str; + ref = str; + while ( ref.size () >= i ) { + interop ( work, ref ); + work.erase ( 0, i ); + ref.remove_prefix (i); + } + } + + for ( size_t i = 1; i < sz; ++ i ) { + work = str; + ref = str; + while ( ref.size () >= i ) { + interop ( work, ref ); + work.erase ( work.size () - i, i ); + ref.remove_suffix (i); + } + } + } + +const char *test_strings [] = { + "", + "1", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + "0123456789", + NULL + }; + +BOOST_AUTO_TEST_CASE( test_main ) +{ + const char **p = &test_strings[0]; + + while ( *p != NULL ) { + interop ( *p, *p ); + test_substr ( *p ); + test_remove ( *p ); + null_tests ( *p ); + + p++; + } +} diff --git a/test/string_ref_test2.cpp b/test/string_ref_test2.cpp new file mode 100644 index 0000000..9559c67 --- /dev/null +++ b/test/string_ref_test2.cpp @@ -0,0 +1,318 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include // for std::strchr + +#include + +#define BOOST_TEST_MAIN +#include + +typedef boost::string_ref string_ref; + +void ends_with ( const char *arg ) { + const size_t sz = std::strlen ( arg ); + string_ref sr ( arg ); + string_ref sr2 ( arg ); + const char *p = arg; + + while ( *p ) { + BOOST_CHECK ( sr.ends_with ( p )); + ++p; + } + + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.ends_with ( sr2 )); + sr2.remove_prefix (1); + } + + sr2 = arg; + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.ends_with ( sr2 )); + sr2.remove_prefix (1); + } + + char ch = sz == 0 ? '\0' : arg [ sz - 1 ]; + sr2 = arg; + if ( sz > 0 ) + BOOST_CHECK ( sr2.ends_with ( ch )); + BOOST_CHECK ( !sr2.ends_with ( ++ch )); + BOOST_CHECK ( sr2.ends_with ( string_ref ())); + } + +void starts_with ( const char *arg ) { + const size_t sz = std::strlen ( arg ); + string_ref sr ( arg ); + string_ref sr2 ( arg ); + const char *p = arg + std::strlen ( arg ) - 1; + while ( p >= arg ) { + std::string foo ( arg, p + 1 ); + BOOST_CHECK ( sr.starts_with ( foo )); + --p; + } + + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.starts_with ( sr2 )); + sr2.remove_suffix (1); + } + + char ch = *arg; + sr2 = arg; + if ( sz > 0 ) + BOOST_CHECK ( sr2.starts_with ( ch )); + BOOST_CHECK ( !sr2.starts_with ( ++ch )); + BOOST_CHECK ( sr2.starts_with ( string_ref ())); + } + +void reverse ( const char *arg ) { +// Round trip + string_ref sr1 ( arg ); + std::string string1 ( sr1.rbegin (), sr1.rend ()); + string_ref sr2 ( string1 ); + std::string string2 ( sr2.rbegin (), sr2.rend ()); + + BOOST_CHECK ( std::equal ( sr2.rbegin (), sr2.rend (), arg )); + BOOST_CHECK ( string2 == arg ); + BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ())); + } + +// This helper function eliminates signed vs. unsigned warnings +string_ref::size_type ptr_diff ( const char *res, const char *base ) { + BOOST_CHECK ( res >= base ); + return static_cast ( res - base ); + } + +void find ( const char *arg ) { + string_ref sr1; + string_ref sr2; + const char *p; + +// Look for each character in the string(searching from the start) + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos = sr1.find(*p); + BOOST_CHECK ( pos != string_ref::npos && ( pos <= ptr_diff ( p, arg ))); + ++p; + } + +// Look for each character in the string (searching from the end) + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos = sr1.rfind(*p); + BOOST_CHECK ( pos != string_ref::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg ))); + ++p; + } + +// Look for pairs on characters (searching from the start) + sr1 = arg; + p = arg; + while ( *p && *(p+1)) { + string_ref sr3 ( p, 2 ); + string_ref::size_type pos = sr1.find ( sr3 ); + BOOST_CHECK ( pos != string_ref::npos && pos <= static_cast( p - arg )); + p++; + } + + sr1 = arg; + p = arg; +// for all possible chars, see if we find them in the right place. +// Note that strchr will/might do the _wrong_ thing if we search for NULL + for ( int ch = 1; ch < 256; ++ch ) { + string_ref::size_type pos = sr1.find(ch); + const char *strp = std::strchr ( arg, ch ); + BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); + if ( strp != NULL ) + BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); + } + + sr1 = arg; + p = arg; +// for all possible chars, see if we find them in the right place. +// Note that strchr will/might do the _wrong_ thing if we search for NULL + for ( int ch = 1; ch < 256; ++ch ) { + string_ref::size_type pos = sr1.rfind(ch); + const char *strp = std::strrchr ( arg, ch ); + BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); + if ( strp != NULL ) + BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); + } + + +// Find everything at the start + p = arg; + sr1 = arg; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find(*p); + BOOST_CHECK ( pos == 0 ); + sr1.remove_prefix (1); + ++p; + } + +// Find everything at the end + sr1 = arg; + p = arg + std::strlen ( arg ) - 1; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.rfind(*p); + BOOST_CHECK ( pos == sr1.size () - 1 ); + sr1.remove_suffix (1); + --p; + } + +// Find everything at the start + sr1 = arg; + p = arg; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find_first_of(*p); + BOOST_CHECK ( pos == 0 ); + sr1.remove_prefix (1); + ++p; + } + + +// Find everything at the end + sr1 = arg; + p = arg + std::strlen ( arg ) - 1; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find_last_of(*p); + BOOST_CHECK ( pos == sr1.size () - 1 ); + sr1.remove_suffix (1); + --p; + } + +// Basic sanity checking for "find_first_of / find_first_not_of" + sr1 = arg; + sr2 = arg; + while ( !sr1.empty() ) { + BOOST_CHECK ( sr1.find_first_of ( sr2 ) == 0 ); + BOOST_CHECK ( sr1.find_first_not_of ( sr2 ) == string_ref::npos ); + sr1.remove_prefix ( 1 ); + } + + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos1 = sr1.find_first_of(*p); + string_ref::size_type pos2 = sr1.find_first_not_of(*p); + BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg )); + if ( pos2 != string_ref::npos ) { + for ( size_t i = 0 ; i < pos2; ++i ) + BOOST_CHECK ( sr1[i] == *p ); + BOOST_CHECK ( sr1 [ pos2 ] != *p ); + } + + BOOST_CHECK ( pos2 != pos1 ); + ++p; + } + +// Basic sanity checking for "find_last_of / find_last_not_of" + sr1 = arg; + sr2 = arg; + while ( !sr1.empty() ) { + BOOST_CHECK ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 )); + BOOST_CHECK ( sr1.find_last_not_of ( sr2 ) == string_ref::npos ); + sr1.remove_suffix ( 1 ); + } + + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos1 = sr1.find_last_of(*p); + string_ref::size_type pos2 = sr1.find_last_not_of(*p); + BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg )); + BOOST_CHECK ( pos2 == string_ref::npos || pos1 < sr1.size ()); + if ( pos2 != string_ref::npos ) { + for ( size_t i = sr1.size () -1 ; i > pos2; --i ) + BOOST_CHECK ( sr1[i] == *p ); + BOOST_CHECK ( sr1 [ pos2 ] != *p ); + } + + BOOST_CHECK ( pos2 != pos1 ); + ++p; + } + + } + + +void to_string ( const char *arg ) { + string_ref sr1; + std::string str1; + std::string str2; + + str1.assign ( arg ); + sr1 = arg; +// str2 = sr1.to_string > (); + str2 = sr1.to_string (); + BOOST_CHECK ( str1 == str2 ); + +#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS + std::string str3 = static_cast ( sr1 ); + BOOST_CHECK ( str1 == str3 ); +#endif + } + +void compare ( const char *arg ) { + string_ref sr1; + std::string str1; + std::string str2 = str1; + + str1.assign ( arg ); + sr1 = arg; + BOOST_CHECK ( sr1 == sr1); // compare string_ref and string_ref + BOOST_CHECK ( sr1 == str1); // compare string and string_ref + BOOST_CHECK ( str1 == sr1 ); // compare string_ref and string + BOOST_CHECK ( sr1 == arg ); // compare string_ref and pointer + BOOST_CHECK ( arg == sr1 ); // compare pointer and string_ref + + if ( sr1.size () > 0 ) { + (*str1.rbegin())++; + BOOST_CHECK ( sr1 != str1 ); + BOOST_CHECK ( str1 != sr1 ); + BOOST_CHECK ( sr1 < str1 ); + BOOST_CHECK ( sr1 <= str1 ); + BOOST_CHECK ( str1 > sr1 ); + BOOST_CHECK ( str1 >= sr1 ); + + (*str1.rbegin()) -= 2; + BOOST_CHECK ( sr1 != str1 ); + BOOST_CHECK ( str1 != sr1 ); + BOOST_CHECK ( sr1 > str1 ); + BOOST_CHECK ( sr1 >= str1 ); + BOOST_CHECK ( str1 < sr1 ); + BOOST_CHECK ( str1 <= sr1 ); + } + } + +const char *test_strings [] = { + "", + "0", + "abc", + "AAA", // all the same + "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf", + "abc\0asdfadsfasf", + NULL + }; + +BOOST_AUTO_TEST_CASE( test_main ) +{ + const char **p = &test_strings[0]; + + while ( *p != NULL ) { + starts_with ( *p ); + ends_with ( *p ); + reverse ( *p ); + find ( *p ); + to_string ( *p ); + compare ( *p ); + + p++; + } +} diff --git a/test/string_ref_test_io.cpp b/test/string_ref_test_io.cpp new file mode 100644 index 0000000..72f5837 --- /dev/null +++ b/test/string_ref_test_io.cpp @@ -0,0 +1,179 @@ +/* + * Copyright Andrey Semashev 2013. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file string_ref_test_io.cpp + * \author Andrey Semashev + * \date 26.05.2013 + * + * \brief This header contains tests for stream operations of \c basic_string_ref. + */ + +#define BOOST_TEST_MODULE string_ref_test_io + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +typedef boost::mpl::vector< + char +#if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_STD_WSTREAMBUF) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + , wchar_t +#endif +/* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available. +#if !defined(BOOST_NO_CXX11_CHAR16_T) + , char16_t +#endif +#if !defined(BOOST_NO_CXX11_CHAR32_T) + , char32_t +#endif +*/ +>::type char_types; + +static const char* test_strings[] = +{ + "begin", + "abcd", + "end" +}; + +//! The context with test data for particular character type +template< typename CharT > +struct context +{ + typedef CharT char_type; + typedef std::basic_string< char_type > string_type; + typedef std::basic_ostringstream< char_type > ostream_type; + + string_type begin, abcd, end; + + context() + { + boost::string_ref str = test_strings[0]; + std::copy(str.begin(), str.end(), std::back_inserter(begin)); + + str = test_strings[1]; + std::copy(str.begin(), str.end(), std::back_inserter(abcd)); + + str = test_strings[2]; + std::copy(str.begin(), str.end(), std::back_inserter(end)); + } +}; + +// Test regular output +BOOST_AUTO_TEST_CASE_TEMPLATE(string_ref_output, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + ostream_type strm; + strm << string_ref_type(ctx.abcd); + BOOST_CHECK(strm.str() == ctx.abcd); +} + +// Test support for padding +BOOST_AUTO_TEST_CASE_TEMPLATE(padding, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + // Test for padding + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } + + // Test for long padding + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::setw(100) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } + + // Test that short width does not truncate the string + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::setw(1) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } +} + +// Test support for padding fill +BOOST_AUTO_TEST_CASE_TEMPLATE(padding_fill, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + ostream_type strm_ref; + strm_ref << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); +} + +// Test support for alignment +BOOST_AUTO_TEST_CASE_TEMPLATE(alignment, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + // Left alignment + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::left << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } + + // Right alignment + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::right << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } +} From a9236d00a972c3d4a8606759c90c3e03133c4b08 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sat, 26 Mar 2016 14:00:12 +0300 Subject: [PATCH 06/12] Ensure the file ends with a newline. Fixes compiler warnings. --- include/boost/utility/string_view_fwd.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/utility/string_view_fwd.hpp b/include/boost/utility/string_view_fwd.hpp index 3764f99..30d1753 100644 --- a/include/boost/utility/string_view_fwd.hpp +++ b/include/boost/utility/string_view_fwd.hpp @@ -36,4 +36,4 @@ namespace boost { } -#endif \ No newline at end of file +#endif From 0b492bee9cca261066846216f0ba36602e2216b8 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Thu, 14 Apr 2016 07:50:28 -0700 Subject: [PATCH 07/12] Re-install string_ref - to be removed in the future --- include/boost/utility/string_ref.hpp | 516 +++++++++++++++++++++- include/boost/utility/string_ref_fwd.hpp | 12 +- include/boost/utility/string_view_fwd.hpp | 2 +- test/Jamfile.v2 | 3 + test/string_ref_test1.cpp | 109 +++++ test/string_ref_test2.cpp | 318 +++++++++++++ test/string_ref_test_io.cpp | 179 ++++++++ 7 files changed, 1129 insertions(+), 10 deletions(-) create mode 100644 test/string_ref_test1.cpp create mode 100644 test/string_ref_test2.cpp create mode 100644 test/string_ref_test_io.cpp diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index df5dcbe..8707157 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -18,9 +18,519 @@ #include #include #include -#include +#include -// The primary template has been changed to basic_string_view -// The convenience typedefs string_ref still exist. +#include +#include +#include +#include +#include +#include + +namespace boost { + + namespace detail { + // A helper functor because sometimes we don't have lambdas + template + class string_ref_traits_eq { + public: + string_ref_traits_eq ( charT ch ) : ch_(ch) {} + bool operator () ( charT val ) const { return traits::eq ( ch_, val ); } + charT ch_; + }; + } + + template + class basic_string_ref { + public: + // types + typedef charT value_type; + typedef const charT* pointer; + typedef const charT& reference; + typedef const charT& const_reference; + typedef pointer const_iterator; // impl-defined + typedef const_iterator iterator; + typedef std::reverse_iterator const_reverse_iterator; + typedef const_reverse_iterator reverse_iterator; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1); + + // construct/copy + BOOST_CONSTEXPR basic_string_ref () + : ptr_(NULL), len_(0) {} + + BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) + : ptr_(rhs.ptr_), len_(rhs.len_) {} + + basic_string_ref& operator=(const basic_string_ref &rhs) { + ptr_ = rhs.ptr_; + len_ = rhs.len_; + return *this; + } + + basic_string_ref(const charT* str) + : ptr_(str), len_(traits::length(str)) {} + + template + basic_string_ref(const std::basic_string& str) + : ptr_(str.data()), len_(str.length()) {} + + BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) + : ptr_(str), len_(len) {} + +#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS + template + explicit operator std::basic_string() const { + return std::basic_string ( begin(), end()); + } +#endif + + std::basic_string to_string () const { + return std::basic_string ( begin(), end()); + } + + // iterators + BOOST_CONSTEXPR const_iterator begin() const { return ptr_; } + BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; } + BOOST_CONSTEXPR const_iterator end() const { return ptr_ + len_; } + BOOST_CONSTEXPR const_iterator cend() const { return ptr_ + len_; } + const_reverse_iterator rbegin() const { return const_reverse_iterator (end()); } + const_reverse_iterator crbegin() const { return const_reverse_iterator (end()); } + const_reverse_iterator rend() const { return const_reverse_iterator (begin()); } + const_reverse_iterator crend() const { return const_reverse_iterator (begin()); } + + // capacity + BOOST_CONSTEXPR size_type size() const { return len_; } + BOOST_CONSTEXPR size_type length() const { return len_; } + BOOST_CONSTEXPR size_type max_size() const { return len_; } + BOOST_CONSTEXPR bool empty() const { return len_ == 0; } + + // element access + BOOST_CONSTEXPR const charT& operator[](size_type pos) const { return ptr_[pos]; } + + const charT& at(size_t pos) const { + if ( pos >= len_ ) + BOOST_THROW_EXCEPTION( std::out_of_range ( "boost::string_ref::at" ) ); + return ptr_[pos]; + } + + BOOST_CONSTEXPR const charT& front() const { return ptr_[0]; } + BOOST_CONSTEXPR const charT& back() const { return ptr_[len_-1]; } + BOOST_CONSTEXPR const charT* data() const { return ptr_; } + + // modifiers + void clear() { len_ = 0; } + void remove_prefix(size_type n) { + if ( n > len_ ) + n = len_; + ptr_ += n; + len_ -= n; + } + + void remove_suffix(size_type n) { + if ( n > len_ ) + n = len_; + len_ -= n; + } + + + // basic_string_ref string operations + basic_string_ref substr(size_type pos, size_type n=npos) const { + if ( pos > size()) + BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) ); + if ( n == npos || pos + n > size()) + n = size () - pos; + return basic_string_ref ( data() + pos, n ); + } + + int compare(basic_string_ref x) const { + const int cmp = traits::compare ( ptr_, x.ptr_, (std::min)(len_, x.len_)); + return cmp != 0 ? cmp : ( len_ == x.len_ ? 0 : len_ < x.len_ ? -1 : 1 ); + } + + bool starts_with(charT c) const { return !empty() && traits::eq ( c, front()); } + bool starts_with(basic_string_ref x) const { + return len_ >= x.len_ && traits::compare ( ptr_, x.ptr_, x.len_ ) == 0; + } + + bool ends_with(charT c) const { return !empty() && traits::eq ( c, back()); } + bool ends_with(basic_string_ref x) const { + return len_ >= x.len_ && traits::compare ( ptr_ + len_ - x.len_, x.ptr_, x.len_ ) == 0; + } + + size_type find(basic_string_ref s) const { + const_iterator iter = std::search ( this->cbegin (), this->cend (), + s.cbegin (), s.cend (), traits::eq ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find(charT c) const { + const_iterator iter = std::find_if ( this->cbegin (), this->cend (), + detail::string_ref_traits_eq ( c )); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type rfind(basic_string_ref s) const { + const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (), + s.crbegin (), s.crend (), traits::eq ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type rfind(charT c) const { + const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (), + detail::string_ref_traits_eq ( c )); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type find_first_of(charT c) const { return find (c); } + size_type find_last_of (charT c) const { return rfind (c); } + + size_type find_first_of(basic_string_ref s) const { + const_iterator iter = std::find_first_of + ( this->cbegin (), this->cend (), s.cbegin (), s.cend (), traits::eq ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find_last_of(basic_string_ref s) const { + const_reverse_iterator iter = std::find_first_of + ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); + } + + size_type find_first_not_of(basic_string_ref s) const { + const_iterator iter = find_not_of ( this->cbegin (), this->cend (), s ); + return iter == this->cend () ? npos : std::distance ( this->cbegin (), iter ); + } + + size_type find_first_not_of(charT c) const { + for ( const_iterator iter = this->cbegin (); iter != this->cend (); ++iter ) + if ( !traits::eq ( c, *iter )) + return std::distance ( this->cbegin (), iter ); + return npos; + } + + size_type find_last_not_of(basic_string_ref s) const { + const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s ); + return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + } + + size_type find_last_not_of(charT c) const { + for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter ) + if ( !traits::eq ( c, *iter )) + return reverse_distance ( this->crbegin (), iter ); + return npos; + } + + private: + template + size_type reverse_distance ( r_iter first, r_iter last ) const { + return len_ - 1 - std::distance ( first, last ); + } + + template + Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const { + for ( ; first != last ; ++first ) + if ( 0 == traits::find ( s.ptr_, s.len_, *first )) + return first; + return last; + } + + + + const charT *ptr_; + std::size_t len_; + }; + + +// Comparison operators +// Equality + template + inline bool operator==(basic_string_ref x, basic_string_ref y) { + if ( x.size () != y.size ()) return false; + return x.compare(y) == 0; + } + + template + inline bool operator==(basic_string_ref x, const std::basic_string & y) { + return x == basic_string_ref(y); + } + + template + inline bool operator==(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) == y; + } + + template + inline bool operator==(basic_string_ref x, const charT * y) { + return x == basic_string_ref(y); + } + + template + inline bool operator==(const charT * x, basic_string_ref y) { + return basic_string_ref(x) == y; + } + +// Inequality + template + inline bool operator!=(basic_string_ref x, basic_string_ref y) { + if ( x.size () != y.size ()) return true; + return x.compare(y) != 0; + } + + template + inline bool operator!=(basic_string_ref x, const std::basic_string & y) { + return x != basic_string_ref(y); + } + + template + inline bool operator!=(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) != y; + } + + template + inline bool operator!=(basic_string_ref x, const charT * y) { + return x != basic_string_ref(y); + } + + template + inline bool operator!=(const charT * x, basic_string_ref y) { + return basic_string_ref(x) != y; + } + +// Less than + template + inline bool operator<(basic_string_ref x, basic_string_ref y) { + return x.compare(y) < 0; + } + + template + inline bool operator<(basic_string_ref x, const std::basic_string & y) { + return x < basic_string_ref(y); + } + + template + inline bool operator<(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) < y; + } + + template + inline bool operator<(basic_string_ref x, const charT * y) { + return x < basic_string_ref(y); + } + + template + inline bool operator<(const charT * x, basic_string_ref y) { + return basic_string_ref(x) < y; + } + +// Greater than + template + inline bool operator>(basic_string_ref x, basic_string_ref y) { + return x.compare(y) > 0; + } + + template + inline bool operator>(basic_string_ref x, const std::basic_string & y) { + return x > basic_string_ref(y); + } + + template + inline bool operator>(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) > y; + } + + template + inline bool operator>(basic_string_ref x, const charT * y) { + return x > basic_string_ref(y); + } + + template + inline bool operator>(const charT * x, basic_string_ref y) { + return basic_string_ref(x) > y; + } + +// Less than or equal to + template + inline bool operator<=(basic_string_ref x, basic_string_ref y) { + return x.compare(y) <= 0; + } + + template + inline bool operator<=(basic_string_ref x, const std::basic_string & y) { + return x <= basic_string_ref(y); + } + + template + inline bool operator<=(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) <= y; + } + + template + inline bool operator<=(basic_string_ref x, const charT * y) { + return x <= basic_string_ref(y); + } + + template + inline bool operator<=(const charT * x, basic_string_ref y) { + return basic_string_ref(x) <= y; + } + +// Greater than or equal to + template + inline bool operator>=(basic_string_ref x, basic_string_ref y) { + return x.compare(y) >= 0; + } + + template + inline bool operator>=(basic_string_ref x, const std::basic_string & y) { + return x >= basic_string_ref(y); + } + + template + inline bool operator>=(const std::basic_string & x, basic_string_ref y) { + return basic_string_ref(x) >= y; + } + + template + inline bool operator>=(basic_string_ref x, const charT * y) { + return x >= basic_string_ref(y); + } + + template + inline bool operator>=(const charT * x, basic_string_ref y) { + return basic_string_ref(x) >= y; + } + + namespace detail { + + template + inline void insert_fill_chars(std::basic_ostream& os, std::size_t n) { + enum { chunk_size = 8 }; + charT fill_chars[chunk_size]; + std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); + for (; n >= chunk_size && os.good(); n -= chunk_size) + os.write(fill_chars, static_cast< std::size_t >(chunk_size)); + if (n > 0 && os.good()) + os.write(fill_chars, n); + } + + template + void insert_aligned(std::basic_ostream& os, const basic_string_ref& str) { + const std::size_t size = str.size(); + const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; + const bool align_left = (os.flags() & std::basic_ostream::adjustfield) == std::basic_ostream::left; + if (!align_left) { + detail::insert_fill_chars(os, alignment_size); + if (os.good()) + os.write(str.data(), size); + } + else { + os.write(str.data(), size); + if (os.good()) + detail::insert_fill_chars(os, alignment_size); + } + } + + } // namespace detail + + // Inserter + template + inline std::basic_ostream& + operator<<(std::basic_ostream& os, const basic_string_ref& str) { + if (os.good()) { + const std::size_t size = str.size(); + const std::size_t w = static_cast< std::size_t >(os.width()); + if (w <= size) + os.write(str.data(), size); + else + detail::insert_aligned(os, str); + os.width(0); + } + return os; + } + +#if 0 + // numeric conversions + // + // These are short-term implementations. + // In a production environment, I would rather avoid the copying. + // + inline int stoi (string_ref str, size_t* idx=0, int base=10) { + return std::stoi ( std::string(str), idx, base ); + } + + inline long stol (string_ref str, size_t* idx=0, int base=10) { + return std::stol ( std::string(str), idx, base ); + } + + inline unsigned long stoul (string_ref str, size_t* idx=0, int base=10) { + return std::stoul ( std::string(str), idx, base ); + } + + inline long long stoll (string_ref str, size_t* idx=0, int base=10) { + return std::stoll ( std::string(str), idx, base ); + } + + inline unsigned long long stoull (string_ref str, size_t* idx=0, int base=10) { + return std::stoull ( std::string(str), idx, base ); + } + + inline float stof (string_ref str, size_t* idx=0) { + return std::stof ( std::string(str), idx ); + } + + inline double stod (string_ref str, size_t* idx=0) { + return std::stod ( std::string(str), idx ); + } + + inline long double stold (string_ref str, size_t* idx=0) { + return std::stold ( std::string(str), idx ); + } + + inline int stoi (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoi ( std::wstring(str), idx, base ); + } + + inline long stol (wstring_ref str, size_t* idx=0, int base=10) { + return std::stol ( std::wstring(str), idx, base ); + } + + inline unsigned long stoul (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoul ( std::wstring(str), idx, base ); + } + + inline long long stoll (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoll ( std::wstring(str), idx, base ); + } + + inline unsigned long long stoull (wstring_ref str, size_t* idx=0, int base=10) { + return std::stoull ( std::wstring(str), idx, base ); + } + + inline float stof (wstring_ref str, size_t* idx=0) { + return std::stof ( std::wstring(str), idx ); + } + + inline double stod (wstring_ref str, size_t* idx=0) { + return std::stod ( std::wstring(str), idx ); + } + + inline long double stold (wstring_ref str, size_t* idx=0) { + return std::stold ( std::wstring(str), idx ); + } +#endif + +} + +#if 0 +namespace std { + // Hashing + template<> struct hash; + template<> struct hash; + template<> struct hash; + template<> struct hash; +} +#endif #endif diff --git a/include/boost/utility/string_ref_fwd.hpp b/include/boost/utility/string_ref_fwd.hpp index ead7b13..f58b98c 100644 --- a/include/boost/utility/string_ref_fwd.hpp +++ b/include/boost/utility/string_ref_fwd.hpp @@ -16,20 +16,20 @@ #define BOOST_STRING_REF_FWD_HPP #include -#include -#include // for std::char_traits +#include namespace boost { - typedef basic_string_view > string_ref; - typedef basic_string_view > wstring_ref; + template > class basic_string_ref; + typedef basic_string_ref > string_ref; + typedef basic_string_ref > wstring_ref; #ifndef BOOST_NO_CXX11_CHAR16_T - typedef basic_string_view > u16string_ref; + typedef basic_string_ref > u16string_ref; #endif #ifndef BOOST_NO_CXX11_CHAR32_T - typedef basic_string_view > u32string_ref; + typedef basic_string_ref > u32string_ref; #endif } diff --git a/include/boost/utility/string_view_fwd.hpp b/include/boost/utility/string_view_fwd.hpp index 30d1753..dbda0de 100644 --- a/include/boost/utility/string_view_fwd.hpp +++ b/include/boost/utility/string_view_fwd.hpp @@ -18,7 +18,7 @@ #define BOOST_STRING_VIEW_FWD_HPP #include -#include // for std::char_traits +#include namespace boost { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 6e39ef6..a2d5e4b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -27,6 +27,9 @@ test-suite utility [ run ../operators_test.cpp ../../test/build//boost_test_exec_monitor/static ] [ compile result_of_test.cpp ] [ run ../shared_iterator_test.cpp ] + [ run string_ref_test1.cpp unit_test_framework ] + [ run string_ref_test2.cpp unit_test_framework ] + [ run string_ref_test_io.cpp unit_test_framework ] [ run string_view_test1.cpp unit_test_framework ] [ run string_view_test2.cpp unit_test_framework ] [ run string_view_test_io.cpp unit_test_framework ] diff --git a/test/string_ref_test1.cpp b/test/string_ref_test1.cpp new file mode 100644 index 0000000..f1d4df2 --- /dev/null +++ b/test/string_ref_test1.cpp @@ -0,0 +1,109 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include +#include + +#include + +#define BOOST_TEST_MAIN +#include + +typedef boost::string_ref string_ref; + +// Should be equal +void interop ( const std::string &str, string_ref ref ) { +// BOOST_CHECK ( str == ref ); + BOOST_CHECK ( str.size () == ref.size ()); + BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ())); + BOOST_CHECK ( std::equal ( str.rbegin (), str.rend (), ref.rbegin ())); + } + +void null_tests ( const char *p ) { +// All zero-length string-refs should be equal + string_ref sr1; // NULL, 0 + string_ref sr2 ( NULL, 0 ); + string_ref sr3 ( p, 0 ); + string_ref sr4 ( p ); + sr4.clear (); + + BOOST_CHECK ( sr1 == sr2 ); + BOOST_CHECK ( sr1 == sr3 ); + BOOST_CHECK ( sr2 == sr3 ); + BOOST_CHECK ( sr1 == sr4 ); + } + +// make sure that substrings work just like strings +void test_substr ( const std::string &str ) { + const size_t sz = str.size (); + string_ref ref ( str ); + +// Substrings at the end + for ( size_t i = 0; i <= sz; ++ i ) + interop ( str.substr ( i ), ref.substr ( i )); + +// Substrings at the beginning + for ( size_t i = 0; i <= sz; ++ i ) + interop ( str.substr ( 0, i ), ref.substr ( 0, i )); + +// All possible substrings + for ( size_t i = 0; i < sz; ++i ) + for ( size_t j = i; j < sz; ++j ) + interop ( str.substr ( i, j ), ref.substr ( i, j )); + } + +// make sure that removing prefixes and suffixes work just like strings +void test_remove ( const std::string &str ) { + const size_t sz = str.size (); + std::string work; + string_ref ref; + + for ( size_t i = 1; i <= sz; ++i ) { + work = str; + ref = str; + while ( ref.size () >= i ) { + interop ( work, ref ); + work.erase ( 0, i ); + ref.remove_prefix (i); + } + } + + for ( size_t i = 1; i < sz; ++ i ) { + work = str; + ref = str; + while ( ref.size () >= i ) { + interop ( work, ref ); + work.erase ( work.size () - i, i ); + ref.remove_suffix (i); + } + } + } + +const char *test_strings [] = { + "", + "1", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + "0123456789", + NULL + }; + +BOOST_AUTO_TEST_CASE( test_main ) +{ + const char **p = &test_strings[0]; + + while ( *p != NULL ) { + interop ( *p, *p ); + test_substr ( *p ); + test_remove ( *p ); + null_tests ( *p ); + + p++; + } +} diff --git a/test/string_ref_test2.cpp b/test/string_ref_test2.cpp new file mode 100644 index 0000000..9559c67 --- /dev/null +++ b/test/string_ref_test2.cpp @@ -0,0 +1,318 @@ +/* + Copyright (c) Marshall Clow 2012-2012. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + For more information, see http://www.boost.org +*/ + +#include +#include // for std::strchr + +#include + +#define BOOST_TEST_MAIN +#include + +typedef boost::string_ref string_ref; + +void ends_with ( const char *arg ) { + const size_t sz = std::strlen ( arg ); + string_ref sr ( arg ); + string_ref sr2 ( arg ); + const char *p = arg; + + while ( *p ) { + BOOST_CHECK ( sr.ends_with ( p )); + ++p; + } + + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.ends_with ( sr2 )); + sr2.remove_prefix (1); + } + + sr2 = arg; + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.ends_with ( sr2 )); + sr2.remove_prefix (1); + } + + char ch = sz == 0 ? '\0' : arg [ sz - 1 ]; + sr2 = arg; + if ( sz > 0 ) + BOOST_CHECK ( sr2.ends_with ( ch )); + BOOST_CHECK ( !sr2.ends_with ( ++ch )); + BOOST_CHECK ( sr2.ends_with ( string_ref ())); + } + +void starts_with ( const char *arg ) { + const size_t sz = std::strlen ( arg ); + string_ref sr ( arg ); + string_ref sr2 ( arg ); + const char *p = arg + std::strlen ( arg ) - 1; + while ( p >= arg ) { + std::string foo ( arg, p + 1 ); + BOOST_CHECK ( sr.starts_with ( foo )); + --p; + } + + while ( !sr2.empty ()) { + BOOST_CHECK ( sr.starts_with ( sr2 )); + sr2.remove_suffix (1); + } + + char ch = *arg; + sr2 = arg; + if ( sz > 0 ) + BOOST_CHECK ( sr2.starts_with ( ch )); + BOOST_CHECK ( !sr2.starts_with ( ++ch )); + BOOST_CHECK ( sr2.starts_with ( string_ref ())); + } + +void reverse ( const char *arg ) { +// Round trip + string_ref sr1 ( arg ); + std::string string1 ( sr1.rbegin (), sr1.rend ()); + string_ref sr2 ( string1 ); + std::string string2 ( sr2.rbegin (), sr2.rend ()); + + BOOST_CHECK ( std::equal ( sr2.rbegin (), sr2.rend (), arg )); + BOOST_CHECK ( string2 == arg ); + BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ())); + } + +// This helper function eliminates signed vs. unsigned warnings +string_ref::size_type ptr_diff ( const char *res, const char *base ) { + BOOST_CHECK ( res >= base ); + return static_cast ( res - base ); + } + +void find ( const char *arg ) { + string_ref sr1; + string_ref sr2; + const char *p; + +// Look for each character in the string(searching from the start) + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos = sr1.find(*p); + BOOST_CHECK ( pos != string_ref::npos && ( pos <= ptr_diff ( p, arg ))); + ++p; + } + +// Look for each character in the string (searching from the end) + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos = sr1.rfind(*p); + BOOST_CHECK ( pos != string_ref::npos && pos < sr1.size () && ( pos >= ptr_diff ( p, arg ))); + ++p; + } + +// Look for pairs on characters (searching from the start) + sr1 = arg; + p = arg; + while ( *p && *(p+1)) { + string_ref sr3 ( p, 2 ); + string_ref::size_type pos = sr1.find ( sr3 ); + BOOST_CHECK ( pos != string_ref::npos && pos <= static_cast( p - arg )); + p++; + } + + sr1 = arg; + p = arg; +// for all possible chars, see if we find them in the right place. +// Note that strchr will/might do the _wrong_ thing if we search for NULL + for ( int ch = 1; ch < 256; ++ch ) { + string_ref::size_type pos = sr1.find(ch); + const char *strp = std::strchr ( arg, ch ); + BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); + if ( strp != NULL ) + BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); + } + + sr1 = arg; + p = arg; +// for all possible chars, see if we find them in the right place. +// Note that strchr will/might do the _wrong_ thing if we search for NULL + for ( int ch = 1; ch < 256; ++ch ) { + string_ref::size_type pos = sr1.rfind(ch); + const char *strp = std::strrchr ( arg, ch ); + BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos )); + if ( strp != NULL ) + BOOST_CHECK ( ptr_diff ( strp, arg ) == pos ); + } + + +// Find everything at the start + p = arg; + sr1 = arg; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find(*p); + BOOST_CHECK ( pos == 0 ); + sr1.remove_prefix (1); + ++p; + } + +// Find everything at the end + sr1 = arg; + p = arg + std::strlen ( arg ) - 1; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.rfind(*p); + BOOST_CHECK ( pos == sr1.size () - 1 ); + sr1.remove_suffix (1); + --p; + } + +// Find everything at the start + sr1 = arg; + p = arg; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find_first_of(*p); + BOOST_CHECK ( pos == 0 ); + sr1.remove_prefix (1); + ++p; + } + + +// Find everything at the end + sr1 = arg; + p = arg + std::strlen ( arg ) - 1; + while ( !sr1.empty ()) { + string_ref::size_type pos = sr1.find_last_of(*p); + BOOST_CHECK ( pos == sr1.size () - 1 ); + sr1.remove_suffix (1); + --p; + } + +// Basic sanity checking for "find_first_of / find_first_not_of" + sr1 = arg; + sr2 = arg; + while ( !sr1.empty() ) { + BOOST_CHECK ( sr1.find_first_of ( sr2 ) == 0 ); + BOOST_CHECK ( sr1.find_first_not_of ( sr2 ) == string_ref::npos ); + sr1.remove_prefix ( 1 ); + } + + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos1 = sr1.find_first_of(*p); + string_ref::size_type pos2 = sr1.find_first_not_of(*p); + BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 <= ptr_diff ( p, arg )); + if ( pos2 != string_ref::npos ) { + for ( size_t i = 0 ; i < pos2; ++i ) + BOOST_CHECK ( sr1[i] == *p ); + BOOST_CHECK ( sr1 [ pos2 ] != *p ); + } + + BOOST_CHECK ( pos2 != pos1 ); + ++p; + } + +// Basic sanity checking for "find_last_of / find_last_not_of" + sr1 = arg; + sr2 = arg; + while ( !sr1.empty() ) { + BOOST_CHECK ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 )); + BOOST_CHECK ( sr1.find_last_not_of ( sr2 ) == string_ref::npos ); + sr1.remove_suffix ( 1 ); + } + + p = arg; + sr1 = arg; + while ( *p ) { + string_ref::size_type pos1 = sr1.find_last_of(*p); + string_ref::size_type pos2 = sr1.find_last_not_of(*p); + BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 >= ptr_diff ( p, arg )); + BOOST_CHECK ( pos2 == string_ref::npos || pos1 < sr1.size ()); + if ( pos2 != string_ref::npos ) { + for ( size_t i = sr1.size () -1 ; i > pos2; --i ) + BOOST_CHECK ( sr1[i] == *p ); + BOOST_CHECK ( sr1 [ pos2 ] != *p ); + } + + BOOST_CHECK ( pos2 != pos1 ); + ++p; + } + + } + + +void to_string ( const char *arg ) { + string_ref sr1; + std::string str1; + std::string str2; + + str1.assign ( arg ); + sr1 = arg; +// str2 = sr1.to_string > (); + str2 = sr1.to_string (); + BOOST_CHECK ( str1 == str2 ); + +#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS + std::string str3 = static_cast ( sr1 ); + BOOST_CHECK ( str1 == str3 ); +#endif + } + +void compare ( const char *arg ) { + string_ref sr1; + std::string str1; + std::string str2 = str1; + + str1.assign ( arg ); + sr1 = arg; + BOOST_CHECK ( sr1 == sr1); // compare string_ref and string_ref + BOOST_CHECK ( sr1 == str1); // compare string and string_ref + BOOST_CHECK ( str1 == sr1 ); // compare string_ref and string + BOOST_CHECK ( sr1 == arg ); // compare string_ref and pointer + BOOST_CHECK ( arg == sr1 ); // compare pointer and string_ref + + if ( sr1.size () > 0 ) { + (*str1.rbegin())++; + BOOST_CHECK ( sr1 != str1 ); + BOOST_CHECK ( str1 != sr1 ); + BOOST_CHECK ( sr1 < str1 ); + BOOST_CHECK ( sr1 <= str1 ); + BOOST_CHECK ( str1 > sr1 ); + BOOST_CHECK ( str1 >= sr1 ); + + (*str1.rbegin()) -= 2; + BOOST_CHECK ( sr1 != str1 ); + BOOST_CHECK ( str1 != sr1 ); + BOOST_CHECK ( sr1 > str1 ); + BOOST_CHECK ( sr1 >= str1 ); + BOOST_CHECK ( str1 < sr1 ); + BOOST_CHECK ( str1 <= sr1 ); + } + } + +const char *test_strings [] = { + "", + "0", + "abc", + "AAA", // all the same + "adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf", + "abc\0asdfadsfasf", + NULL + }; + +BOOST_AUTO_TEST_CASE( test_main ) +{ + const char **p = &test_strings[0]; + + while ( *p != NULL ) { + starts_with ( *p ); + ends_with ( *p ); + reverse ( *p ); + find ( *p ); + to_string ( *p ); + compare ( *p ); + + p++; + } +} diff --git a/test/string_ref_test_io.cpp b/test/string_ref_test_io.cpp new file mode 100644 index 0000000..72f5837 --- /dev/null +++ b/test/string_ref_test_io.cpp @@ -0,0 +1,179 @@ +/* + * Copyright Andrey Semashev 2013. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file string_ref_test_io.cpp + * \author Andrey Semashev + * \date 26.05.2013 + * + * \brief This header contains tests for stream operations of \c basic_string_ref. + */ + +#define BOOST_TEST_MODULE string_ref_test_io + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +typedef boost::mpl::vector< + char +#if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_STD_WSTREAMBUF) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + , wchar_t +#endif +/* Current implementations seem to be missing codecvt facets to convert chars to char16_t and char32_t even though the types are available. +#if !defined(BOOST_NO_CXX11_CHAR16_T) + , char16_t +#endif +#if !defined(BOOST_NO_CXX11_CHAR32_T) + , char32_t +#endif +*/ +>::type char_types; + +static const char* test_strings[] = +{ + "begin", + "abcd", + "end" +}; + +//! The context with test data for particular character type +template< typename CharT > +struct context +{ + typedef CharT char_type; + typedef std::basic_string< char_type > string_type; + typedef std::basic_ostringstream< char_type > ostream_type; + + string_type begin, abcd, end; + + context() + { + boost::string_ref str = test_strings[0]; + std::copy(str.begin(), str.end(), std::back_inserter(begin)); + + str = test_strings[1]; + std::copy(str.begin(), str.end(), std::back_inserter(abcd)); + + str = test_strings[2]; + std::copy(str.begin(), str.end(), std::back_inserter(end)); + } +}; + +// Test regular output +BOOST_AUTO_TEST_CASE_TEMPLATE(string_ref_output, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + ostream_type strm; + strm << string_ref_type(ctx.abcd); + BOOST_CHECK(strm.str() == ctx.abcd); +} + +// Test support for padding +BOOST_AUTO_TEST_CASE_TEMPLATE(padding, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + // Test for padding + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } + + // Test for long padding + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::setw(100) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setw(100) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } + + // Test that short width does not truncate the string + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::setw(1) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setw(1) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } +} + +// Test support for padding fill +BOOST_AUTO_TEST_CASE_TEMPLATE(padding_fill, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + ostream_type strm_ref; + strm_ref << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::setfill(static_cast< char_type >('x')) << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); +} + +// Test support for alignment +BOOST_AUTO_TEST_CASE_TEMPLATE(alignment, CharT, char_types) +{ + typedef CharT char_type; + typedef std::basic_ostringstream< char_type > ostream_type; + typedef boost::basic_string_ref< char_type > string_ref_type; + + context< char_type > ctx; + + // Left alignment + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::left << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::left << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } + + // Right alignment + { + ostream_type strm_ref; + strm_ref << ctx.begin << std::right << std::setw(8) << string_ref_type(ctx.abcd) << ctx.end; + + ostream_type strm_correct; + strm_correct << ctx.begin << std::right << std::setw(8) << ctx.abcd << ctx.end; + + BOOST_CHECK(strm_ref.str() == strm_correct.str()); + } +} From 181f302ee4c8153aa2ad91bfbc123294a34e05cb Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 15 Jun 2016 15:50:21 -0700 Subject: [PATCH 08/12] Fix Ticket 12140; mark only single-arg ctor as explicit. Thanks to Thimo for the patch. --- include/boost/utility/base_from_member.hpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/boost/utility/base_from_member.hpp b/include/boost/utility/base_from_member.hpp index fc0e13c..604541d 100644 --- a/include/boost/utility/base_from_member.hpp +++ b/include/boost/utility/base_from_member.hpp @@ -47,11 +47,11 @@ // {} // This macro should only persist within this file. -#define BOOST_PRIVATE_CTR_DEF( z, n, data ) \ - template < BOOST_PP_ENUM_PARAMS(n, typename T) > \ - explicit base_from_member( BOOST_PP_ENUM_BINARY_PARAMS(n, T, x) ) \ - : member( BOOST_PP_ENUM_PARAMS(n, x) ) \ - {} \ +#define BOOST_PRIVATE_CTR_DEF( z, n, data ) \ + template < BOOST_PP_ENUM_PARAMS(n, typename T) > \ + base_from_member( BOOST_PP_ENUM_BINARY_PARAMS(n, T, x) ) \ + : member( BOOST_PP_ENUM_PARAMS(n, x) ) \ + {} \ /**/ @@ -142,7 +142,8 @@ protected: : member() {} - BOOST_PP_REPEAT_FROM_TO( 1, BOOST_PP_INC(BOOST_BASE_FROM_MEMBER_MAX_ARITY), + template < typename T0 > explicit base_from_member( T0 x0 ) : member( x0 ) {} + BOOST_PP_REPEAT_FROM_TO( 2, BOOST_PP_INC(BOOST_BASE_FROM_MEMBER_MAX_ARITY), BOOST_PRIVATE_CTR_DEF, _ ) #endif From c56dd13592daa332a74b90aeca8724c5b3cc3d5d Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 27 Jul 2016 11:18:18 -0700 Subject: [PATCH 09/12] Mark the copy ctor/assignment operator as '= default' when the compiler supports it. This makes these types trivially copy/move assignable/constructible. See https://svn.boost.org/trac/boost/ticket/11684 --- include/boost/utility/string_ref.hpp | 21 ++++++++++++++++----- include/boost/utility/string_view.hpp | 13 ++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 8707157..1283f38 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -57,26 +57,37 @@ namespace boost { static BOOST_CONSTEXPR_OR_CONST size_type npos = size_type(-1); // construct/copy - BOOST_CONSTEXPR basic_string_ref () + BOOST_CONSTEXPR basic_string_ref () BOOST_NOEXCEPT : ptr_(NULL), len_(0) {} - BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) + // by defaulting these functions, basic_string_ref becomes + // trivially copy/move constructible. + BOOST_CONSTEXPR basic_string_ref (const basic_string_ref &rhs) BOOST_NOEXCEPT +#ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS + = default; +#else : ptr_(rhs.ptr_), len_(rhs.len_) {} +#endif - basic_string_ref& operator=(const basic_string_ref &rhs) { + basic_string_ref& operator=(const basic_string_ref &rhs) BOOST_NOEXCEPT +#ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS + = default; +#else + { ptr_ = rhs.ptr_; len_ = rhs.len_; return *this; } +#endif - basic_string_ref(const charT* str) + basic_string_ref(const charT* str) BOOST_NOEXCEPT : ptr_(str), len_(traits::length(str)) {} template basic_string_ref(const std::basic_string& str) : ptr_(str.data()), len_(str.length()) {} - BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) + BOOST_CONSTEXPR basic_string_ref(const charT* str, size_type len) BOOST_NOEXCEPT : ptr_(str), len_(len) {} #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index 9de32cc..070927d 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -65,14 +65,25 @@ namespace boost { BOOST_CONSTEXPR basic_string_view() BOOST_NOEXCEPT : ptr_(NULL), len_(0) {} + // by defaulting these functions, basic_string_ref becomes + // trivially copy/move constructible. BOOST_CONSTEXPR basic_string_view(const basic_string_view &rhs) BOOST_NOEXCEPT +#ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS + = default; +#else : ptr_(rhs.ptr_), len_(rhs.len_) {} +#endif - basic_string_view& operator=(const basic_string_view &rhs) BOOST_NOEXCEPT { + basic_string_view& operator=(const basic_string_view &rhs) BOOST_NOEXCEPT +#ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS + = default; +#else + { ptr_ = rhs.ptr_; len_ = rhs.len_; return *this; } +#endif template basic_string_view(const std::basic_string Date: Sun, 14 Aug 2016 11:11:12 -0700 Subject: [PATCH 10/12] rename routines in detail namespace so that someone who includes both and won't get duplicate decls --- include/boost/utility/string_ref.hpp | 10 +++++----- include/boost/utility/string_view.hpp | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 1283f38..9b597c8 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -416,7 +416,7 @@ namespace boost { namespace detail { template - inline void insert_fill_chars(std::basic_ostream& os, std::size_t n) { + inline void sr_insert_fill_chars(std::basic_ostream& os, std::size_t n) { enum { chunk_size = 8 }; charT fill_chars[chunk_size]; std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); @@ -427,19 +427,19 @@ namespace boost { } template - void insert_aligned(std::basic_ostream& os, const basic_string_ref& str) { + void sr_insert_aligned(std::basic_ostream& os, const basic_string_ref& str) { const std::size_t size = str.size(); const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; const bool align_left = (os.flags() & std::basic_ostream::adjustfield) == std::basic_ostream::left; if (!align_left) { - detail::insert_fill_chars(os, alignment_size); + detail::sr_insert_fill_chars(os, alignment_size); if (os.good()) os.write(str.data(), size); } else { os.write(str.data(), size); if (os.good()) - detail::insert_fill_chars(os, alignment_size); + detail::sr_insert_fill_chars(os, alignment_size); } } @@ -455,7 +455,7 @@ namespace boost { if (w <= size) os.write(str.data(), size); else - detail::insert_aligned(os, str); + detail::sr_insert_aligned(os, str); os.width(0); } return os; diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index 070927d..e578f5e 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -549,7 +549,7 @@ namespace boost { namespace detail { template - inline void insert_fill_chars(std::basic_ostream& os, std::size_t n) { + inline void sv_insert_fill_chars(std::basic_ostream& os, std::size_t n) { enum { chunk_size = 8 }; charT fill_chars[chunk_size]; std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); @@ -560,19 +560,19 @@ namespace boost { } template - void insert_aligned(std::basic_ostream& os, const basic_string_view& str) { + void sv_insert_aligned(std::basic_ostream& os, const basic_string_view& str) { const std::size_t size = str.size(); const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; const bool align_left = (os.flags() & std::basic_ostream::adjustfield) == std::basic_ostream::left; if (!align_left) { - detail::insert_fill_chars(os, alignment_size); + detail::sv_insert_fill_chars(os, alignment_size); if (os.good()) os.write(str.data(), size); } else { os.write(str.data(), size); if (os.good()) - detail::insert_fill_chars(os, alignment_size); + detail::sv_insert_fill_chars(os, alignment_size); } } @@ -589,7 +589,7 @@ namespace boost { if (w <= size) os.write(str.data(), size); else - detail::insert_aligned(os, str); + detail::sv_insert_aligned(os, str); os.width(0); } return os; From 8392991c467a950c055ad96797b0f19f0be44918 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sun, 14 Aug 2016 11:19:32 -0700 Subject: [PATCH 11/12] Remove extraneous semicolon; no functional change --- include/boost/utility/string_view.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index e578f5e..e3cb0ae 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -255,7 +255,7 @@ namespace boost { pos = len_ - s.len_; if (s.len_ == 0u) // an empty string is always found return pos; - for (const charT* cur = ptr_ + pos;; --cur) { + for (const charT* cur = ptr_ + pos; ; --cur) { if (traits::compare(cur, s.ptr_, s.len_) == 0) return cur - ptr_; if (cur == ptr_) @@ -322,7 +322,7 @@ namespace boost { // find_last_not_of BOOST_CXX14_CONSTEXPR size_type find_last_not_of(basic_string_view s, size_type pos = npos) const BOOST_NOEXCEPT { if (pos >= len_) - pos = len_ - 1;; + pos = len_ - 1; if (s.len_ == 0u) return pos; pos = len_ - (pos+1); From 39577f86d1b3a260afc1e7f7caec1ee8f12594bb Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sun, 14 Aug 2016 11:20:28 -0700 Subject: [PATCH 12/12] Fix rfind (and other finders). Fixes bug https://svn.boost.org/trac/boost/ticket/9518 --- include/boost/utility/string_ref.hpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 9b597c8..447d3cc 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -185,13 +185,13 @@ namespace boost { size_type rfind(basic_string_ref s) const { const_reverse_iterator iter = std::search ( this->crbegin (), this->crend (), s.crbegin (), s.crend (), traits::eq ); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + return iter == this->crend () ? npos : (std::distance(iter, this->crend()) - s.size()); } size_type rfind(charT c) const { const_reverse_iterator iter = std::find_if ( this->crbegin (), this->crend (), detail::string_ref_traits_eq ( c )); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter)); } size_type find_first_of(charT c) const { return find (c); } @@ -206,7 +206,7 @@ namespace boost { size_type find_last_of(basic_string_ref s) const { const_reverse_iterator iter = std::find_first_of ( this->crbegin (), this->crend (), s.cbegin (), s.cend (), traits::eq ); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter); + return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter)); } size_type find_first_not_of(basic_string_ref s) const { @@ -223,21 +223,17 @@ namespace boost { size_type find_last_not_of(basic_string_ref s) const { const_reverse_iterator iter = find_not_of ( this->crbegin (), this->crend (), s ); - return iter == this->crend () ? npos : reverse_distance ( this->crbegin (), iter ); + return iter == this->crend () ? npos : (this->size() - 1 - std::distance(this->crbegin(), iter)); } size_type find_last_not_of(charT c) const { for ( const_reverse_iterator iter = this->crbegin (); iter != this->crend (); ++iter ) if ( !traits::eq ( c, *iter )) - return reverse_distance ( this->crbegin (), iter ); + return this->size() - 1 - std::distance(this->crbegin(), iter); return npos; } private: - template - size_type reverse_distance ( r_iter first, r_iter last ) const { - return len_ - 1 - std::distance ( first, last ); - } template Iterator find_not_of ( Iterator first, Iterator last, basic_string_ref s ) const {