Added to_string and better comparisons to Boost.StringRef

[SVN r82902]
This commit is contained in:
Marshall Clow 2013-02-15 16:12:30 +00:00
parent e4d622019f
commit 6e2c1b6b53
2 changed files with 71 additions and 6 deletions

View File

@ -34,6 +34,8 @@ namespace boost {
bool operator () ( charT val ) const { return traits::eq ( ch_, val ); }
charT ch_;
};
template<typename T> struct __identity { typedef T type; };
}
template<typename charT, typename traits> class basic_string_ref;
@ -94,6 +96,10 @@ namespace boost {
}
#endif
std::basic_string<charT, traits> to_string () const {
return std::basic_string<charT, traits> ( 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<typename charT, typename traits>
bool operator==(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
if ( x.size () != y.size ()) return false;
return x.compare(y) == 0;
}
template<typename charT, typename traits>
bool operator==(basic_string_ref<charT, traits> x, typename detail::__identity<basic_string_ref<charT, traits> >::type y) {
if ( x.size () != y.size ()) return false;
return x.compare(y) == 0;
}
template<typename charT, typename traits>
bool operator==(typename detail::__identity<basic_string_ref<charT, traits> >::type x, basic_string_ref<charT, traits> y) {
if ( x.size () != y.size ()) return false;
return x.compare(y) == 0;
}
// Inequality
template<typename charT, typename traits>
bool operator!=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
if ( x.size () != y.size ()) return true;
return x.compare(y) != 0;
}
template<typename charT, typename traits>
bool operator!=(basic_string_ref<charT, traits> x, typename detail::__identity<basic_string_ref<charT, traits> >::type y) {
if ( x.size () != y.size ()) return true;
return x.compare(y) != 0;
}
template<typename charT, typename traits>
bool operator!=(typename detail::__identity<basic_string_ref<charT, traits> >::type x, basic_string_ref<charT, traits> y) {
if ( x.size () != y.size ()) return true;
return x.compare(y) != 0;
}
// Less than
template<typename charT, typename traits>
bool operator<(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
return x.compare(y) < 0;
}
template<typename charT, typename traits>
bool operator<(basic_string_ref<charT, traits> x, typename detail::__identity<basic_string_ref<charT, traits> >::type y) {
return x.compare(y) < 0;
}
template<typename charT, typename traits>
bool operator<(typename detail::__identity<basic_string_ref<charT, traits> >::type x, basic_string_ref<charT, traits> y) {
return x.compare(y) < 0;
}
// Greater than
template<typename charT, typename traits>
bool operator>(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
return x.compare(y) > 0;
}
template<typename charT, typename traits>
bool operator>(basic_string_ref<charT, traits> x, typename detail::__identity<basic_string_ref<charT, traits> >::type y) {
return x.compare(y) > 0;
}
template<typename charT, typename traits>
bool operator>(typename detail::__identity<basic_string_ref<charT, traits> >::type x, basic_string_ref<charT, traits> y) {
return x.compare(y) > 0;
}
// Less than or equal to
template<typename charT, typename traits>
bool operator<=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
return x.compare(y) <= 0;
}
template<typename charT, typename traits>
bool operator<=(basic_string_ref<charT, traits> x, typename detail::__identity<basic_string_ref<charT, traits> >::type y) {
return x.compare(y) <= 0;
}
template<typename charT, typename traits>
bool operator<=(typename detail::__identity<basic_string_ref<charT, traits> >::type x, basic_string_ref<charT, traits> y) {
return x.compare(y) <= 0;
}
// Greater than or equal to
template<typename charT, typename traits>
bool operator>=(basic_string_ref<charT, traits> x, basic_string_ref<charT, traits> y) {
return x.compare(y) >= 0;
}
template<typename charT, typename traits>
bool operator>=(basic_string_ref<charT, traits> x, typename detail::__identity<basic_string_ref<charT, traits> >::type y) {
return x.compare(y) >= 0;
}
template<typename charT, typename traits>
bool operator>=(typename detail::__identity<basic_string_ref<charT, traits> >::type x, basic_string_ref<charT, traits> y) {
return x.compare(y) >= 0;
}
// Inserter

View File

@ -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<string_ref::size_type>( 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++;
}