From 6e2c1b6b53fdc571dbd81453159029582fbf39ff Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Fri, 15 Feb 2013 16:12:30 +0000 Subject: [PATCH] Added to_string and better comparisons to Boost.StringRef [SVN r82902] --- include/boost/utility/string_ref.hpp | 68 +++++++++++++++++++++++++++- test/string_ref_test2.cpp | 9 ++-- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 1be2ac9..657e9b6 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -34,6 +34,8 @@ namespace boost { bool operator () ( charT val ) const { return traits::eq ( ch_, val ); } charT ch_; }; + + template struct __identity { typedef T type; }; } template class basic_string_ref; @@ -94,6 +96,10 @@ namespace boost { } #endif + std::basic_string to_string () const { + return std::basic_string ( ptr_, len_ ); + } + // iterators BOOST_CONSTEXPR const_iterator begin() const { return ptr_; } BOOST_CONSTEXPR const_iterator cbegin() const { return ptr_; } @@ -252,38 +258,98 @@ namespace boost { std::size_t len_; }; - // Comparison operators + +// Comparison operators (3 for each operation) +// Equality template bool operator==(basic_string_ref x, basic_string_ref y) { if ( x.size () != y.size ()) return false; return x.compare(y) == 0; } + template + bool operator==(basic_string_ref x, typename detail::__identity >::type y) { + if ( x.size () != y.size ()) return false; + return x.compare(y) == 0; + } + template + bool operator==(typename detail::__identity >::type x, basic_string_ref y) { + if ( x.size () != y.size ()) return false; + return x.compare(y) == 0; + } +// Inequality template bool operator!=(basic_string_ref x, basic_string_ref y) { if ( x.size () != y.size ()) return true; return x.compare(y) != 0; } + template + bool operator!=(basic_string_ref x, typename detail::__identity >::type y) { + if ( x.size () != y.size ()) return true; + return x.compare(y) != 0; + } + template + bool operator!=(typename detail::__identity >::type x, basic_string_ref y) { + if ( x.size () != y.size ()) return true; + return x.compare(y) != 0; + } +// Less than template bool operator<(basic_string_ref x, basic_string_ref y) { return x.compare(y) < 0; } + template + bool operator<(basic_string_ref x, typename detail::__identity >::type y) { + return x.compare(y) < 0; + } + template + bool operator<(typename detail::__identity >::type x, basic_string_ref y) { + return x.compare(y) < 0; + } +// Greater than template bool operator>(basic_string_ref x, basic_string_ref y) { return x.compare(y) > 0; } + template + bool operator>(basic_string_ref x, typename detail::__identity >::type y) { + return x.compare(y) > 0; + } + template + bool operator>(typename detail::__identity >::type x, basic_string_ref y) { + return x.compare(y) > 0; + } +// Less than or equal to template bool operator<=(basic_string_ref x, basic_string_ref y) { return x.compare(y) <= 0; } + template + bool operator<=(basic_string_ref x, typename detail::__identity >::type y) { + return x.compare(y) <= 0; + } + template + bool operator<=(typename detail::__identity >::type x, basic_string_ref y) { + return x.compare(y) <= 0; + } +// Greater than or equal to template bool operator>=(basic_string_ref x, basic_string_ref y) { return x.compare(y) >= 0; } + template + bool operator>=(basic_string_ref x, typename detail::__identity >::type y) { + return x.compare(y) >= 0; + } + template + bool operator>=(typename detail::__identity >::type x, basic_string_ref y) { + return x.compare(y) >= 0; + } + // Inserter diff --git a/test/string_ref_test2.cpp b/test/string_ref_test2.cpp index 4624fb9..c4885a4 100644 --- a/test/string_ref_test2.cpp +++ b/test/string_ref_test2.cpp @@ -118,7 +118,7 @@ void find ( const char *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 <= ( p - arg )); + BOOST_CHECK ( pos != string_ref::npos && pos <= static_cast( p - arg )); p++; } @@ -241,7 +241,7 @@ void find ( const char *arg ) { } -#if 0 + void to_string ( const char *arg ) { string_ref sr1; std::string str1; @@ -289,7 +289,6 @@ void compare ( const char *arg ) { BOOST_CHECK ( str1 <= sr1 ); } } -#endif const char *test_strings [] = { "", @@ -310,8 +309,8 @@ BOOST_AUTO_TEST_CASE( test_main ) ends_with ( *p ); reverse ( *p ); find ( *p ); -// to_string ( *p ); -// compare ( *p ); + to_string ( *p ); + compare ( *p ); p++; }